[Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
Putting this here so it doesn't get lost. Evan >From 8e226395197515fe5aac0ac6e0bf3a17a7584394 Mon Sep 17 00:00:00 2001 From: Evan Hanson Date: Thu, 27 Jun 2019 16:34:36 +1200 Subject: [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes The unboxing pass added in 79cf7427 introduces a `##core#float' type to the closure-converted language, for which floating point literals are emitted inline. This also needs to handle NaN and infinite values. For these, we use the NAN and INFINITY constants defined by C99. Fixes #1626. --- c-backend.scm | 7 ++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/c-backend.scm b/c-backend.scm index 2ef2337f..a7783379 100644 --- a/c-backend.scm +++ b/c-backend.scm @@ -127,7 +127,12 @@ (gen "lf[" (first params) #\])) ) ) ((##core#float) - (gen (first params))) +(let ((n (first params))) + (cond ((nan? n) (gen "NAN")) +((infinite? n) + (when (negative? n) (gen #\-)) + (gen "INFINITY")) +(else (gen n) ((if) (gen #t "if(C_truep(") -- 2.21.0 ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
Don't forget to cast them to double, however. To get a negative zero you need an initialized global variable: double negative_zero = 1.0 / -INFINITY; and then negative_zero is the Right Thing. On Thu, Jun 27, 2019 at 5:20 PM Evan Hanson wrote: > Putting this here so it doesn't get lost. > > Evan > ___ > Chicken-hackers mailing list > Chicken-hackers@nongnu.org > https://lists.nongnu.org/mailman/listinfo/chicken-hackers > ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
Hi John, On 2019-06-27 18:28, John Cowan wrote: > Don't forget to cast them to double, however. Good point. Updated version attached. > To get a negative zero you need an initialized global variable: I'm not sure we need a negative zero here, do we? Evan >From 40ee7d67559a49515f2a8f6738f43139ac62337c Mon Sep 17 00:00:00 2001 From: Evan Hanson Date: Thu, 27 Jun 2019 16:34:36 +1200 Subject: [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes The unboxing pass added in 79cf7427 introduces a `##core#float' type to the closure-converted language, for which floating point literals are emitted inline. This also needs to handle NaN and infinite values. For these, we use the NAN and INFINITY constants defined by C99. We also adds a cast to "double" for all inline flonums. Fixes #1626. --- c-backend.scm | 8 +++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/c-backend.scm b/c-backend.scm index 2ef2337f..10134fbc 100644 --- a/c-backend.scm +++ b/c-backend.scm @@ -127,7 +127,13 @@ (gen "lf[" (first params) #\])) ) ) ((##core#float) - (gen (first params))) +(let ((n (first params))) + (gen "(double)") + (cond ((nan? n) (gen "NAN")) +((infinite? n) + (when (negative? n) (gen #\-)) + (gen "INFINITY")) +(else (gen n) ((if) (gen #t "if(C_truep(") -- 2.21.0 ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
No, you do. If a Schemer writes (/ 1.0 -0.0) the result must be -inf.0, not +inf.0. But that fact leads to a better way to compile a negative zero into C: (1.0 / (-INFINITY)). No unions needed. On Thu, Jun 27, 2019 at 6:50 PM Evan Hanson wrote: > Hi John, > > On 2019-06-27 18:28, John Cowan wrote: > > Don't forget to cast them to double, however. > > Good point. Updated version attached. > > > To get a negative zero you need an initialized global variable: > > I'm not sure we need a negative zero here, do we? > > Evan > ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
On 2019-06-27 21:27, John Cowan wrote: > No, you do. If a Schemer writes (/ 1.0 -0.0) the result must be > -inf.0, not +inf.0. OK, but I think that's a separate issue, because currently if a CHICKEN user writes (/ 1.0 -0.0) they get +inf.0. And -0.0 gets them 0.0. It sounds like this might be worth a bug report (would you be so kind?). But, for the purposes of this patch, there is no negative zero. Cheers, Evan ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
Done; < https://bugs.call-cc.org/ticket/1627#ticket >. On Thu, Jun 27, 2019 at 10:26 PM Evan Hanson wrote: > On 2019-06-27 21:27, John Cowan wrote: > > No, you do. If a Schemer writes (/ 1.0 -0.0) the result must be > > -inf.0, not +inf.0. > > OK, but I think that's a separate issue, because currently if a CHICKEN > user writes (/ 1.0 -0.0) they get +inf.0. And -0.0 gets them 0.0. > > It sounds like this might be worth a bug report (would you be so kind?). > But, for the purposes of this patch, there is no negative zero. > > Cheers, > > Evan > ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers
Re: [Chicken-hackers] [PATCH] Emit C99 constants for +nan.0 and [+-]inf.0 `##core#float' nodes
On Fri, Jun 28, 2019 at 10:50:51AM +1200, Evan Hanson wrote: > Hi John, > > On 2019-06-27 18:28, John Cowan wrote: > > Don't forget to cast them to double, however. > > Good point. Updated version attached. Thanks, pushed! Regarding negative zero, see my other patch. Cheers, Peter signature.asc Description: PGP signature ___ Chicken-hackers mailing list Chicken-hackers@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-hackers