Files
sx/examples/0148-types-int-numeric-limits.sx
agra d8076b9333 lang: rename signed integer types sN -> iN
Surface rename of the signed integer family: s1..s64 become i1..i64
(u1..u64, usize, isize unchanged). 'string' keeps the s-prefix arm in
name classification; width parsing moves to the i-prefix arm next to
isize.

Internal TypeId tags follow the surface (.s8/.s16/.s32/.s64 ->
.i8/.i16/.i32/.i64), as do mono-key mangle fragments (ptr_i64,
tu_i64_bool) and all display/diagnostic formatting (i{d}).

Migrated in the same sweep: stdlib + examples + issue repros + FFI C
companions (shared symbol names like ffi_id_i64), expected
stdout/stderr/ir snapshots, specs.md, readme.md, CLAUDE.md/AGENTS.md,
implementation_plan.md, docs/, issue writeups. Vendored stb_image and
historical flow state left untouched.

zig build test: 426/426; examples suite: 595/595.
2026-06-12 09:31:53 +03:00

66 lines
3.1 KiB
Plaintext

// Integer numeric-limit accessors: `<IntType>.min` / `.max` fold to a
// compile-time constant of the QUERIED integer type, driven by the
// (width, signedness) arithmetic (`sN`: min=-(2^(N-1)), max=2^(N-1)-1; `uN`:
// min=0, max=2^N-1) — every width 1..64, not just the power-of-two ones, plus
// `usize`/`isize` (target-width). Usable in expressions and in array-dimension
// position via the comptime-int path (`[u8.max]T`).
//
// The extreme values that the i64-based integer formatter cannot render
// directly — `i64.min` (i64::MIN) and the all-ones `u64.max`/`usize.max` — are
// asserted EXACTLY via comparison and untagged-union bit reinterpret, never via
// the formatter (which prints i64::MIN as a bare "-" and u64.max as "-1").
#import "modules/std.sx";
// Untagged union for the exact u64.max bit-reinterpret check.
UU :: union { u: u64; s: i64; }
main :: () -> i32 {
// Sub-byte widths — arbitrary bit-width arithmetic, not a per-name table.
print("i1.min={} i1.max={}\n", i1.min, i1.max); // -1 0
print("i2.min={} i2.max={}\n", i2.min, i2.max); // -2 1
print("i3.max={}\n", i3.max); // 3
print("u1.min={} u1.max={}\n", u1.min, u1.max); // 0 1
print("u2.max={}\n", u2.max); // 3
// Byte / word widths.
print("i8.min={} i8.max={}\n", i8.min, i8.max); // -128 127
print("u8.max={}\n", u8.max); // 255
print("i32.min={} i32.max={}\n", i32.min, i32.max); // -2147483648 2147483647
// i64 extremes: max prints; min (i64::MIN) is pinned by relation since the
// formatter cannot render it (this is independent of this feature).
print("i64.max={}\n", i64.max); // 9223372036854775807
print("i64.min+1 == -(i64.max): {}\n", i64.min + 1 == -9223372036854775807); // true
print("i64.min + i64.max == -1: {}\n", i64.min + i64.max == -1); // true
// u64.max / usize.max = all-ones (18446744073709551615); reinterpret to i64
// to confirm the bit pattern is -1 (and NOT a mangled value).
o : UU = ---;
o.u = u64.max;
print("u64.max as i64 == -1: {}\n", o.s == -1); // true
o.u = usize.max;
print("usize.max as i64 == -1: {}\n", o.s == -1); // true (host = u64)
print("usize.max == u64.max: {}\n", usize.max == u64.max); // true
print("isize.min == i64.min: {}\n", isize.min == i64.min); // true (host = i64)
// Result carries the QUERIED type: each binding is declared with the queried
// type and round-trips, so a mistyped fold (e.g. boxed as Any / widened)
// would not type-check here.
m3 : i3 = i3.max;
mu : u8 = u8.max;
ms : i8 = i8.min;
print("typed: m3={} mu={} ms={}\n", m3, mu, ms); // 3 255 -128
// Array-dimension / comptime-int path: `[u8.max]T` and `[i16.max]T` are
// valid counts (255 and 32767), usable end-to-end.
a : [u8.max]u8 = ---;
a[254] = 7;
print("[u8.max]u8 len={} a[254]={}\n", a.len, a[254]); // 255 7
b : [i16.max]u8 = ---;
b[32766] = 9;
print("[i16.max]u8 len={} b[32766]={}\n", b.len, b[32766]); // 32767 9
return 0;
}