Skip to content

contrib.xmp tracker-module player; std.string to_float parses to 64 bits - #3882

Merged
paul-hammant merged 2 commits into
mainfrom
batch/nov26b
Nov 27, 2026
Merged

paul-hammant merged 2 commits into
mainfrom
batch/nov26b

Conversation

@paul-hammant

Copy link
Copy Markdown
Collaborator

Two commits: a new contrib module and a std.string precision fix.

Commit 1: cfc4814 contrib.xmp, a tracker-module player for std.audio

A new contrib module over full libxmp (MOD, S3M, XM, IT, MED and the other formats libxmp reads), playing through std.audio. It has no audio output of its own.

  • Source: libxmp 4.7.4 from its GitHub release, SHA-256 a25583aa3b031c78ba0b4e83387fa596fb05742622e60f60d1540ee2af79b8d9 (matches upstream's release notes and GitHub's digest), pinned in contrib/xmp/libxmp.lock.
    • Fetched and checked by scripts/fetch-libxmp.sh, and compiled from its source list without configure, the same way contrib/sqlite handles its amalgamation.
    • Falls back to the system libxmp. Cross-builds with ae build --target. Release archives ship the source and NOTICE (MIT, Claudio Matsuoka and Hipolito Carraro Jr).
  • API: load / load_file / free; info (title, format, channels, duration_ms, …); start, render (16-bit stereo), render_all, seek_ms, set_order, position_ms, looping, ended.
    • std.audio: sound(p, rate) (whole module via load_pcm) and stream_open + pump (a stream ring).
    • One context per player, no shared state.
  • Tests: contrib/xmp/test_xmp.ae has 20 tests on a MOD and an XM written byte by byte, covering sound, silence, exact length, seek, loop, error paths and std.audio headless. It fails on five deliberate mutations. Also tests/integration/libxmp_fetch and xmp_vendored_cross.
  • Lanes:
    • ae-x64: CC=gcc make ci passed; make test-ae 2397 passed / 0 failed; valgrind contrib-check, veneer under -Werror, ASan+UBSan, release_contrib_c_modules, and the xmp and sqlite cross builds for x86_64- and aarch64-linux-musl (qemu) all pass.
    • Mac: spec 20/20, leaks 0.
    • Windows under Wine: 20/20.
    • FreeBSD 15.1: gmake test 786/786; gmake test-ae 2381 passed, 1 failed (cross_emit_lib, a cold-zig-cache timeout, not using xmp); spec valgrind-clean.
  • Open questions:
    1. contrib_check.sh passes valgrind --fair-sched, which FreeBSD's valgrind refuses (Linux-only in CI).
    2. install.sh doesn't fetch libxmp (nor sqlite). Should it, as it does quickjs?
    3. ae_vendored_xmp compiles one file at a time, about 70 s per target. Parallelise it?
    4. Listed in docs/stdlib-vs-contrib.md and docs/bootstrap-from-source.md (docs/api covers std/ only).

Commit 2: 8ed403b

std.string: to_float parses to 64 bits, correctly rounded; to_f32 added (8ed403b)

Bug. Aether's float is 64-bit (C double); f32 is the 32-bit type. string.to_float parsed with strtof and widened the 32-bit result, so it returned about seven significant digits dressed as a double. The C side's comment called this deliberate ("to_float's contract"), but nothing in the language makes float 32-bit.

Repro (0.947.0):

import std.string
main() {
    a, e1 = string.to_float("0.1")
    println(string.string_from_double(a))   // 0.10000000149011612 (wrong)
    b, e2 = string.to_double("0.1")
    println(string.string_from_double(b))   // 0.10000000000000001
    c, e3 = string.to_float("16777217")
    println(string.string_from_double(c))   // 16777216 (wrong)
}

Fix.

  • to_float(s) -> (float, string) parses with strtod (locale-pinned aether_c_strtod), correctly rounded to binary64, with exactly the syntax to_double accepted: hexadecimal constants, inf/infinity/nan in any case with a sign, the same whitespace and error rules. string_to_float_raw now writes a double; string_get_float parses at 64 bits instead of widening a float.
  • to_double is kept, not removed. This is not a breaking change. It is now return to_float(s), so the two cannot drift. The externs string_try_double / string_get_double are kept the same way, as the old names of string_try_float / string_get_float, because downstream repos call them directly. Each carries a TODO: delete it once nothing in the aether-lang-dev org's repos or in friends-and-family repos uses it. string_to_double_raw is removed: nothing outside std.string used it.
  • New string.to_f32(s) -> (f32, string): strtof, rounded once and directly to binary32, returning a real f32 (C float). A value past the f32 range is an overflow error. Externs: string_to_f32_raw, string_try_f32, string_get_f32. No in-tree caller wanted 32-bit rounding. to_f32 keeps the locale-pinned strtof path (aether_c_strtof, with its msvcrt handling) reachable and tested.
  • Every in-tree caller moves from to_double to to_float, so the in-tree count is now 0. That covers about 34 call sites across the benchmarks, contrib/jq, contrib/templating/liquid, std/cbor, std/decimal, std/jsonpath (string.get_double), the tests, the LSP completion list and docs/stdlib-reference.md. decimal.to_double is a conversion from decimal to float, not a parse, so it keeps its name. The existing to_float callers (liquid, schema, resp, message, language) all wanted full precision. They now get it.
  • docs/api and the stdlib index are regenerated, and check-docs passes. Changelog fragment: changed/to-float-64-bit.

