[Nickle] nickle: Branch 'master' - 4 commits

Keith Packard keithp at keithp.com
Thu Dec 5 22:23:08 PST 2024


 float.c             |  121 +++++++++++++++++++++++++++++++++-------------------
 test/math-tables.sh |    4 -
 2 files changed, 80 insertions(+), 45 deletions(-)

New commits:
commit 545e951fb907de286418b8e9d4490bc6da873994
Author: Keith Packard <keithp at keithp.com>
Date:   Thu Dec 5 22:22:41 2024 -0800

    test: Reduce the size of the math test vectors
    
    Bart is complaining about how long it takes to build these.
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/test/math-tables.sh b/test/math-tables.sh
index e252d41..7a4837a 100755
--- a/test/math-tables.sh
+++ b/test/math-tables.sh
@@ -51,8 +51,8 @@ if [ $# -gt 0 ]; then
     done
 fi
 
-inc=1
-ainc=1
+inc=11
+ainc=11
 
 # sine and cosine table
 
commit d1914834f4b8c2d76063252ee0918b98cfa578d5
Author: Keith Packard <keithp at keithp.com>
Date:   Thu Dec 5 22:19:19 2024 -0800

    float: Don't recompute exponent when rounding value in 'f' print
    
    We only need to recompute the exponent when rounding in 'e' mode; in
    'f' mode, we aren't printing the exponent.
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/float.c b/float.c
index 34ebfdb..2c1bb51 100644
--- a/float.c
+++ b/float.c
@@ -829,7 +829,9 @@ FloatPrint (Value f, Value fv, char format, int base, int width, int prec, int f
     m = NewInteger (Positive, a->mant->mag);
 
     m = Times (m, fratio);
-try_again:
+
+try_again_e:
+    DPrintF("scaled m", &m->floats);
     if (True (Less (m, One)))
     {
 	m = Times (m, NewInt (base));
@@ -897,7 +899,8 @@ try_again:
 	if (prec == INFINITE_OUTPUT_PRECISION)
 	    prec = mant_prec;
     }
-    
+
+try_again_f:
     int_part = Floor (m);
     frac_part = Minus (m, int_part);
     DPrintV("m int", int_part);
@@ -986,7 +989,10 @@ try_again:
 	    rounded = True;
 	    free (int_buffer);
 	    m = Plus(int_part, frac_part);
-	    goto try_again;
+	    if (format == 'e')
+		goto try_again_e;
+	    else
+		goto try_again_f;
 	}
     }
     frac_buffer = 0;
