tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

46 lignes · 4789 octetsBrut · Historique

ADR 0035: Numbers: bit operations on Int, a Float's bits, and U64

Status: Proposed (2026-10-13, issue #143); needs engineering review (#44). Extends ADR 0013 and ADR 0014.

Context

Int had no bit operations, and a Float's IEEE 754 bits couldn't be read or written. So code working on bits couldn't be written in Louise, or only slowly with / and %:

  • random generators (SplitMix64, PCG), for std/random in Louise (#142);
  • binary formats (MessagePack, #140): big-endian integers and floats;
  • base-N codecs (UUID alphabets, #141);
  • hashes and checksums (FNV, CRC32), flags and masks.

Each needed a Rust extern instead.

Decision

std/int: bit operations as named functions

and, or, xor, not, shift_left(n, k), shift_right(n, k), bit_length(n), and in Louise bit(n, k) and low_bits(n, bits).

  • Not operators. The lexer has no &, ^ or <<, | already separates a row's tail, and >> would clash with nested generics (List<List<Int>>). Named functions need no syntax, as in Gleam.
  • Semantics: two's complement with infinite sign extension, as num-bigint and Python compute. -1 is all ones, not(n) is -n - 1, and shift_right rounds towards minus infinity (shift_right(-5, 1) is -3, shift_right(-1, k) is -1). So a small and a big operand give the same result, and promotion stays invisible (ADR 0014).
  • Panics: a negative shift amount; shift_left of a non-zero n by 2^32 bits or more (the result couldn't be allocated; the same message on both backends instead of running out of memory). shift_right by a huge amount is 0 or -1.
  • Fast paths: inline in louise_rt.h for two small ints (2x+1): and and or keep the tag bit (a & b, a | b), xor restores it ((a ^ b) | 1), not flips every bit but it (a ^ ~1): none can overflow. A shift stays inline while its result is small. Anything else calls tl_int_*_slow (num-bigint).
  • Masks above 62 bits can't be literals (literals are 63-bit): int.shift_left(1, 64) - 1.

std/float: conversions and bits

from_int(n) (nearest, ties to even; an infinity past the largest Float), to_bits(x) (in [0, 2^64), so -0.0 is 2^63, a big integer), from_bits(n) (the low 64 bits of n), to_bits32(x) (rounded to a single), from_bits32(n) (exact). NaN payloads are kept: the single conversions are written in louise_rt_prims, shared by the runtime and the interpreter, not left to the hardware.

U64: unsigned 64-bit integers that wrap

  • Why a type: 64-bit algorithms (SplitMix64, PCG64, FNV-1a 64) overflow 63 bits at every step, so on Int every step would promote to a big integer. A fixed-width type with wrapping arithmetic keeps them simple and bounded.
  • Where: std/u64 declares @builtin pub type U64 and its impls (coherence: the type's module). No prelude or type-checker change. No literals: u64.from_int(n) takes n modulo 2^64 (from_int(-1) is the largest), to_int gives [0, 2^64).
  • Traits: Num wraps (+, -, *, negate); / and % are unsigned and panic on zero. Eq, Ord (unsigned), Hash, Show and Debug (decimal), Default (0).
  • Bits: and, or, xor, not; logical shift_left, shift_right (64 or more gives 0, a negative amount panics); rotate_left, rotate_right (modulo 64).
  • Representation: boxed, a TL_TAG_U64 block holding one uint64_t, like a Float. It can't be a 63-bit small int, and ADR 0013 keeps one representation for every value: its specialisation turns dictionary calls into direct calls, but never unboxes. So every result allocates. Unboxing U64 (and Float) would come with an unboxing pass, later; the owner accepted the boxed form for now.
  • No U32: 32-bit algorithms mask an Int (int.and(a + b, 0xFFFFFFFF) stays small).

Runtime ABI 5: the inline bit operations and TL_TAG_U64.

Consequences

  • Generators, codecs and binary formats can be written in Louise, the same on both backends.
  • U64 arithmetic costs an allocation per result, about as much as Float's.
  • Bit masks above 62 bits are computed, not written.

Alternatives considered

  • Operators (&, ^, <<, >>): they read better in algorithms, but need tokens that clash with | in rows and >> in nested generics.
  • Wrapping functions on Int (int.wrapping_mul_u64) instead of a type: no new type, but three quarters of random 64-bit values are above 2^62, so most results would be big integers.
  • An unboxed U64 now: a representation analysis with boxing at polymorphic boundaries, a much larger compiler change.
  • U64 as small-when-it-fits plus boxed: needs a canonical form like Int's, for little gain over boxing.