Skip to content

Commit e97536d

Browse files
committed
Fix FLIMAX; floor/ceil for WASM; change pmctr to pmlvl
1 parent a78654a commit e97536d

11 files changed

Lines changed: 33 additions & 28 deletions

File tree

jsrc/f.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ static C*dropl(C*zu,C*zv,I lb,I la,C*eol){C ec0,ec1,*u,*v;I n,p,zn=zv-zu;
797797
// maxlen is JT(jt,outmaxlen): max length of a line (later chars replaced by ...)
798798
// lb is JT(jt,outmaxbefore): number of leading lines to display
799799
// la is JT(jt,outmaxafter): number of trailing lines to display
800-
static A jtjprx(J jt,I ieol,I maxlen,I lb,I la,A w){A y,z;B ch;C e,eov[2],*v,x,*zu,*zv;D lba;
800+
static A jtjprx(J jt,I ieol,I maxlen,I lb,I la,A w){A y,z;B ch;C e,eov[2],*v,x,*zu,*zv;
801801
I c,c1,h,i,j,k,lc,m,nbx,nq,p,q,r,*s,t,zn;
802802
static C bdc[]="123456789_123456\214\254\220\234\274\244\224\264\230\202\200";
803803
// Convert w to a character array; set t=1 if it's LIT, t=2 if C2T, 4 if C4T
@@ -812,11 +812,12 @@ static A jtjprx(J jt,I ieol,I maxlen,I lb,I la,A w){A y,z;B ch;C e,eov[2],*v,x,*
812812
// if w is empty the values could overflow. In that case, just display nothing
813813
SHAPEN(y,r-2,q); SHAPEN(y,r-1,c); nq=prod(r-1,s); if(jt->jerr){RESETERR z=str(m+1,eov); makewritable(z) CAV(z)[m]=0; AN(z)=AS(z)[0]=m; R z;}
814814
// c1=#characters to put out per line, lba=max # lines to put out
815-
c1=MIN(c,maxlen); lba=(D)lb+la;
815+
c1=MIN(c,maxlen);
816+
// obsolete lba=(D)lb+la;
816817
// calculate p=total # lines of spacing needed, as sum of (#k-cells-1) for k>=2
817-
p=2<r?2-r:0; h=1; DO(r-2, if(s[i]){h*=s[i]; p+=h;}else{p=0; break;});
818+
p=2<r?2-r:0; h=1; if(AN(w)==0)p=0; else{DO(r-2, if(s[i]){h*=s[i]; if(__builtin_add_overflow(p,h,&p)){p=IMAX-1; break;}}else{p=0; break;})} // h cannot overflow if AN!=0; but p can
818819
// Set h = max#lines to output, the smaller of (the # before spacing) and (the number we allow)
819-
h=lba<FLIMAX?lb+la:IMAX; h=MIN(nq,h);
820+
I lba; if(__builtin_add_overflow(lb,la,&lba))lba=IMAX; lba=MIN(lba,IMAX-1); h=MIN(nq,lba);
820821
// zn=# characters in result string. Start with enough for '...\n', plus '\n' for each line of spacing,
821822
// plus, for each line, the max length:
822823
// if character type, max line length + '\n' + room for '...\n' to continue the line till all characters are displayed
@@ -830,7 +831,7 @@ static A jtjprx(J jt,I ieol,I maxlen,I lb,I la,A w){A y,z;B ch;C e,eov[2],*v,x,*
830831
// Now we can allocate the result array. Set zu,zv->beginning of the data area
831832
GATV0(z,LIT,zn,1); zu=zv=CAV1(z);
832833
// h=# beginning lines to output. If all the lines, including spacing, fit in the user's limit, accept them all; otherwise use the user's starting number
833-
h=lba<nq+(q?p:0)?lb:IMAX;
834+
h=lba-(q?p:0)<nq?lb:IMAX-1;
834835
// Loop for each line of output. lc gives number of lines emitted so far, including ones called for by EOL inside character data
835836
for(i=lc=0;i<nq;++i){
836837
// Emit leading EOLs according to number of boundary crossings - only when we cross a 2-cell boundary

jsrc/j.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ static inline omp_int_t omp_get_num_threads() { return 1;}
381381
#define IMAX 9223372036854775807LL
382382
#define IMAXPRIME 9223372036854775783LL
383383
#define IMIN (~9223372036854775807LL) /* ANSI C LONG_MIN is -LONG_MAX */
384-
#define FLIMAX 9223372036854775296. // largest FL value that can be converted to I
385384
#define FLIMIN ((D)IMIN) // smallest FL value that can be converted to I
386385
#define FMTI "%lli"
387386
#define FMTI02 "%02lli"
@@ -400,7 +399,6 @@ static inline omp_int_t omp_get_num_threads() { return 1;}
400399
#define IMAX 2147483647L
401400
#define IMAXPRIME IMAX
402401
#define IMIN (~2147483647L) /* ANSI C LONG_MIN is -LONG_MAX */
403-
#define FLIMAX ((D)IMAX+0.4) // largest FL value that can be converted to I
404402
#define FLIMIN ((D)IMIN) // smallest FL value that can be converted to I
405403
#define FMTI "%d"
406404
#define FMTI02 "%02d"
@@ -410,6 +408,7 @@ static inline omp_int_t omp_get_num_threads() { return 1;}
410408
#define UIMAX -1ul
411409
#define strtoI strtol
412410
#endif
411+
#define FLIMAX (-(D)IMIN) // smallest FL value that cannot be converted to I
413412

414413
#define NEGATIVE0 (UIL)0x8000000000000000LL // IEEE -0 (double precision)
415414

jsrc/je.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ extern DF1(jtpinv);
284284
extern DF1(jtpix);
285285
extern F1(jtplt);
286286
extern F1(jtpmarea1);
287-
extern F1(jtpmctr);
287+
extern F1(jtpmlvl);
288288
extern F1(jtpmstats);
289289
extern F1(jtpmunpack);
290290
extern F1(jtpolar);

jsrc/jtype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ typedef struct{
11471147
I n; /* maximum number of records */
11481148
I i; /* index of next record to be written */
11491149
I s; /* initial bytesmax value */
1150-
I pmctr; // counter, set > 0 to start sampling
1150+
I pmlvl; // sampling level, set > 0 to start sampling
11511151
B rec; /* what to record (0 entry & exit; 1 all) */
11521152
B trunc; /* what to do on overflow (0 wrap; 1 truncate) */
11531153
B wrapped; /* 1 iff wrapping has happened */

jsrc/k.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,13 +1024,14 @@ A jtbcvt(J jtfg,C mode,A w){F12IP; A z=w;
10241024
RNE(z);
10251025
} /* convert to lowest type. 0=mode: don't convert XNUM/RAT to other types */
10261026

1027+
// w is a D type with integral values. If it can be converted to integer without overflow, do so and return the integer version, otherwise return w
10271028
F1(jticvt){F12IP;A z;D*v,x;I i,n,*u;
10281029
ARGCHK1(w);
10291030
n=AN(w); v=DAV(w);
10301031
I zr=AR(w); GATV(z,INT,n,AR(w),AS(w)); u=AVn(zr,z);
10311032
for(i=0;i<n;++i){
1032-
x=*v++; if(x<IMIN||FLIMAX<=x)R w; // if conversion will fail, skip it
1033-
*u++=(I)x;
1033+
x=v[i]; if(unlikely(x<FLIMIN||x>=FLIMAX))R w; // if conversion will fail, skip it
1034+
u[i]=(I)x;
10341035
}
10351036
R z;
10361037
}

jsrc/va1.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ INLINE static A jtssingleton1(J jtfg, A w,I caseno){F12JT;A z;void *zv;
3939
case SSINGCASE(VA1CMIN-VA1ORIGIN,SSINGENC(INT)): R w;
4040
case SSINGCASE(VA1CMIN-VA1ORIGIN,SSINGENC(FL)):
4141
{D x=wdv; wdv=jround(x); wdv-=TGT(wdv,x);} // do round/floor in parallel
42-
if(likely(wdv==(D)(I)wdv)) SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
42+
// obsolete if(likely(wdv==(D)(I)wdv)) SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
43+
if(likely(wdv>=FLIMIN&&wdv<FLIMAX))SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
4344
R z;
4445

4546

4647
case SSINGCASE(VA1CMAX-VA1ORIGIN,SSINGENC(B01)):
4748
case SSINGCASE(VA1CMAX-VA1ORIGIN,SSINGENC(INT)): R w;
4849
case SSINGCASE(VA1CMAX-VA1ORIGIN,SSINGENC(FL)):
4950
{D x=wdv; wdv=jround(x); wdv+=TLT(wdv,x);} // do round/ceil in parallel
50-
if(likely(wdv==(D)(I)wdv)) SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
51+
// obsolete if(likely(wdv==(D)(I)wdv)) SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
52+
if(likely(wdv>=FLIMIN&&wdv<FLIMAX))SSSTORE((I)wdv,z,INT,I) else SSSTORENVFL(wdv,z,FL,D)
5153
R z;
5254

5355

@@ -125,11 +127,11 @@ static AMONPS(floorDI,I,D,
125127
R rc?rc:EVOK;
126128
; ) // x100 0011 1100 =>2^61
127129
#else
128-
static AMON(floorDI,I,D, {D d=tfloor(*x); *z=(I)d; ASSERTWR(d==*z,EWOV);})
130+
static AMON(floorDI,I,D, { D d=tfloor(*x); ASSERTWR(d>=FLIMIN&&d<FLIMAX,EWOV) *z=(I)d;})
129131
#endif
130132
static AMON(floorD, D,D, *z=tfloor(*x);)
131133
static AMON(floorZ, Z,Z, *z=zfloor(*x);)
132-
static AMON(floorEI,I,E, {D d=tfloor(x->hi); *z=(I)d; ASSERTWR(d==*z,EWOV1);}) // tolerant floor of high part only...
134+
static AMON(floorEI,I,E, {D d=tfloor(x->hi); ASSERTWR(d>=FLIMIN&&d<FLIMAX,EWOV1) *z=(I)d;}) // tolerant floor of high part only...
133135
static AMON(floorE, E,E, *z=efloor(*x);) // ... if INT overflow, keep as extended to preserve precision
134136

135137
#if BW==64
@@ -142,9 +144,9 @@ static AMONPS(ceilDI,I,D,
142144
R rc?rc:EVOK;
143145
; ) // x100 0011 1100 =>2^61
144146
#else
145-
static AMON(ceilDI, I,D, {D d=tceil(*x); *z=(I)d; ASSERTWR(d==*z,EWOV);})
147+
static AMON(ceilDI, I,D, {D d=tceil(*x); ASSERTWR(d>=FLIMIN&&d<FLIMAX,EWOV) *z=(I)d;})
146148
#endif
147-
static AMON(ceilEI,I,E, {D d=tceil(x->hi); *z=(I)d; ASSERTWR(d==*z,EWOV1);})
149+
static AMON(ceilEI,I,E, {D d=tceil(x->hi); ASSERTWR(d>=FLIMIN&&d<FLIMAX,EWOV1) *z=(I)d;})
148150
static AMON(ceilD, D,D, *z=tceil(*x);)
149151
static AMON(ceilZ, Z,Z, *z=zceil(*x);)
150152
static AMON(ceilE, E,E, *z=eceil(*x);)

jsrc/vbang.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ AMONPS(factI, D,I, , *z=dgamma(1.0+(D)*x); , HDR1JERR)
7171
AMONPS(factD, D,D, , *z=_isnan(*x)?*x:dgamma(1.0+*x); , HDR1JERR)
7272
AMONPS(factZ, Z,Z, , *z=zgamma(zplus(z1,*x)); , HDR1JERR)
7373

74-
static D pq(D h,D m,D*c,D*d){D x=*c,y=*d;I n=(I)MIN(m,FLIMAX); // x and y cannot be 0
74+
// ??
75+
static D pq(D h,D m,D*c,D*d){D x=*c,y=*d;I n=(I)MIN(m,(D)IMAX); // x and y cannot be 0
7576
if(0>=m)R h;
7677
D xsgn=x<0?-1.0:1.0, ysgn=y<0?-1.0:1.0;
7778
if(ABS(x)!=ABS(y)){

jsrc/vi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ A jtindexofsub(J jtfg,I mode,A a,A w){F12JT;PROLOG(0079);A h=0,hi=mtv,z;B mk=w==
11421142
}else{B b=1.0==jt->cct;I t1;
11431143
AF fn=0; // we haven't figured it out yet
11441144
UI booladj = (mode&(IIOPMSK&~(IIDOT^IICO)))?5:0; // init table length not found; booladj = 5 if boolean hashvalue is OK, 0 if full index needed
1145-
p = (UI)MIN(FLIMAX,(2.1*MAX(m,c))); // length we will use for hashtable, if small-range not used.
1145+
p = (UI)MIN((D)IMAX,(2.1*MAX(m,c))); // length we will use for hashtable, if small-range not used.
11461146
if(!b&&t&BOX+FL+CMPX)ctmask(jt);
11471147
if (t&BOX) fn=b&&(1<n||usebs(a,ac,m))?jtiobs:1<n?jtioa:b?jtioax1:
11481148
(t1=utype(a,ac))&&(mk||a==w||TYPESEQ(t1,utype(w,wc)))?jtioau:jtioa1;

jsrc/vz.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ ZF1(jtzlog){ZF1DECL;
144144

145145
ZF2(jtzpow){ZF2DECL;D m;I n;
146146
if(!a&&!b){z.re=d?0:0>c?inf:!c; z.im=0; R z;}
147-
if(!d&&IMIN<c&&c<=FLIMAX&&(n=(I)jfloor(c),c==n)){
147+
if(!d&&FLIMIN<c&&c<FLIMAX&&(n=(I)jfloor(c),c==n)){
148+
// integral power, do repeated squaring
148149
if(0>n){u=zdiv(z1,u); n=-n;}
149150
z=z1;
150151
while(n){if(1&n)z=ztymes(z,u); u=ztymes(u,u); n>>=1;}

jsrc/x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void jtforeigninit(J jt){UI i;
251251
MN(6,9) XPRIM(VERB, jtqpctr, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
252252
MN(6,10) XPRIM(VERB, jtpmarea1, jtpmarea2, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
253253
MN(6,11) XPRIM(VERB, jtpmunpack, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
254-
MN(6,12) XPRIM(VERB, jtpmctr, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
254+
MN(6,12) XPRIM(VERB, jtpmlvl, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
255255
MN(6,13) XPRIM(VERB, jtpmstats, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
256256
MN(7,0) XPRIM(VERB, jtsp, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);
257257
MN(7,1) XPRIM(VERB, jtsphwmk, 0, VNONAME+VNOSELF,VF2NONE,RMAX,RMAX,RMAX);

0 commit comments

Comments
 (0)