Tests.

  • New tests/regression/test_string_to_float_64_bit.ae. Run against the old code, it failed 6 checks. It checks:

    • to_float("0.1") == 0.1, bits 3fb999999999999a;
    • "16777217" reads back as 16777217;
    • 17-significant-digit values (pi, DBL_MAX, DBL_MIN, 2^53+1, 0.30000000000000004) round-trip through string_from_double to the same bits, and pi has the exact bits;
    • "1e39" is in range;
    • to_f32 rounds like strtof: "0.1" gives 3fb99999a0000000 widened, "16777217" gives 16777216, and "1e39" is an overflow error;
    • to_double equals to_float bit for bit on decimal, hex, infinity and whitespace-padded input, and still rejects trailing garbage.
  • Tests whose expectations encoded the 32-bit parse now call to_f32. Their to_double checks became to_float checks:

    No golden file changed.

Lanes.

  • Mac (arm64, clang):
    • On the final tree: make test-ae 2362 passed, 0 failed, 55 skipped. make check-docs passes.
    • On the combined batch/nov26b tree: build, every std.string / decimal / cbor test and contrib/xmp/test_xmp.ae (via contrib_check.sh, after make contrib MODULES=xmp) pass.
  • ghostbsd (FreeBSD 15.1, native, own dir):
    • On the first cut (to_double removed): gmake test 786/786. gmake test-ae 2381 passed, 2 failed, 34 skipped. Both failures pass when run alone: crypto_tls13_external_psk, and cross_emit_lib (420 s timeout under load, 78 s alone).
    • On the final tree: gmake test 786/786, and all the string tests pass.
  • ae-x64 (Ubuntu 22.04, gcc, own dir), first cut: CC=gcc make ci compiled clean under -Werror and ran C units 788/788. Its test-ae stage had 2395 passed, 2 failed, 20 skipped. The two failures were cross_emit_lib and cross_linux_musl, both hitting the 420 s timeout on a box at load average 7+ shared with other agents; neither touches std.string. - ae-x64 (Ubuntu 22.04 x86_64): make test-ae 2397 passed, 0 failed; final tree built with gcc (compiler -Werror) and all 62 string tests pass.

