read hex floats

0x1.8p3 was no number to the reader, so it became a symbol C got inside
|pipes|, and _ could not type it. It reads now as a literal written as
it was: the digits of a double's own bits, which no decimal rounds to.
This commit is contained in:
2026-10-16 14:44:29 +03:00
parent c72b19035d
commit 3abb39854b
5 changed files with 55 additions and 16 deletions

View File

@@ -198,7 +198,7 @@
((string=? s ".") dot-token) ((string=? s ".") dot-token)
((or (string->number digits) (radix-number digits)) ((or (string->number digits) (radix-number digits))
=> (lambda (n) (c-number n s))) => (lambda (n) (c-number n s)))
((suffixed-number digits s) => identity) ((spelled-number digits s) => identity)
((negated-name? s) (list '- (string->symbol (substring s 1)))) ((negated-name? s) (list '- (string->symbol (substring s 1))))
(else (string->symbol s))))) (else (string->symbol s)))))
@@ -232,8 +232,8 @@
((and (real? n) (inexact? n) (finite? n)) n) ((and (real? n) (inexact? n) (finite? n)) n)
(else (error "C has no literal for this number" s)))) (else (error "C has no literal for this number" s))))
;;; 1u, 10L, 1ULL, 1.5f, 0.1L: a number with C's suffix, which the C is ;;; 1u, 10L, 1ULL, 1.5f, 0.1L, 0x1.8p3: a number with C's suffix, or a
;;; written with. ;;; hex float, which the C is written with.
;;; ;;;
;;; 0b101u -> 5u in decimal, so C23's 0b never reaches C11 ;;; 0b101u -> 5u in decimal, so C23's 0b never reaches C11
;;; 0.1f -> 0.1f in the digits written: through a double, 0.1f ;;; 0.1f -> 0.1f in the digits written: through a double, 0.1f
@@ -260,7 +260,10 @@
(define suffixed-float (define suffixed-float
(irregex "((?:[0-9]+\\.[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)([fFlL])")) (irregex "((?:[0-9]+\\.[0-9]*|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)([fFlL])"))
(define (suffixed-number digits s) (define hex-float
(irregex "(0[xX]([0-9a-fA-F]*)\\.?([0-9a-fA-F]*)[pP]([+-]?[0-9]+))([fFlL]?)"))
(define (spelled-number digits s)
(let* ((negative? (and (> (string-length digits) 1) (char=? #\- (string-ref digits 0)))) (let* ((negative? (and (> (string-length digits) 1) (char=? #\- (string-ref digits 0))))
(body (if negative? (substring digits 1) digits)) (body (if negative? (substring digits 1) digits))
;; each spelling begins with a digit or a point, and few names do ;; each spelling begins with a digit or a point, and few names do
@@ -268,7 +271,8 @@
(or (char-numeric? (string-ref body 0)) (or (char-numeric? (string-ref body 0))
(char=? #\. (string-ref body 0))) (char=? #\. (string-ref body 0)))
(or (suffixed-integer-literal body s) (or (suffixed-integer-literal body s)
(suffixed-float-literal body s))))) (suffixed-float-literal body s)
(hex-float-literal body s)))))
(and literal (if negative? (list '- literal) literal)))) (and literal (if negative? (list '- literal) literal))))
(define (suffixed-integer-literal body s) (define (suffixed-integer-literal body s)
@@ -282,22 +286,41 @@
(error "too large for its suffix" s)) (error "too large for its suffix" s))
(make-c-literal n suffix #f))))) (make-c-literal n suffix #f)))))
;;; 0x1.8p3 is 0x18 sixteenths times 2^3
(define (hex-float-literal body s)
(let ((m (irregex-match hex-float body)))
(and m
(let ((whole (irregex-match-substring m 2))
(fraction (irregex-match-substring m 3))
(exponent (string->number (irregex-match-substring m 4)))
(suffix (irregex-match-substring m 5)))
(and (> (+ (string-length whole) (string-length fraction)) 0)
(float-literal
(if (> (abs exponent) 20000) ; past any C type
(if (> exponent 0) +inf.0 0.)
(exact->inexact
(* (string->number (string-append whole fraction) 16)
(expt 2 (- exponent (* 4 (string-length fraction)))))))
suffix (irregex-match-substring m 1) s))))))
(define (suffixed-float-literal body s) (define (suffixed-float-literal body s)
(let ((m (irregex-match suffixed-float body))) (let ((m (irregex-match suffixed-float body)))
(and m (and m
(let* ((digits (irregex-match-substring m 1)) (let ((digits (irregex-match-substring m 1)))
(suffix (irregex-match-substring m 2)) (float-literal (string->number digits) (irregex-match-substring m 2)
(n (string->number digits))) digits s)))))
(unless (in-range? n suffix)
(error "C has no literal for this number" s)) (define (float-literal n suffix digits s)
(make-c-literal n suffix digits))))) (unless (in-range? n suffix)
(error "C has no literal for this number" s))
(make-c-literal n suffix digits))
;;; A float overflows where it would round past FLT_MAX; a long double ;;; A float overflows where it would round past FLT_MAX; a long double
;;; may be wider than any double, so whatever a double holds it does too ;;; may be wider than any double, so whatever a double holds it does too
(define (in-range? n suffix) (define (in-range? n suffix)
(case (string-ref suffix 0) (cond ((string-ci=? suffix "f") (< (abs n) (* (- 2 (expt 2. -24)) (expt 2. 127))))
((#\f #\F) (< (abs n) (* (- 2 (expt 2. -24)) (expt 2. 127)))) ((string-ci=? suffix "l") #t)
(else #t))) (else (finite? n))))
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those ;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
;;; numbers: the C written from them says 224 and 5 ;;; numbers: the C written from them says 224 and 5

View File

@@ -1225,12 +1225,14 @@
;;; 1 -> int 1u -> (unsigned int) ;;; 1 -> int 1u -> (unsigned int)
;;; 4000000000 -> (long long) 10L -> long ;;; 4000000000 -> (long long) 10L -> long
;;; 1.5f -> float 0.1L -> (long double) ;;; 1.5f -> float 0.1L -> (long double)
;;; 0x1.8p3 -> double
(define (c-literal-type literal) (define (c-literal-type literal)
(let ((n (c-literal-value literal)) (let ((n (c-literal-value literal))
(suffix (map char-downcase (string->list (c-literal-suffix literal))))) (suffix (map char-downcase (string->list (c-literal-suffix literal)))))
(cond ((exact? n) (integer-literal-type n suffix)) (cond ((exact? n) (integer-literal-type n suffix))
((memv #\f suffix) 'float) ((memv #\f suffix) 'float)
(else '(long double))))) ((memv #\l suffix) '(long double))
(else 'double))))
(define (integer-literal-type n suffix) (define (integer-literal-type n suffix)
(let ((ranks (drop (if (memv #\u suffix) (let ((ranks (drop (if (memv #\u suffix)

View File

@@ -827,6 +827,10 @@ whitespace aside, THEN?"
(test-assert "a float keeps its digits, which a double would round" (test-assert "a float keeps its digits, which a double would round"
(emits? (in-fn "(var x (long double) 3.14159265358979323846264338327950288L)") (emits? (in-fn "(var x (long double) 3.14159265358979323846264338327950288L)")
"x = 3.14159265358979323846264338327950288L;")) "x = 3.14159265358979323846264338327950288L;"))
(test-assert "a hex float is written as it was"
(emits? (in-fn "(var x double 0x1.8p3)") "x = 0x1.8p3;"))
(test-assert "suffix and all"
(emits? (in-fn "(var x float -0x1p-2f)") "x = -0x1p-2f;"))
(test-assert "grouped digits reach C ungrouped" (test-assert "grouped digits reach C ungrouped"
(emits? (in-fn "(var x float 1_000.5f)") "x = 1000.5f;")) (emits? (in-fn "(var x float 1_000.5f)") "x = 1000.5f;"))
(test-assert "a minus is C's own" (test-assert "a minus is C's own"

View File

@@ -76,6 +76,10 @@
(test "nor a float" "C has no literal for this number" (read-error "1e39f")) (test "nor a float" "C has no literal for this number" (read-error "1e39f"))
(test "but a long double may be wider than a double" (test "but a long double may be wider than a double"
#f (read-error "1e400L")) #f (read-error "1e400L"))
(test "a hex float is held to the same ranges"
"C has no literal for this number" (read-error "0x1p1024"))
(test "a float's too" "C has no literal for this number" (read-error "0x1p128f"))
(test "and a long double's" #f (read-error "0x1p1024L"))
(test "nor an infinity" "C has no literal for this number" (read-error "-inf.0")) (test "nor an infinity" "C has no literal for this number" (read-error "-inf.0"))
(test "nor a NaN" "C has no literal for this number" (read-error "+nan.0")) (test "nor a NaN" "C has no literal for this number" (read-error "+nan.0"))
(test "nor a complex number" "C has no literal for this number" (read-error "1+2i")) (test "nor a complex number" "C has no literal for this number" (read-error "1+2i"))

View File

@@ -4,7 +4,8 @@
"widths: 1 1 1" "widths: 1 1 1"
"a float stays a float: 1" "a float stays a float: 1"
"radix with suffix: 255 5" "radix with suffix: 255 5"
"by magnitude: 4000000000 18446744073709551615") "by magnitude: 4000000000 18446744073709551615"
"hex floats: 12 0.25 1")
(return 0) (return 0)
;;; A number is written the way C writes it, suffix included, and `_' ;;; A number is written the way C writes it, suffix included, and `_'
@@ -36,4 +37,9 @@
(var big _ 4000000000) (var big _ 4000000000)
(var mask _ 0xFFFFFFFFFFFFFFFF) (var mask _ 0xFFFFFFFFFFFFFFFF)
(printf "by magnitude: %lld %llu\n" big mask) (printf "by magnitude: %lld %llu\n" big mask)
;; a double's own digits, which no decimal has to round to
(var twelve _ 0x1.8p3)
(var quarter _ 0x1p-2f)
(printf "hex floats: %g %g %d\n" twelve quarter (== (sizeof quarter) (sizeof float)))
(return 0)) (return 0))