ddn.num.uintfix
ddn.num.uintfix — fixed-capacity unsigned integers.
UIntFix!limbs is an unsigned integer of exactly limbs 64-bit words: it lives wherever its owner lives (the stack, a struct, a static array), never grows, never allocates and never throws. Every operation is @safe pure nothrow @nogc and CTFE-able.
Why this module exists
DDN core had no integer wider than Int128, and three of its text and number facilities need one under hard constraints:
std.bigint.BigIntallocates limbs on the GC heap and reportsfailure by throwing, so it cannot be called from
@nogcornothrowcode, and it is far more machinery than a bounded computation needs.Int128stops at 128 bits: enough for the decimal codecs(
ddn.num.decimal,ddn.encoding.bid, the columnar wide decimal), not for longer digit strings.ddn.bigint— the cryptographic multiprecision integer — is adynamic, GC-backed type in
ddn-crypto, a package that depends on core; core cannot import it, and its allocation model does not fit a per-cell parser either.
The computations in question have a provable maximum size: the exact decimal-to-binary conversion behind ddn.data.csv's fromCsv (ADR-2026-11-28-01) needs at most 64 limbs, so a fixed-capacity type whose every intermediate is bounded by a proof is the right tool. Where a computation's bound is known, UIntFix gives it integer arithmetic with the cost profile of a plain struct: no heap traffic, no exception paths, identical results on every platform.
UIntFix is currently used by ddn.data.csv: the correctly rounded conversion of decimal text to double forms its intermediates — powers of five, shifted significands, exact quotients — in UIntFix64 (4096 bits).
Semantics
- The value lies in
0 .. 2^(64 · limbs); the limbs are storedlittle-endian with no leading zero limb.
- Overflow is checked. An operation whose result would not
fit the capacity returns
falseand leaves its operand unchanged; it never wraps and never writes past the array. The caller sizes the capacity — by proof, asddn.data.csvdoes, or by handling thefalse. - The mutating operations mutate and report.
add,sub,mul,mulSmallandshlchange the operand in place and return the overflow flag. Call the operation, then inspect the flag:const ok = v.mulSmall(5); assert(ok);. Never writeassert(v.mulSmall(5))— a release build drops the whole expression, the operation included, and the value silently stays unchanged. - Division takes a nonzero divisor; a zero divisor is an
assertion failure (a precondition, like an out-of-range bit index).
- Conversion follows the
as!Tidiom ofddn.varandddn.util.docopt, withto!Tas an alias for the Phobos spelling:as!string(itstoString()twin, and the freestd.conv.to!string) is the one allocating operation —purebut not@nogc;as!Tto an integral type is range-checked and throwsConvOverflowExceptionon a value that does not fit. The checked, non-throwing forms arefitsUlongandtoUlong; a plaincastis deliberately not offered, since a cast must not throw and a truncating one would break the checked-overflow contract.
Example:
import ddn.num.uintfix;
// 5^40 needs 93 bits: more than a ulong, built without a heap.
auto v = UIntFix!2.of(1);
foreach (_; 0 .. 40) {
const grown = v.mulSmall(5);
assert(grown); // the call runs; only the check vanishes in release
}
assert(v.as!string == "9094947017729282379150390625");
const squared = v.mul(v); // 186 bits would not fit
assert(!squared && v.as!string == "9094947017729282379150390625");
// An exact quotient and remainder far beyond a ulong.
auto num = UIntFix!16.of(1);
foreach (_; 0 .. 100) {
const grown = num.mulSmall(10); // 10^100
assert(grown);
}
UIntFix!16 q, r;
UIntFix!16.divmod(num, UIntFix!16.of(7), q, r);
assert(r.toUlong() == 4);Copyright
Types 2
A fixed-capacity unsigned integer: limbs little-endian 64-bit words with no leading zero word. The capacity is part of the type, so every intermediate is bounded at compile time; an operation whose result would exceed it returns false and leaves the value unchanged.
All operations are @safe pure nothrow @nogc and CTFE-able, except toString, which allocates.
size_t capacityThe number of 64-bit words every value owns.size_t bitCapacityThe number of bits every value owns.UIntFix allOnes() static @safe pure nothrow @nogcReturns: the largest value of this capacity (every bit set).size_t bitLength() const @safe pure nothrow @nogcReturns: the number of significant bits (0 for zero).bool anyBitsBelow(size_t n) const @safe pure nothrow @nogcReturns: whether any bit below `n` is set.ulong bits64(size_t from) const @safe pure nothrow @nogcReturns: the 64 bits starting at bit `from` (zero beyond the capacity).const(ulong)[] words() const @safe pure nothrow @nogcReturns: the significant limbs, least significant first (a view; empty never — zero is one zero limb).int opCmp()(auto ref const(UIntFix) o) const @safe pure nothrow @nogcDitto, for ordering and the relational operators.bool mul()(auto ref const(UIntFix) o) @safe pure nothrow @nogcMultiplies by `o` in place, keeping only the low half of the product when both halves fit this capacity.void mulWide()(auto ref const(UIntFix) a, auto ref const(UIntFix) b,
out UIntFix lo, out UIntFix hi) static @safe pure nothrow @nogcThe full double-width product of `a` and `b`: `a × b = hi × 2^bitCapacity + lo`.void shr(size_t n) @safe pure nothrow @nogcShifts right by `n` bits in place (a shift of `bitCapacity` or more yields zero).UIntFix shiftedRight(size_t n) const @safe pure nothrow @nogcReturns: a copy shifted right by `n` bits (zero when `n` is `bitCapacity` or more).void divmod()(auto ref const(UIntFix) a, auto ref const(UIntFix) d,
out UIntFix q, out UIntFix r) static @safe pure nothrow @nogcDivides `a` by `d` with remainder: `q = a / d`, `r = a % d`. A restoring long division aligned to the quotient's top bit, so it runs once per quotient bit rather than once per numerator bit.T as(T)() if (is(T == string) || isIntegral!T) const @safe pureConverts the value to `T` — the `as!T` idiom of `ddn.var` and `ddn.util.docopt` (a member template, written `value.as!T`).