Skip to content

Commit bb44fcc

Browse files
committed
[repatch] fix: moebius_ function (form-dev#422, form-dev#430)
* Build issue on 32-bit machines. * Corner cases where n ~ MAXPOSITIVE. Repatched 28e15ea (form-dev#441)
1 parent 2b32ea5 commit bb44fcc

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

sources/reken.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3739,8 +3739,8 @@ WORD Moebius(PHEAD WORD nn)
37393739
b: the number is not already in the table.
37403740
*/
37413741
if ( nn >= AR.moebiustablesize ) {
3742-
if ( AR.moebiustablesize <= 0 ) { newsize = nn + 20; }
3743-
else { newsize = nn*2; }
3742+
if ( AR.moebiustablesize <= 0 ) { newsize = (LONG)nn + 20; }
3743+
else { newsize = (LONG)nn*2; }
37443744
if ( newsize > MAXPOSITIVE ) newsize = MAXPOSITIVE;
37453745
newtable = (char *)Malloc1(newsize*sizeof(char),"Moebius");
37463746
for ( i = 0; i < AR.moebiustablesize; i++ ) newtable[i] = AR.moebiustable[i];
@@ -3749,7 +3749,8 @@ WORD Moebius(PHEAD WORD nn)
37493749
AR.moebiustable = newtable;
37503750
AR.moebiustablesize = newsize;
37513751
}
3752-
if ( AR.moebiustable[nn] != 2 ) return((WORD)AR.moebiustable[nn]);
3752+
/* NOTE: nn == MAXPOSITIVE never fits in moebiustable. */
3753+
if ( nn != MAXPOSITIVE && AR.moebiustable[nn] != 2 ) return((WORD)AR.moebiustable[nn]);
37533754
mu = 1;
37543755
if ( n == 1 ) goto putvalue;
37553756
if ( n % 2 == 0 ) {
@@ -3759,8 +3760,12 @@ WORD Moebius(PHEAD WORD nn)
37593760
mu = -mu;
37603761
if ( n == 1 ) goto putvalue;
37613762
}
3763+
#if ( BITSINWORD == 32 )
37623764
for ( i = 0; i < AR.numinprimelist; i++ ) {
37633765
x = AR.PrimeList[i];
3766+
#else
3767+
for ( x = 3; x < MAXPOSITIVE; x += 2 ) {
3768+
#endif
37643769
if ( n % x == 0 ) {
37653770
n /= x;
37663771
if ( n % x == 0 ) { mu = 0; goto putvalue; }
@@ -3772,7 +3777,7 @@ WORD Moebius(PHEAD WORD nn)
37723777
}
37733778
mu = -mu;
37743779
putvalue:
3775-
AR.moebiustable[nn] = mu;
3780+
if ( nn != MAXPOSITIVE ) AR.moebiustable[nn] = mu;
37763781
return((WORD)mu);
37773782
}
37783783

0 commit comments

Comments
 (0)