commit 7f0ee678111a67b21aa58c27e64640dc6ce479c3
Author: Keith Packard <keithp at keithp.com>
Date:   Thu Dec 5 22:19:01 2024 -0800

    Clean up trailing whitespace in float.c
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/float.c b/float.c
index 7a9696b..34ebfdb 100644
--- a/float.c
+++ b/float.c
@@ -115,7 +115,7 @@ FpartAdd (Fpart *a, Fpart *b, Bool negate)
 {
     ENTER ();
     Fpart   *ret;
-    
+
     switch (catagorize_signs(a->sign, negate ? SignNegate (b->sign):b->sign)) {
     default:
     case BothPositive:
@@ -163,7 +163,7 @@ FpartDivide (Fpart *a, Fpart *b)
     sign = Positive;
     if (a->sign != b->sign)
 	sign = Negative;
-    
+
     quo = NaturalDivide (a->mag, b->mag, &rem);
     RETURN (NewFpart (sign, quo));
 }
@@ -227,7 +227,7 @@ FpartLength (Fpart *a)
 
     if (a->mag->length == 0)
 	return 0;
-    
+
     bits = (a->mag->length - 1) * LBASE2;
     top = NaturalDigits(a->mag)[a->mag->length - 1];
     while (top)
@@ -311,7 +311,7 @@ FloatAdd (Value av, Value bv, int expandOk, Bool negate)
 	d = NaturalToInt (dist->mag);
 	if (dist->sign == Negative)
 	    d = -d;
-	
+
 	amant = a->mant;
 	bmant = b->mant;
 	alen = FpartLength (amant);
@@ -447,7 +447,7 @@ FloatLess (Value av, Value bv, int expandOk)
     DPrintF("less b", b);
     if (FpartEqual (a->mant, zero_fpart))
     {
-	if (b->mant->sign == Positive && 
+	if (b->mant->sign == Positive &&
 	    !FpartEqual (b->mant, zero_fpart))
 	    ret = TrueVal;
 	else
@@ -591,7 +591,7 @@ FloatCeil (Value av, int expandOk)
     exp = zero_fpart;
     RETURN (FloatInteger (NewFloat (mant, exp, a->prec - d)));
 }
-    
+
 static Value
 FloatPromote (Value av, Value bv)
 {
@@ -641,7 +641,7 @@ FloatReduce (Value av)
  *	1 <= 2^(exp2-1) / (base^(expbase-1)) < base
  *
  *	-log(base) <= (exp2-1) * log(2) - expbase * log(base) < 1
- *	
+ *
  *  ignoring the right inequality
  *
  *	0 <= (exp2 - 1) * log(2) - (expbase-1) * log(base)
@@ -659,7 +659,7 @@ NaturalBitSet (Natural *n, int i)
 {
     int	d = i / LBASE2;
     int	b = i & LBASE2;
-    
+
     return d < NaturalLength (n) && (NaturalDigits(n)[d] & 1 << b);
 }
 #endif
@@ -698,12 +698,12 @@ FloatExp (Value exp2, Value *ratio, int ibase, unsigned prec)
 	max = Div (min, two);
     else
 	max = Times (min, two);
-    
+
     /*
      * pow2 = 2 ** (exp2-1)
      */
     pow2 = Pow (two_f, Minus (exp2, One));
-    
+
     mean = 0;
     done = False;
     do
@@ -798,19 +798,16 @@ FloatPrint (Value f, Value fv, char format, int base, int width, int prec, int f
 	if (base <= 0) {
 	    base = 10;
 	}
-    
+
 	if (prec == DEFAULT_OUTPUT_PRECISION)
 	    prec = 15;
 	base_exp = base;
 	exp_scale = base;
     }
-    
+
     mant_prec = ceil(a->prec * log(2) / log(base)) + 1;
 
-    DebugFp ("mant", a->mant);
-    DebugFp ("exp ", a->exp);
-    
-    expbase = FloatExp (Plus (NewInt (length), 
+    expbase = FloatExp (Plus (NewInt (length),
 			      NewInteger (a->exp->sign,
 					  a->exp->mag)),
 			&ratio,
@@ -830,7 +827,7 @@ FloatPrint (Value f, Value fv, char format, int base, int width, int prec, int f
     DPrintF ("fratio ", &fratio->floats);
     negative = a->mant->sign == Negative;
     m = NewInteger (Positive, a->mant->mag);
-    
+
     m = Times (m, fratio);
 try_again:
     if (True (Less (m, One)))
@@ -868,7 +865,7 @@ try_again:
 	    format = 'f';
 	}
     }
-    
+
     if (format == 'f')
     {
 	m = Times (m, Pow (NewInt (base), expbase));
@@ -916,11 +913,11 @@ try_again:
 	int_width += 2;
     if (negative)
 	int_width++;
-    
+
     int_buffer = malloc (int_width + 1);
     int_string = NaturalSprint (int_buffer + int_width + 1,
 				int_n, base, &int_width);
-    
+
     if (aborting)
     {
 	EXIT ();
@@ -929,7 +926,7 @@ try_again:
     frac_prec = mant_prec - int_width;
     if (*int_string == '0')
 	frac_prec++;
-    
+
     if (leader)
     {
 	*--int_string = 'x';
@@ -941,7 +938,7 @@ try_again:
 	*--int_string = '-';
 	int_width++;
     }
-    
+
     if (width)
     {
 	if (width > 0)
@@ -965,7 +962,7 @@ try_again:
      */
     if (frac_width > frac_prec + 1)
 	frac_width = frac_prec + 1;
-    
+
     if (frac_width < 2)
 	frac_width = 0;
     /*
@@ -979,7 +976,7 @@ try_again:
 			       Pow (NewInt (base),
 				    NewInt (-frac_digits)));
 	frac_part = Plus (frac_part, round);
-			  
+
 	/*
 	 * If the fractional overflowed, bump the integer part
 	 * and try again
@@ -995,17 +992,17 @@ try_again:
     frac_buffer = 0;
     frac_string = 0;
     if (frac_width)
-	frac_part = Floor (Times (frac_part, Pow (NewInt (base), 
+	frac_part = Floor (Times (frac_part, Pow (NewInt (base),
 						  NewInt (frac_width - 1))));
     if (frac_width && (!Zerop (frac_part) || orig_prec > 0))
     {
 	int	frac_wrote;
-	
+
 	if (ValueIsInteger(frac_part))
 	    frac_n = IntegerMag(frac_part);
 	else
 	    frac_n = NewNatural (ValueInt(frac_part));
-	
+
 	frac_buffer = malloc (frac_width + 1);
 	frac_string = NaturalSprint (frac_buffer + frac_width + 1,
 				     frac_n, base, &frac_wrote);
@@ -1034,7 +1031,7 @@ try_again:
 	FileOutchar (f, fill);
 	width--;
     }
-    
+
     FilePuts (f, int_string);
     if (frac_string)
 	FilePuts (f, frac_string);
@@ -1054,7 +1051,7 @@ try_again:
     free (int_buffer);
     if (frac_buffer)
 	free (frac_buffer);
-    
+
     EXIT ();
     return True;
 }
@@ -1176,7 +1173,7 @@ NewRationalFloat (Rational *r, unsigned prec)
 {
     ENTER ();
     Value   num, den;
-    
+
     num = NewNaturalFloat (r->sign, r->num, prec);
     den = NewNaturalFloat (Positive, r->den, prec);
     RETURN (FloatDivide (num, den, 1));
@@ -1245,12 +1242,12 @@ DoublePart (Value av, char *error)
     int	    e;
     digit   *mt;
     double  div;
-    
+
     av = NewValueFloat (av, 64);
     if (!ValueIsFloat (av))
     {
 	RaiseStandardException (exception_invalid_argument, 3,
-				NewStrString (error), 
+				NewStrString (error),
 				NewInt (0), av);
 	return 0.0;
     }
@@ -1262,7 +1259,7 @@ DoublePart (Value av, char *error)
     {
 	if (av->floats.exp->sign == Negative)
 	    return 0.0;
-	
+
 	RaiseStandardException (exception_invalid_argument, 3,
 				NewStrString (error),
 				NewInt (0), av);
@@ -1270,7 +1267,7 @@ DoublePart (Value av, char *error)
     }
     if (av->floats.exp->sign == Negative)
 	e = -e;
-    
+
     mantissa = 0.0;
     i = av->floats.mant->mag->length;
     e += DIGITBITS * i;
commit d413c9ddd8db6b8e0e261c17acd5c447a7713db2
Author: Keith Packard <keithp at keithp.com>
Date:   Thu Dec 5 22:18:01 2024 -0800

    Split out float print debugging
    
    Make it possible to see the debug output while printing floats
    
    Signed-off-by: Keith Packard <keithp at keithp.com>

diff --git a/float.c b/float.c
index 8fd9055..7a9696b 100644
--- a/float.c
+++ b/float.c
@@ -28,6 +28,26 @@ Fpart	*zero_fpart, *one_fpart;
 #define DebugF(s,f)
 #endif
 
+#if 0
+#define DPrintV(s,v) FilePrintf (FileStdout, "%s %v\n", s, v)
+
+#define DPrintN(s,n) FilePrintf (FileStdout, "%s %n\n", s, n)
+
+#define DPrintFp(s,f) FilePrintf (FileStdout, "%s %s%n\n", s, \
+				 (f)->sign == Negative ? "-" : "", \
+				 (f)->mag)
+#define DPrintF(s,f) FilePrintf (FileStdout, "%s %s%n * 2**(%s%n)\n", s, \
+				 (f)->mant->sign == Negative ? "-" : "", \
+				 (f)->mant->mag,			\
+				 (f)->exp->sign == Negative ? "-" : "", \
+				 (f)->exp->mag)
+#else
+#define DPrintV(s,v)
+#define DPrintN(s,n)
+#define DPrintFp(s,f)
+#define DPrintF(s,f)
+#endif
+
 static void
 FpartMark (void *object)
 {
@@ -422,7 +442,9 @@ FloatLess (Value av, Value bv, int expandOk)
     ENTER ();
     Value	ret;
     Float	*a = &av->floats, *b = &bv->floats;
-    
+
+    DPrintF("less a", a);
+    DPrintF("less b", b);
     if (FpartEqual (a->mant, zero_fpart))
     {
 	if (b->mant->sign == Positive && 
@@ -447,10 +469,12 @@ FloatLess (Value av, Value bv, int expandOk)
     else
     {
 	av = FloatMinus (av, bv, expandOk);
+	DPrintF("less -", &av->floats);
 	ret = FalseVal;
 	if (av->floats.mant->sign == Negative)
 	    ret = TrueVal;
     }
+    DPrintV("less:", ret);
     RETURN (ret);
 }
 
@@ -757,7 +781,8 @@ FloatPrint (Value f, Value fv, char format, int base, int width, int prec, int f
     char	*exp_label = "e";
     Bool	leader = False;
     int		exp_scale;
-    
+
+    DPrintF("print", a);
     length = FpartLength (a->mant);
 
     if (format == 'a') {
@@ -796,13 +821,13 @@ FloatPrint (Value f, Value fv, char format, int base, int width, int prec, int f
 	EXIT ();
 	return False;
     }
-    DebugV ("expbase", expbase);
-    DebugF ("ratio  ", &ratio->floats);
+    DPrintV ("expbase", expbase);
+    DPrintF ("ratio  ", &ratio->floats);
     down = Pow (NewInt (2),
 		NewInt ((int) length));
-    DebugV ("down   ", down);
+    DPrintV ("down   ", down);
     fratio = Divide (ratio, down);
-    DebugF ("fratio ", &fratio->floats);
+    DPrintF ("fratio ", &fratio->floats);
     negative = a->mant->sign == Negative;
     m = NewInteger (Positive, a->mant->mag);
     
@@ -812,11 +837,15 @@ try_again:
     {
 	m = Times (m, NewInt (base));
 	expbase = Minus (expbase, One);
+	DPrintF("make m >= 1", &m->floats);
+	DPrintV("expbase", expbase);
     }
     else if (False (Less (m, NewInt (base))))
     {
 	m = Divide (m, NewInt (base));
 	expbase = Plus (expbase, One);
+	DPrintF("make m < base", &m->floats);
+	DPrintV("expbase", expbase);
     }
     exp = NewValueFpart (expbase);
     switch (format) {
@@ -843,6 +872,7 @@ try_again:
     if (format == 'f')
     {
 	m = Times (m, Pow (NewInt (base), expbase));
+	DPrintF("f format m", &m->floats);
 	exp_width = 0;
 	if (prec == INFINITE_OUTPUT_PRECISION)
 	{
@@ -873,6 +903,8 @@ try_again:
     
     int_part = Floor (m);
     frac_part = Minus (m, int_part);
+    DPrintV("m int", int_part);
+    DPrintF("m frac", &frac_part->floats);
 
     if (ValueIsInteger(int_part))
 	int_n = IntegerMag(int_part);


More information about the Nickle mailing list