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.BigInt allocates limbs on the GC heap and reports

    failure by throwing, so it cannot be called from @nogc or nothrow code, and it is far more machinery than a bounded computation needs.

  • Int128 stops 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 a

    dynamic, 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 stored

    little-endian with no leading zero limb.

  • Overflow is checked. An operation whose result would not

    fit the capacity returns false and leaves its operand unchanged; it never wraps and never writes past the array. The caller sizes the capacity — by proof, as ddn.data.csv does, or by handling the false.

  • The mutating operations mutate and report. add, sub,

    mul, mulSmall and shl change the operand in place and return the overflow flag. Call the operation, then inspect the flag: const ok = v.mulSmall(5); assert(ok);. Never write assert(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!T idiom of ddn.var and

    ddn.util.docopt, with to!T as an alias for the Phobos spelling: as!string (its toString() twin, and the free std.conv.to!string) is the one allocating operation — pure but not @nogc; as!T to an integral type is range-checked and throws ConvOverflowException on a value that does not fit. The checked, non-throwing forms are fitsUlong and toUlong; a plain cast is 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);

alias UIntFix64
struct UIntFix

Types 2

structUIntFix(size_t limbs) if (limbs >= 1)

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.

Fields
size_t capacityThe number of 64-bit words every value owns.
size_t bitCapacityThe number of bits every value owns.
Methods
UIntFix of(ulong v) static @safe pure nothrow @nogcReturns: the integer `v`.
UIntFix allOnes() static @safe pure nothrow @nogcReturns: the largest value of this capacity (every bit set).
bool isZero() const @safe pure nothrow @nogcReturns: whether the value is zero.
size_t bitLength() const @safe pure nothrow @nogcReturns: the number of significant bits (0 for zero).
bool bit(size_t i) const @safe pure nothrow @nogcReturns: bit `i`.
void setBit(size_t i) @safe pure nothrow @nogcSets bit `i`.
void clearBit(size_t i) @safe pure nothrow @nogcClears bit `i`.
bool anyBitsBelow(size_t n) const @safe pure nothrow @nogcReturns: whether any bit below `n` is set.
bool fitsUlong() const @safe pure nothrow @nogcReturns: whether the value fits a `ulong`.
ulong toUlong() const @safe pure nothrow @nogcReturns: the value as a `ulong`, which it must fit.
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 compare()(auto ref const(UIntFix) o) const @safe pure nothrow @nogcCompares with `o`.
bool opEquals()(auto ref const(UIntFix) o) const @safe pure nothrow @nogcDitto, for `==`.
int opCmp()(auto ref const(UIntFix) o) const @safe pure nothrow @nogcDitto, for ordering and the relational operators.
size_t toHash() const @safe pure nothrow @nogcReturns: a hash over the significant limbs.
bool add(ulong a) @safe pure nothrow @nogcAdds `a` in place.
bool add()(auto ref const(UIntFix) o) @safe pure nothrow @nogcAdds `o` in place.
bool sub()(auto ref const(UIntFix) o) @safe pure nothrow @nogcSubtracts `o` in place.
bool mulSmall(ulong m) @safe pure nothrow @nogcMultiplies by `m` in place.
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`.
bool shl(size_t n) @safe pure nothrow @nogcShifts left by `n` bits in place.
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.
string toString() const @safe pureReturns: the decimal text of the value (allocates).
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`).
aliasUIntFix64 = UIntFix!64

4096 bits (64 limbs): the capacity of the exact decimal-to-double conversion in ddn.data.csv (fromCsv), which forms every one of its intermediates in it.