110 lines
5.5 KiB
Plaintext
110 lines
5.5 KiB
Plaintext
;;;; Implicit widening, and the only thing worth pinning about it: the bits.
|
|
;;;; FIX.org 2026-09-20, "Implicit widening".
|
|
;;;;
|
|
;;;; A widening conversion is admitted exactly when it cannot change the
|
|
;;;; number, so every row here has an answer that is also the answer the source
|
|
;;;; type had. Which means the test is entirely about the *emitted* cast being
|
|
;;;; the right one: a sign-extension where the source is signed, a
|
|
;;;; zero-extension where it is not, and the float conversions picking sitofp
|
|
;;;; against uitofp by the same rule. Each of those is a separate instruction
|
|
;;;; on both backends and choosing the wrong one gives a wrong number rather
|
|
;;;; than a wrong type, which no type test would catch.
|
|
;;;;
|
|
;;;; The rows are picked so that a mistake is visible in the printed value:
|
|
;;;;
|
|
;;;; -5 i8 to i64 sext; a zext prints 251
|
|
;;;; -1 i32 to i64 sext; a zext prints 4294967295
|
|
;;;; 255 u8 to i16 zext; a sext prints -1
|
|
;;;; 4000000000 u32 zext to i64; a sext prints -294967296
|
|
;;;; 4294967295 u32 to f64, exact; a signed conversion prints -1
|
|
;;;; -2000000000 i32 to f64; a uitofp prints 2294967296
|
|
;;;;
|
|
;;;; Everything goes through a global rather than a literal, because a literal
|
|
;;;; is built at the wanted width by the literal rule and would never reach a
|
|
;;;; cast at all.
|
|
|
|
(defvar i8-neg i8 -5)
|
|
(defvar i8-pos i8 127)
|
|
(defvar i16-neg i16 -300)
|
|
(defvar i32-neg i32 -2000000000)
|
|
(defvar i32-one i32 1)
|
|
(defvar i32-all i32 -1)
|
|
(defvar u8-max u8 255)
|
|
(defvar u16-max u16 65535)
|
|
(defvar u32-big u32 4000000000)
|
|
(defvar u32-max u32 4294967295)
|
|
(defvar i64-big i64 5000000000)
|
|
(defvar f32-half f32 0.5)
|
|
|
|
;; Widening at a parameter. Each of these is a plain typed function and the
|
|
;; call sites below hand it a narrower type with no cast written anywhere.
|
|
(defn take-i64 [x i64] i64 x)
|
|
(defn take-i16 [x i16] i16 x)
|
|
(defn take-u64 [x u64] u64 x)
|
|
(defn take-f64 [x f64] f64 x)
|
|
(defn take-f32 [x f32] f32 x)
|
|
|
|
;; Widening at a return position: the body is an i32 and the signature is i64.
|
|
(defn ret-widened [] i64 i32-neg)
|
|
|
|
;; Widening in a binary operator, both orders. The first is the direction
|
|
;; [expect] already served; the second is the one the join rule added.
|
|
(defn add-wide-first [] i64 (+ i64-big i32-one))
|
|
(defn add-narrow-first [] i64 (+ i32-one i64-big))
|
|
|
|
(defn main [args [string]] i32
|
|
;; ── integer to integer ──────────────────────────────────────────
|
|
(println (take-i64 i8-neg)) ;; -5
|
|
(println (take-i64 i8-pos)) ;; 127
|
|
(println (take-i64 i16-neg)) ;; -300
|
|
(println (take-i64 i32-all)) ;; -1
|
|
(println (take-i64 i32-neg)) ;; -2000000000
|
|
(println (take-i16 u8-max)) ;; 255
|
|
(println (take-i64 u8-max)) ;; 255
|
|
(println (take-i64 u16-max)) ;; 65535
|
|
(println (take-i64 u32-big)) ;; 4000000000
|
|
(println (take-i64 u32-max)) ;; 4294967295
|
|
(println (take-u64 u32-big)) ;; 4000000000
|
|
(println (take-u64 u8-max)) ;; 255
|
|
|
|
;; ── a widened return ────────────────────────────────────────────
|
|
(println (ret-widened)) ;; -2000000000
|
|
|
|
;; ── integer to float, exact only ────────────────────────────────
|
|
(println (take-f64 i32-neg)) ;; -2000000000.0
|
|
(println (take-f64 u32-max)) ;; 4294967295.0
|
|
(println (take-f64 i8-neg)) ;; -5.0
|
|
(println (take-f32 i16-neg)) ;; -300.0
|
|
(println (take-f32 u16-max)) ;; 65535.0
|
|
|
|
;; The printer answers %g, which rounds an f64 long before the bits it is
|
|
;; carrying run out, so the exactness the int-to-float boundary is chosen for
|
|
;; is asserted by subtraction rather than by reading the digits. Each of
|
|
;; these is the difference between the widened value and the number it is
|
|
;; supposed to be, and a conversion that lost anything answers something
|
|
;; other than the last unit.
|
|
(println (- (take-f64 u32-max) 4294967294.0)) ;; 1
|
|
(println (- (take-f64 i32-neg) -1999999999.0)) ;; -1
|
|
(println (- (take-f32 u16-max) 65534.0)) ;; 1
|
|
|
|
;; ── float to float ──────────────────────────────────────────────
|
|
(println (take-f64 f32-half)) ;; 0.5
|
|
|
|
;; ── the binary join, both operand orders ────────────────────────
|
|
(println (add-wide-first)) ;; 5000000001
|
|
(println (add-narrow-first)) ;; 5000000001
|
|
;; The narrower operand is the first one, and the sum is an i64 even though
|
|
;; nothing on this line is annotated.
|
|
(println (+ i32-neg i64-big)) ;; 3000000000
|
|
;; A comparison joins the same way, and the widened -1 must still be -1.
|
|
(println (< i32-all i64-big)) ;; true
|
|
;; min and max over two widths answer at the wider one.
|
|
(println (max i8-neg i16-neg)) ;; -5
|
|
(println (min i8-neg i32-neg)) ;; -2000000000
|
|
;; A count narrower than the value widens to it; the value's width decides.
|
|
(println (<< i64-big i8-pos)) ;; 0 -- masked to 127 mod 64 = 63
|
|
;; An expectation reaches the operands, so this adds at i64 rather than
|
|
;; wrapping at i32 and widening the sum afterwards.
|
|
(println (take-i64 (+ i32-neg i32-neg))) ;; -4000000000
|
|
0)
|