ref: d0505fae65fce8e65074561301f2257450c22339
parent: c3f004633be7e8a15fe8e1e158dde24c2af2f765
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Nov 17 19:40:16 EST 2024
libmp: if somebody decides to redefine mpdigit as uint64, this was the fix to make it work
--- a/3rd/mp/mpdigdiv.c
+++ b/3rd/mp/mpdigdiv.c
@@ -47,7 +47,7 @@
hi--;
lo -= y;
hi -= x;
- q |= 1U<<i;
+ q |= (mpdigit)1<<i;
}
q += lo/divisor;
*quotient = q;
--- a/3rd/mp/mptoi.c
+++ b/3rd/mp/mptoi.c
@@ -21,19 +21,19 @@
int
mptoi(mpint *b)
{
- uint32_t x;
+ mpdigit x;
if(b->top==0)
return 0;
x = *b->p;
if(b->sign > 0){
- if(b->top > 1 || (x > MAXINT))
- x = (int)MAXINT;
+ if(b->top > 1 || x > INT32_MAX)
+ x = (int)INT32_MAX;
else
x = (int)x;
} else {
- if(b->top > 1 || x > MAXINT+1)
- x = (int)MININT;
+ if(b->top > 1 || x > (mpdigit)INT32_MAX+1)
+ x = (int)INT32_MIN;
else
x = -(int)x;
}
--- a/3rd/mp/mptoui.c
+++ b/3rd/mp/mptoui.c
@@ -8,9 +8,9 @@
mpint*
uitomp(uint32_t i, mpint *b)
{
- if(b == nil){
+ if(b == nil)
b = mpnew(0);
- }
+
*b->p = i;
b->top = 1;
b->sign = 1;
@@ -20,12 +20,12 @@
uint32_t
mptoui(mpint *b)
{
- uint32_t x;
+ mpdigit x;
x = *b->p;
if(b->sign < 0)
x = 0;
- else if(b->top > 1 || (sizeof(mpdigit) > sizeof(uint32_t) && x > MAXUINT))
- x = MAXUINT;
+ else if(b->top > 1 || (Dbytes > sizeof(uint32_t) && x > UINT32_MAX))
+ x = UINT32_MAX;
return x;
}
--- a/3rd/mp/mptouv.c
+++ b/3rd/mp/mptouv.c
@@ -1,6 +1,6 @@
#include "platform.h"
-#define VLDIGITS (int)(sizeof(int64_t)/sizeof(mpdigit))
+#define VLDIGITS (int)(sizeof(int64_t)/Dbytes)
/*
* this code assumes that a int64_t is an integral number of
@@ -11,14 +11,14 @@
{
int s;
- if(b == nil){
+ if(b == nil)
b = mpnew(VLDIGITS*Dbits);
- }else
+ else
mpbits(b, VLDIGITS*Dbits);
b->sign = 1;
for(s = 0; s < VLDIGITS; s++){
b->p[s] = v;
- v >>= sizeof(mpdigit)*8;
+ v >>= Dbits;
}
b->top = s;
return mpnorm(b);
@@ -38,7 +38,7 @@
v = 0ULL;
for(s = 0; s < b->top; s++)
- v |= (uint64_t)b->p[s]<<(s*sizeof(mpdigit)*8);
+ v |= (uint64_t)b->p[s]<<(s*Dbits);
return v;
}
--- a/3rd/mp/mptov.c
+++ b/3rd/mp/mptov.c
@@ -1,6 +1,6 @@
#include "platform.h"
-#define VLDIGITS (int)(sizeof(int64_t)/sizeof(mpdigit))
+#define VLDIGITS (int)(sizeof(int64_t)/Dbytes)
/*
* this code assumes that a int64_t is an integral number of
@@ -20,7 +20,7 @@
uv = v * b->sign;
for(s = 0; s < VLDIGITS; s++){
b->p[s] = uv;
- uv >>= sizeof(mpdigit)*8;
+ uv >>= Dbits;
}
b->top = s;
return mpnorm(b);
@@ -35,25 +35,18 @@
if(b->top == 0)
return 0LL;
- if(b->top > VLDIGITS){
- if(b->sign > 0)
- return (int64_t)MAXVLONG;
- else
- return (int64_t)MINVLONG;
- }
+ if(b->top > VLDIGITS)
+ return b->sign > 0 ? INT64_MAX : INT64_MIN;
v = 0ULL;
for(s = 0; s < b->top; s++)
- v |= (uint64_t)b->p[s]<<(s*sizeof(mpdigit)*8);
+ v |= (uint64_t)b->p[s]<<(s*Dbits);
if(b->sign > 0){
- if(v > MAXVLONG)
- v = MAXVLONG;
- } else {
- if(v > MINVLONG)
- v = MINVLONG;
- else
- v = -(int64_t)v;
+ if(v > INT64_MAX)
+ v = INT64_MAX;
+ }else{
+ v = v > (uint64_t)INT64_MIN ? INT64_MIN : -(int64_t)v;
}
return (int64_t)v;
--- a/3rd/mp/mpvecdigmuladd.c
+++ b/3rd/mp/mpvecdigmuladd.c
@@ -1,6 +1,6 @@
#include "platform.h"
-#define LO(x) ((x) & ((1<<(Dbits/2))-1))
+#define LO(x) ((x) & ((((mpdigit)1)<<(Dbits/2))-1))
#define HI(x) ((x) >> (Dbits/2))
static void
--- a/3rd/mp/strtomp.c
+++ b/3rd/mp/strtomp.c
@@ -1,6 +1,6 @@
#include "platform.h"
-static const char*
+static const char *
frompow2(const char *a, mpint *b, int s)
{
const char *p, *next;
--- a/3rd/mp/test.c
+++ b/3rd/mp/test.c
@@ -162,7 +162,7 @@
if(loops > 1)
printf("%"PRId64" ns for a %d*%d vecdigmul\n", (nsec()-now)/loops, b->top*Dbits, Dbits);
mpnorm(b2);
- printf("0 + %s * %x = %s\n", MP(b), d, MP(b2));
+ printf("0 + %s * %"PRIx64" = %s\n", MP(b), (uint64_t)d, MP(b2));
mpfree(b);
mpfree(b2);
@@ -187,7 +187,7 @@
if(loops > 1)
printf("%"PRId64" ns for a %d*%d vecdigmul\n", (nsec()-now)/loops, b->top*Dbits, Dbits);
mpnorm(b2);
- printf("0 - %s * %x = %s\n", MP(b), d, MP(b2));
+ printf("0 - %s * %"PRIx64" = %s\n", MP(b), (uint64_t)d, MP(b2));
mpfree(b);
mpfree(b2);
@@ -252,7 +252,7 @@
mpdigdiv(b->p, d, &q);
if(loops > 1)
printf("%"PRId64" ns for a %d / %d div\n", (nsec()-now)/loops, 2*Dbits, Dbits);
- printf("%s / %x = %x\n", MP(b), d, q);
+ printf("%s / %"PRIx64" = %"PRIx64"\n", MP(b), (uint64_t)d, (uint64_t)q);
mpfree(b);
}
--- a/3rd/mp/u16.c
+++ b/3rd/mp/u16.c
@@ -12,7 +12,7 @@
return c;
}
-uint32_t
+mpdigit
dec16chr(int c)
{
int o;
--- a/posix/mp.h
+++ b/posix/mp.h
@@ -17,22 +17,12 @@
};
};
-#define mpdighi (mpdigit)(1U<<(Dbits-1))
-#define DIGITS(x) ((x) >= -(Dbits-1) ? ((Dbits - 1 + (x))/Dbits) : 0)
+#define mpdighi (((mpdigit)1)<<(Dbits-1))
+#define DIGITS(x) ((int)(x) >= -(Dbits-1) ? ((Dbits - 1 + (x))/Dbits) : 0)
-// for converting between int's and mpint's
-#define MAXUINT ((uint32_t)-1)
-#define MAXINT (MAXUINT>>1)
-#define MININT (MAXINT+1)
-
-// for converting between vlongs's and mpint's
-#define MAXUVLONG (~0ULL)
-#define MAXVLONG (MAXUVLONG>>1)
-#define MINVLONG (MAXVLONG+1ULL)
-
extern int dec16(uint8_t*, int, char*, int);
extern int enc16(char*, int, uint8_t*, int);
-extern uint32_t dec16chr(int);
+extern mpdigit dec16chr(int);
extern int enc16chr(int);
/*