fix(std): render integer formatter extremes — i64::MIN and unsigned all-ones [F0.8]
Resolves issue 0090. The `{}` integer formatter mis-rendered both ends of
the 64-bit range:
- `int_to_string` computed the magnitude as `0 - n`, which overflows for
`s64::MIN` (its magnitude is unrepresentable as a positive s64) — the
value stayed negative, the digit loop ran zero times, so only `-`
printed. It now extracts digits straight from `n` (per-digit
`|n % 10|`, `n` truncating toward zero), never negating MIN.
- `any_to_string`'s `case int:` formatted every integer as s64, so a u64
all-ones value printed as `-1`. There was no `uint` type-category to
distinguish signedness. Added an additive `type_is_unsigned(T)`
reflection builtin (static fold + dynamic interp/LLVM paths, mirroring
`type_name`), backed by the new `TypeTable.isUnsignedInt` predicate, and
a `uint_to_string` formatter (unsigned decimal via long-division over
four 16-bit limbs). `case int:` routes through `type_is_unsigned(type)`.
The 16-bit-limb split is factored into a shared `decompose_u16x4`, now
reused by `int_to_hex_string` (no second unsigned-math routine).
Regression: examples/0046-basic-int-formatter-extremes pins both extremes
plus a width spread; unit tests cover `isUnsignedInt`. Docs (specs.md
representation note, readme std API) updated for unsigned/extreme `{}`
behavior. IR snapshots refreshed for the two new std functions.
This commit is contained in:
@@ -1064,6 +1064,27 @@ pub const Ops = struct {
|
||||
const eq_res = c.LLVMBuildICmp(self.e.builder, c.LLVMIntEQ, a, b, "te.eq");
|
||||
self.e.mapRef(eq_res);
|
||||
},
|
||||
.type_is_unsigned => {
|
||||
// Dynamic `type_is_unsigned(t)`: extract the TypeId from
|
||||
// the arg (Any-boxed Type → value field, or bare i64
|
||||
// index), GEP into the `__sx_type_is_unsigned` table, load
|
||||
// the i1. Mirrors the `type_name` runtime lookup.
|
||||
const arg_ref = bi.args[0];
|
||||
const arg_val = self.e.resolveRef(arg_ref);
|
||||
const tid_idx = switch (self.e.reflectArgRepr(arg_ref)) {
|
||||
.unresolved => @panic("type_is_unsigned: reflection arg IR-type unresolved — a type-resolution failure reached LLVM emission without a diagnostic"),
|
||||
.boxed => c.LLVMBuildExtractValue(self.e.builder, arg_val, 1, "tiu.tid"),
|
||||
.bare => arg_val,
|
||||
};
|
||||
const arr_global = self.e.reflection().getOrBuildTypeIsUnsignedArray();
|
||||
const arr_len = self.e.type_is_unsigned_array_len;
|
||||
const arr_ty = c.LLVMArrayType(self.e.cached_i1, arr_len);
|
||||
const zero = c.LLVMConstInt(self.e.cached_i64, 0, 0);
|
||||
var indices = [2]c.LLVMValueRef{ zero, tid_idx };
|
||||
const gep = c.LLVMBuildInBoundsGEP2(self.e.builder, arr_ty, arr_global, &indices, 2, "tiu.gep");
|
||||
const result = c.LLVMBuildLoad2(self.e.builder, self.e.cached_i1, gep, "tiu.load");
|
||||
self.e.mapRef(result);
|
||||
},
|
||||
.has_impl => {
|
||||
// Runtime has_impl needs a protocol-map
|
||||
// snapshot — not wired yet. Silent false for
|
||||
|
||||
Reference in New Issue
Block a user