spell the least long long as C's LLONG_MIN

-9223372036854775808 reached C as written, where it negates an unsigned
constant, and had no type for inference: it is read as
-9223372036854775807 - 1, which is long long on both sides.
This commit is contained in:
2026-10-27 22:41:06 +03:00
parent 28aaaf637c
commit a2bbb73524
3 changed files with 12 additions and 5 deletions

View File

@@ -234,7 +234,9 @@
;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400, ;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400,
;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide ;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide
;;; for long long is unsigned to C, which says so only by a warning ;;; for long long is unsigned to C, which says so only by a warning
;;; unless it is written with a `u'; so it is. ;;; unless it is written with a `u'; so it is. -9223372036854775808 is
;;; one too: C negates the unsigned 9223372036854775808, so it is
;;; spelled as LLONG_MIN is, -9223372036854775807 - 1.
(define +llong-min+ (- (expt 2 63))) (define +llong-min+ (- (expt 2 63)))
(define +llong-max+ (- (expt 2 63) 1)) (define +llong-max+ (- (expt 2 63) 1))
(define +ullong-max+ (- (expt 2 64) 1)) (define +ullong-max+ (- (expt 2 64) 1))
@@ -245,6 +247,7 @@
(error "too large for any C integer type" s)) (error "too large for any C integer type" s))
((> n +llong-max+) (make-number-literal n "u" #f)) ((> n +llong-max+) (make-number-literal n "u" #f))
((< n +llong-min+) (list '- (make-number-literal (- n) "u" #f))) ((< n +llong-min+) (list '- (make-number-literal (- n) "u" #f)))
((= n +llong-min+) (list '- (+ n 1) 1))
(else n))) (else n)))
((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))))

View File

@@ -1190,7 +1190,10 @@
(emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;")) (emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;"))
(test-assert "a number too wide for long long is unsigned, and says so" (test-assert "a number too wide for long long is unsigned, and says so"
(emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)") (emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)")
"n = 18446744073709551615u;"))) "n = 18446744073709551615u;"))
(test-assert "the least long long is spelled as LLONG_MIN, and typed"
(emits? (in-fn "(var n _ -9223372036854775808)")
"long long n = -9223372036854775807 - 1;")))
;; A define's body was pasted unwalked, and unparenthesised: `cast' ;; A define's body was pasted unwalked, and unparenthesised: `cast'
;; stayed a call, and (* x D) regrouped with D's own operator ;; stayed a call, and (* x D) regrouped with D's own operator

View File

@@ -1,6 +1,6 @@
(compilation "-- -std=c11 -pedantic-errors") (compilation "-- -std=c11 -pedantic-errors")
(input) (input)
(output "c11: 42 43 6 4 6 12 5 0" (output "c11: 42 43 6 4 6 12 5 0 -9223372036854775808"
"trigraphs: what??! ??= ???( ??/") "trigraphs: what??! ??= ???( ??/")
(return 0) (return 0)
@@ -33,6 +33,7 @@
(var origin point #()) (var origin point #())
(var unit _ (shape/square 1)) (var unit _ (shape/square 1))
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4))) (var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
(var least _ -9223372036854775808)
(fn sides ((s shape)) int (fn sides ((s shape)) int
(var n int 0) (var n int 0)
@@ -46,8 +47,8 @@
(var k int 1) (var k int 1)
(var add-k (closure ((int)) int) (var add-k (closure ((int)) int)
(closure ((v int)) int (k) (return (+ v k)))) (closure ((v int)) int (k) (return (+ v k))))
(printf "c11: %d %d %d %d %d %d %d %d\n" (printf "c11: %d %d %d %d %d %d %d %d %lld\n"
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2) (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
(sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y)) (sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y) least)
(printf "trigraphs: %s\n" "what??! ??= ???( ??/") (printf "trigraphs: %s\n" "what??! ??= ???( ??/")
(return 0)) (return 0))