Downstream callers to migrate to to_float before to_double is deleted (grep of ~/scm/*/ excluding aether and not-ours; nothing breaks today):

  • RexxAE: string.string_try_double / string.string_get_double (the externs) in src/builtins.ae, engine.ae, evaluate.ae, library.ae, program.ae, values.ae (about 30 lines).
  • ae3d: string.get_double / try_double in src/ae3d/loader/module.ae, tools/drive_editor.ae, tools/check_coplanar.ae; string.to_double in tests/test_loader.ae and the vendored deps/aephysics/aephysics/files/module.ae.
  • aephysics: aephysics/files/module.ae.
  • aether-ui: vg/ (29 files: module.ae, README.md, backend/gtk.ae, chart/*, geom/*, grammar/*, raster/rasterize.ae, svg/*, test/*) and apps/analog_clock_png.
  • sae and sae-pixel: aevg/tsemit.ae, tests/spec_aevg_parity.ae.
  • servirtium-vcr: integration/climate_interop/module.ae.
  • testcontainers-ae-port: sql/module.ae, sql/mysql/mysqlproto/module.ae.

🤖 Generated with Claude Code

paul-hammant and others added 2 commits November 26, 2026 21:48
contrib.xmp plays MOD, S3M, XM, IT, MED and the other formats libxmp
reads, through std.audio. It is full libxmp (the player, every loader,
ProWizard and the depackers), and std.audio stays the only audio output.

API (contrib/xmp/module.ae): load(data, length) / load_file(path) -> ptr!,
free; title, format, channels, patterns, orders, instruments, samples,
duration_ms; start(p, rate), render(p, frames) -> string! (interleaved
signed 16-bit stereo), render_all, seek_ms, set_order, position_ms, order,
row, looping, ended; and through std.audio, sound(p, rate) (render_all
into audio.load_pcm) and stream_open(p, rate, frames) + pump(p, src)
(blocks pushed into an audio.stream_open ring). Each player owns its
libxmp context and buffers; nothing runs on a thread of its own.

libxmp's source is handled as contrib.sqlite's amalgamation is:
contrib/xmp/libxmp.lock pins libxmp 4.7.4's GitHub release tarball by URL
and SHA-256 (the checksum libxmp publishes for the release);
scripts/fetch-libxmp.sh downloads it, refuses a mismatch, and extracts
include/, src/ (no src/lite) and the licence into the gitignored
contrib/xmp/libxmp/, with sources.txt taken from Makefile.vc's object
lists. No configure or CMake.

- make contrib (tests/scripts/contrib_build.sh) compiles those sources into
  build/contrib/libxmp.a beside libaether_xmp.a, cached by release,
  compiler and flags, and falls back to the system libxmp when the source
  cannot be fetched (LIBXMP_SYSTEM=1 forces it).
- ae build --target compiles libxmp for the target (ae_vendored_xmp in
  tools/ae_cross.c, one cache slot per object), unless a CROSSBUILD_SYSROOT
  stages the archives; a binary release with no archives compiles it for
  the host the same way (tools/ae.c).
- Release staging fetches the source and ships it, with libxmp's COPYING
  and contrib/xmp/NOTICE, plus the two archives; the install trim keeps the
  source; CI fetches it and runs the spec (valgrind on Linux, Windows) and
  the cross build.

Tests: contrib/xmp/test_xmp.ae writes a ProTracker MOD and a Fast Tracker
II XM byte by byte and asserts on the rendered PCM (sound, silence, exact
length, seek and loop positions, refusals), with the std.audio half on the
headless engine; tests/integration/libxmp_fetch (the fetch script, offline)
and tests/integration/xmp_vendored_cross (zig).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Aether's `float` is 64-bit (C double), but string.to_float parsed with
strtof and widened the 32-bit result, so it returned about seven
significant digits dressed as a double:

    a, _ = string.to_float("0.1")
    println(string.string_from_double(a))   // 0.10000000149011612
    c, _ = string.to_float("16777217")
    println(string.string_from_double(c))   // 16777216

to_float now parses with strtod, correctly rounded to binary64, with the
syntax to_double accepted (hex constants, inf/nan spellings, the same
errors). string_to_float_raw writes a double; string_get_float parses at
64 bits rather than widening a float.

to_double is kept, as a call to to_float, so the two always agree; the
externs string_try_double and string_get_double are kept the same way,
as string_try_float and string_get_float under their old names. Each
carries a TODO: delete it once nothing in the aether-lang-dev org's repos
or in friends-and-family repos uses it. Not a breaking change. Every
caller in this tree moves to to_float (benchmarks, contrib jq and liquid,
std cbor, decimal and jsonpath, the tests and the docs), so the in-tree
count of to_double callers is zero. decimal.to_double is a
decimal-to-float conversion, not a parse, and keeps its name.
string_to_double_raw, which nothing outside std.string used, is removed.

string.to_f32(s) -> (f32, string) is the 32-bit parse: strtof, rounded
once straight to binary32, returning a real f32 (C float), with past the
f32 range an overflow error; externs string_to_f32_raw, string_try_f32,
string_get_f32. No caller in the tree wanted 32-bit rounding; to_f32
keeps the locale-pinned strtof path (aether_c_strtof, with its msvcrt
handling) reachable and tested.

Tests whose expectations encoded the 32-bit parse now call to_f32:
test_string_parse_hex_float and test_string_parse_float_halfway (their
float-bits checks, e.g. 0x1.000001p0 -> 1.0 and the float-tie inputs),
test_string_parse_inf_nan, test_string_embedded_nul,
test_string_to_number_widths and tests/syntax/test_string_parse_tuple;
their to_double checks become to_float checks. No golden file changed.

New tests/regression/test_string_to_float_64_bit.ae, failing before the
fix: to_float("0.1") equals the literal 0.1 with bits 3fb999999999999a,
"16777217" reads back as 16777217, 17-significant-digit values
(pi, DBL_MAX, DBL_MIN, ...) round-trip through string_from_double to the
same bits, "1e39" is in range; to_f32 rounds like strtof ("0.1" ->
3fb99999a0000000 widened, "16777217" -> 16777216, "1e39" overflows);
to_double equals to_float bit for bit and still rejects trailing garbage.

The LSP completion list gains to_f32 and marks to_double slated for
deletion; docs/api and the stdlib index regenerated; changelog fragment
under changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@paul-hammant
paul-hammant merged commit 6666346 into main Nov 27, 2026
55 checks passed
@paul-hammant
paul-hammant deleted the batch/nov26b branch November 27, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant