shithub: femtolisp

Download patch

ref: f26ffca1b0f673eba1f773afcd9e3df268e8da7b
parent: 4ca4045c4d9956882813aba344b0b9430063e3e8
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Oct 28 22:25:41 EDT 2024

fix -nan.0 becoming +nan.0 on musl(?) systems

--- a/llt.c
+++ b/llt.c
@@ -1,5 +1,6 @@
 #include "llt.h"
 #include "random.h"
+#include "ieee754.h"
 
 double D_PNAN, D_NNAN, D_PINF, D_NINF;
 float F_PNAN, F_NNAN, F_PINF, F_NINF;
@@ -7,11 +8,15 @@
 void
 llt_init(void)
 {
-	D_PNAN = strtod("+NaN", nil);
-	D_NNAN = strtod("-NaN", nil);
-	D_PINF = strtod("+Inf", nil);
-	D_NINF = strtod("-Inf", nil);
+	D_PNAN = D_NNAN = strtod("+NaN", nil);
+	D_PINF = D_NINF = strtod("+Inf", nil);
 
+	union ieee754_double *d;
+	d = (union ieee754_double *)&D_NNAN;
+	d->ieee.negative = 1;
+	d = (union ieee754_double *)&D_NINF;
+	d->ieee.negative = 1;
+
 #ifdef __plan9__
 	*(uint32_t*)&F_PNAN = 0x7fc00000;
 	*(uint32_t*)&F_NNAN = 0xffc00000;
@@ -18,10 +23,13 @@
 	*(uint32_t*)&F_PINF = 0x7f800000;
 	*(uint32_t*)&F_NINF = 0xff800000;
 #else
-	F_PNAN = strtof("+NaN", nil);
-	F_NNAN = strtof("-NaN", nil);
-	F_PINF = strtof("+Inf", nil);
-	F_NINF = strtof("-Inf", nil);
+	union ieee754_float *f;
+	F_PNAN = F_NNAN = strtof("+NaN", nil);
+	F_PINF = F_NINF = strtof("+Inf", nil);
+	f = (union ieee754_float *)&F_NNAN;
+	f->ieee.negative = 1;
+	f = (union ieee754_float *)&F_NINF;
+	f->ieee.negative = 1;
 #endif
 
 	randomize();
--- a/test/unittest.lsp
+++ b/test/unittest.lsp
@@ -114,8 +114,10 @@
 
 ; NaNs
 (assert (equal? +nan.0 +nan.0))
+(assert (equal? -nan.0 -nan.0))
 (assert (not (= +nan.0 +nan.0)))
 (assert (not (= +nan.0 -nan.0)))
+(assert (not (= -nan.0 -nan.0)))
 (assert (equal? (< +nan.0 3) (> 3 +nan.0)))
 (assert (equal? (< +nan.0 (double 3)) (> (double 3) +nan.0)))
 (assert (equal? (< +nan.0 3) (> (double 3) +nan.0)))
@@ -125,6 +127,7 @@
 (assert (equal? (< 3 +nan.0) (> +nan.0 (double 3))))
 (assert (equal? (> 3 +nan.0) (> (double 3) +nan.0)))
 (assert (not (>= +nan.0 +nan.0)))
+(assert (not (<= -nan.0 -nan.0)))
 
 ; comparing strings
 (assert (< "a" "b"))