diff --git a/benchmarks/cross-language/README.md b/benchmarks/cross-language/README.md index e3d091cac..b0470dfac 100644 --- a/benchmarks/cross-language/README.md +++ b/benchmarks/cross-language/README.md @@ -53,7 +53,9 @@ would use, fixed by the shape of the work rather than picked per pattern. ### Statistical approach - Each benchmark runs **5 times** (`BENCH_RUNS=N`) after **1 warmup run** - (`BENCH_WARMUP=N`), the same for every language + (`BENCH_WARMUP=N`), the same for every language. Skynet runs **25 times** + (`BENCH_SKYNET_RUNS=N`): one run takes about 0.3 ms, short enough that a + median of five is mostly scheduling jitter (#3868) - The **median** of the timed runs is reported, with the minimum, the maximum and the coefficient of variation (sample standard deviation over the mean) - Every run's value is stored in the results JSON diff --git a/benchmarks/cross-language/config.md b/benchmarks/cross-language/config.md index 3c8ec51b7..ad52e64bf 100644 --- a/benchmarks/cross-language/config.md +++ b/benchmarks/cross-language/config.md @@ -36,6 +36,7 @@ The runner reads its settings from the environment: | `BENCHMARK_MESSAGES` | 1000000 | Messages per run (skynet: leaves) | | `BENCH_RUNS` | 5 | Timed runs per language and pattern; the median is reported | | `BENCH_WARMUP` | 1 | Discarded runs before them, the same for every language | +| `BENCH_SKYNET_RUNS` | 25 | Timed runs for skynet, whose run is about 0.3 ms (#3868); never fewer than `BENCH_RUNS` | | `BENCH_LANGS` | all | Languages to run, comma-separated (`aether,c,rust`) | | `BENCH_PATTERNS` | all | Patterns to run, comma-separated (`ping_pong,skynet`) | | `BENCH_STRICT` | 0 | 1: exit 1 if any selected language does not build or is wrong (the CI job) | diff --git a/benchmarks/cross-language/run_benchmarks.ae b/benchmarks/cross-language/run_benchmarks.ae index 59972ac37..a4f11834f 100644 --- a/benchmarks/cross-language/run_benchmarks.ae +++ b/benchmarks/cross-language/run_benchmarks.ae @@ -399,12 +399,21 @@ main() { if env_r != 0 { runs = atoi(env_r) } env_w = getenv("BENCH_WARMUP") if env_w != 0 { warmup = atoi(env_w) } + // Skynet gets more timed runs (#3868): one run creates 1,111 units in + // about 0.3 ms, short enough that scheduling jitter outweighs speed + // (cv 14-27% in five runs), so its median is taken over 25. The same + // count for every language, as every other setting (FAIRNESS.md); never + // fewer than BENCH_RUNS. + skynet_runs = 25 + env_sr = getenv("BENCH_SKYNET_RUNS") + if env_sr != 0 { skynet_runs = atoi(env_sr) } env_m = getenv("BENCHMARK_MESSAGES") if env_m != 0 { msgs = atoi(env_m) } if runs < 1 { println("ERROR: BENCH_RUNS must be at least 1") exit(1) } + if skynet_runs < runs { skynet_runs = runs } only_langs = "" env_l = getenv("BENCH_LANGS") if env_l != 0 { only_langs = string.to_lower(env_l) } @@ -461,8 +470,6 @@ main() { } arch = sh_text("uname -m") - cfg = Settings { runs: runs, warmup: warmup, msgs: msgs, time_cmd: time_cmd, is_mac: is_mac } - _mk, _mks = sh_run("mkdir -p build") io_write_file("build/problems.txt", "") @@ -470,7 +477,7 @@ main() { println(" Aether Cross-Language Benchmark Suite") println("============================================") println(" Messages: ${msgs}") - println(" Runs: ${runs} (median), warmup: ${warmup} for every language") + println(" Runs: ${runs} (median; skynet ${skynet_runs}), warmup: ${warmup} for every language") println(" ${measure_note}") println("") @@ -486,11 +493,15 @@ main() { println(" Running: ${pattern}") println("============================================") + pruns = runs + if pattern == "skynet" { pruns = skynet_runs } + pcfg = Settings { runs: pruns, warmup: warmup, msgs: msgs, time_cmd: time_cmd, is_mac: is_mac } + ts = sh_text("date -u +\"%Y-%m-%dT%H:%M:%SZ\"") json = "{\n \"timestamp\": \"${ts}\",\n" json = "${json} \"pattern\": \"${pattern}\",\n" json = "${json} \"messages\": ${msgs},\n" - json = "${json} \"runs_per_benchmark\": ${runs},\n" + json = "${json} \"runs_per_benchmark\": ${pruns},\n" json = "${json} \"warmup_runs\": ${warmup},\n" json = "${json} \"description\": \"${pattern_description(pattern)}\",\n" json = "${json} \"measurement\": \"${json_escape(measure_note)}\",\n" @@ -508,7 +519,7 @@ main() { li = li + 1 continue } - res = run_lang(li, pattern, cfg) + res = run_lang(li, pattern, pcfg) // A missing third-party toolchain is a fair skip; a missing or // wrong Aether is not. A results file that omits Aether while // listing ten other languages reads as a complete comparison. diff --git a/new_changelogs/20261124T192416Z-changed-3868-skynet-runs.md b/new_changelogs/20261124T192416Z-changed-3868-skynet-runs.md new file mode 100644 index 000000000..15ce9bffa --- /dev/null +++ b/new_changelogs/20261124T192416Z-changed-3868-skynet-runs.md @@ -0,0 +1,6 @@ +- **The cross-language skynet takes its median over 25 runs (#3868).** + One skynet run creates 1,111 units in about 0.3 ms, short enough that a + median of five runs was mostly scheduling jitter (cv 14-27%, Pony's lead + inside it). The runner now times skynet 25 times for every language + (`BENCH_SKYNET_RUNS`, never fewer than `BENCH_RUNS`) and records the + count in the results; the other patterns keep five. diff --git a/new_changelogs/20261124T192416Z-performance-3852-math-inline.md b/new_changelogs/20261124T192416Z-performance-3852-math-inline.md new file mode 100644 index 000000000..23cc76c27 --- /dev/null +++ b/new_changelogs/20261124T192416Z-performance-3852-math-inline.md @@ -0,0 +1,14 @@ +- **std.math's scalar functions are inline (#3852).** `math.sqrt`, + `abs`, `min`, `max`, `clamp`, `floor`, `ceil`, `round` and their f32 + forms were calls into libaether.a the C compiler could not see through, + each calling libm in turn. They now come from `aether_math_inline.h` + through `@c_include`, as std.string's reads do (#3836), using the + compiler's builtins on GCC and clang (no `` in the program) and + libm's names elsewhere; the exported `math_*` functions are the same + bodies, and the values are unchanged (NaN handling of min/max, + `abs_int(INT_MIN)`, half-away rounding: `test_math_inline_scalars`). A + loop of sqrt, floor, min and abs went from 2.38 to 1.98 ns an iteration. + Transcendental functions stay out of line. On x86-64 MinGW `math.sqrt` + is now `sqrtsd`, correctly rounded, where the CRT's x87 sqrt rounded + twice (#3027): a result there may differ from before by one ulp, toward + the correct one. diff --git a/new_changelogs/20261124T192416Z-performance-3853-slice-release.md b/new_changelogs/20261124T192416Z-performance-3853-slice-release.md new file mode 100644 index 000000000..bb8946815 --- /dev/null +++ b/new_changelogs/20261124T192416Z-performance-3853-slice-release.md @@ -0,0 +1,10 @@ +- **A slice's release is inline again for a view (#3853).** The release a + slice gets where it dies (a local's scope exit, a store, an unwind) is + `static inline`, but it carried the string release, the buffer's free and + its element drops, so the compiler left the whole release out of line: a + call per place for what is, for a view (`ptr as T[]`, a slice of a + slice), one test of its marks. The counted path that frees or touches a + published count now sits in a cold helper; the uncounted test and the + common counted case (a reference that is not the last) stay inline. + bench-core on an Apple M5 Pro: `slice_view_returned` 0.86 to 0.22 ns, + `slice_view_bound` 3.34 to 3.15 ns; the ownership rows unchanged. diff --git a/runtime/aether_slice.h b/runtime/aether_slice.h index 29fb6ae18..ea7c03aca 100644 --- a/runtime/aether_slice.h +++ b/runtime/aether_slice.h @@ -204,14 +204,28 @@ static inline void aether_slice_buf_free(AetherSliceBuf* h) { free(h); } -/* The reference a place held to the buffer `s` views, given back; the last - * frees the buffer. An uncounted slice gives back nothing. */ -static inline void aether_slice_unref(AetherSlice s) { +/* aether_slice_unref's counted path, out of its callers (#3853): a release + * is inlined at every place a slice dies (a local's scope exit, a store, an + * unwind), and most of them hold a view (`ptr as T[]`, a parameter, a + * slice of a slice), which gives back nothing. With the counted path + * inline, the string release and the buffer's free with its element drops + * came too, and the compiler left the whole release out of line, a call per + * place for two flag tests. Marked as AETHER_SLICE_FAIL's helpers are: + * cold on GCC and clang, which keeps it out of its callers, and noinline + * on cl.exe, which has no cold. */ +#if defined(__GNUC__) || defined(__clang__) +#define AETHER_SLICE_COLD_PATH static inline __attribute__((cold)) +#elif defined(_MSC_VER) +#define AETHER_SLICE_COLD_PATH static __inline __declspec(noinline) +#else +#define AETHER_SLICE_COLD_PATH static inline +#endif + +AETHER_SLICE_COLD_PATH void aether_slice_unref_counted(AetherSlice s) { if (s.flags & AETHER_SLICE_STRING) { string_release(aether_slice_string_of(s)); return; } - if (!(s.flags & AETHER_SLICE_MADE)) return; AetherSliceBuf* h = aether_slice_buf(s); int rc = aether_count_load(&h->refs); if (rc & AETHER_SLICE_SHARED_BIT) { @@ -222,6 +236,25 @@ static inline void aether_slice_unref(AetherSlice s) { if (rc <= 1) aether_slice_buf_free(h); } +/* The reference a place held to the buffer `s` views, given back; the last + * frees the buffer. Inline: an uncounted slice, which gives back nothing, + * and the common counted case, a reference of an unpublished buffer that is + * not the last (a view of a counted buffer, stored again in a loop), a load + * and a store. Out of line: a string's release, a published count, and the + * last reference, which frees. */ +static inline void aether_slice_unref(AetherSlice s) { + if (!(s.flags & (AETHER_SLICE_STRING | AETHER_SLICE_MADE))) return; + if (!(s.flags & AETHER_SLICE_STRING)) { + AetherSliceBuf* h = aether_slice_buf(s); + int rc = aether_count_load(&h->refs); + if (!(rc & AETHER_SLICE_SHARED_BIT) && rc > 1) { + h->refs = rc - 1; + return; + } + } + aether_slice_unref_counted(s); +} + /* `s` is reachable from other threads from here on (a module `var`, a * closure, a message, an actor's state): its count is atomic, and so is the * count of everything its elements hold (a row of a slice of slices, a diff --git a/std/math/aether_math.c b/std/math/aether_math.c index 9e3769e2f..44da09e08 100644 --- a/std/math/aether_math.c +++ b/std/math/aether_math.c @@ -1,4 +1,5 @@ #include "aether_math.h" +#include "aether_math_inline.h" #include #include #include @@ -11,66 +12,60 @@ // unsigned arithmetic, where that is defined: `-x` overflowed, which C // leaves undefined. int math_abs_int(int x) { - return x < 0 ? (int)(0u - (unsigned)x) : x; + return aether_math_abs_int_inline(x); } double math_abs_float(double x) { - return fabs(x); + return aether_math_abs_float_inline(x); } int math_min_int(int a, int b) { - return a < b ? a : b; + return aether_math_min_int_inline(a, b); } int math_max_int(int a, int b) { - return a > b ? a : b; + return aether_math_max_int_inline(a, b); } // fmin/fmax: when one side is NaN the other is returned, whichever side it // is on. A `<` pick returned b for min(NaN, 1) but NaN for min(1, NaN). double math_min_float(double a, double b) { - return fmin(a, b); + return aether_math_min_float_inline(a, b); } double math_max_float(double a, double b) { - return fmax(a, b); + return aether_math_max_float_inline(a, b); } int math_clamp_int(int x, int min, int max) { - if (x < min) return min; - if (x > max) return max; - return x; + return aether_math_clamp_int_inline(x, min, max); } double math_clamp_float(double x, double min, double max) { - if (x < min) return min; - if (x > max) return max; - return x; + return aether_math_clamp_float_inline(x, min, max); } // Advanced math double math_sqrt(double x) { - return sqrt(x); + return aether_math_sqrt_inline(x); } // Single precision (#2151). fminf/fmaxf rather than a `<` pick: they // return the non-NaN operand when one side is NaN, as the double // versions below do through fmin/fmax. -float math_sqrt_f32(float x) { return sqrtf(x); } -float math_abs_f32(float x) { return fabsf(x); } -float math_min_f32(float a, float b) { return fminf(a, b); } -float math_max_f32(float a, float b) { return fmaxf(a, b); } +float math_sqrt_f32(float x) { return aether_math_sqrt_f32_inline(x); } +float math_abs_f32(float x) { return aether_math_abs_f32_inline(x); } +float math_min_f32(float a, float b) { return aether_math_min_f32_inline(a, b); } +float math_max_f32(float a, float b) { return aether_math_max_f32_inline(a, b); } float math_clamp_f32(float x, float min, float max) { - if (x < min) return min; - if (x > max) return max; - return x; + return aether_math_clamp_f32_inline(x, min, max); } float math_sin_f32(float x) { return sinf(x); } float math_cos_f32(float x) { return cosf(x); } float math_tan_f32(float x) { return tanf(x); } float math_atan2_f32(float y, float x) { return atan2f(y, x); } -float math_floor_f32(float x) { return floorf(x); } -float math_ceil_f32(float x) { return ceilf(x); } +float math_floor_f32(float x) { return aether_math_floor_f32_inline(x); } +float math_ceil_f32(float x) { return aether_math_ceil_f32_inline(x); } float math_pow_f32(float base, float exp) { return powf(base, exp); } float math_exp_f32(float x) { return expf(x); } @@ -107,15 +102,15 @@ double math_atan2(double y, double x) { } double math_floor(double x) { - return floor(x); + return aether_math_floor_inline(x); } double math_ceil(double x) { - return ceil(x); + return aether_math_ceil_inline(x); } double math_round(double x) { - return round(x); + return aether_math_round_inline(x); } /* Round to nearest and return an INTEGER type, unlike math_round which rounds diff --git a/std/math/aether_math_inline.h b/std/math/aether_math_inline.h new file mode 100644 index 000000000..47cc11255 --- /dev/null +++ b/std/math/aether_math_inline.h @@ -0,0 +1,107 @@ +/* aether_math_inline.h: std.math's scalar functions as `static inline` + * (#3852). + * + * std.math asks for this header with `@c_include` (the #1986 mechanism, as + * std.string does since #3836), so in a program that imports it + * `math.sqrt(x)`, `math.min_float(a, b)` and the rest below are the + * operations they name rather than calls into libaether.a the C compiler + * cannot see through: `math.sqrt` was a call to a function that called + * libm's sqrt, where the compiler emits one instruction (a sqrt and a + * check of its operand, keeping errno as libm sets it). + * + * ONE BODY. aether_math.c includes this header, and the exported math_* + * functions there are these, so C that calls the exported names and an + * Aether program that calls the inline ones compute the same values: the + * same libm semantics (fmin/fmax return the non-NaN operand, abs_int of + * INT_MIN is INT_MIN), the same rounding. + * + * Injected into the translation unit of every program that imports + * std.math, so it declares nothing that is not prefixed (the lesson of + * #2162) and does not include on GCC and clang: generated C does + * not include it there, and a program may define a function of its own + * with one of libm's names. The compiler's builtins are those functions + * without a declaration. Other compilers (cl.exe) get , which + * generated C already includes for them. Transcendental functions (sin, + * pow, exp...) stay out of line: their cost is the libm call itself. */ +#ifndef AETHER_MATH_INLINE_H +#define AETHER_MATH_INLINE_H + +#if defined(__GNUC__) || defined(__clang__) +#define AETHER_MATH_SQRT(x) __builtin_sqrt(x) +#define AETHER_MATH_SQRTF(x) __builtin_sqrtf(x) +#define AETHER_MATH_FABS(x) __builtin_fabs(x) +#define AETHER_MATH_FABSF(x) __builtin_fabsf(x) +#define AETHER_MATH_FMIN(a, b) __builtin_fmin((a), (b)) +#define AETHER_MATH_FMAX(a, b) __builtin_fmax((a), (b)) +#define AETHER_MATH_FMINF(a, b) __builtin_fminf((a), (b)) +#define AETHER_MATH_FMAXF(a, b) __builtin_fmaxf((a), (b)) +#define AETHER_MATH_FLOOR(x) __builtin_floor(x) +#define AETHER_MATH_FLOORF(x) __builtin_floorf(x) +#define AETHER_MATH_CEIL(x) __builtin_ceil(x) +#define AETHER_MATH_CEILF(x) __builtin_ceilf(x) +#define AETHER_MATH_ROUND(x) __builtin_round(x) +#else +#include +#define AETHER_MATH_SQRT(x) sqrt(x) +#define AETHER_MATH_SQRTF(x) sqrtf(x) +#define AETHER_MATH_FABS(x) fabs(x) +#define AETHER_MATH_FABSF(x) fabsf(x) +#define AETHER_MATH_FMIN(a, b) fmin((a), (b)) +#define AETHER_MATH_FMAX(a, b) fmax((a), (b)) +#define AETHER_MATH_FMINF(a, b) fminf((a), (b)) +#define AETHER_MATH_FMAXF(a, b) fmaxf((a), (b)) +#define AETHER_MATH_FLOOR(x) floor(x) +#define AETHER_MATH_FLOORF(x) floorf(x) +#define AETHER_MATH_CEIL(x) ceil(x) +#define AETHER_MATH_CEILF(x) ceilf(x) +#define AETHER_MATH_ROUND(x) round(x) +#endif + +/* |x|. Aether's int wraps, and INT_MIN has no positive counterpart in an + * int, so abs_int(INT_MIN) is INT_MIN (as in Java and Go). Negated in + * unsigned arithmetic, where that is defined: `-x` overflowed, which C + * leaves undefined. */ +static inline int aether_math_abs_int_inline(int x) { + return x < 0 ? (int)(0u - (unsigned)x) : x; +} +static inline double aether_math_abs_float_inline(double x) { return AETHER_MATH_FABS(x); } +static inline int aether_math_min_int_inline(int a, int b) { return a < b ? a : b; } +static inline int aether_math_max_int_inline(int a, int b) { return a > b ? a : b; } + +/* fmin/fmax: when one side is NaN the other is returned, whichever side it + * is on. A `<` pick returned b for min(NaN, 1) but NaN for min(1, NaN). */ +static inline double aether_math_min_float_inline(double a, double b) { return AETHER_MATH_FMIN(a, b); } +static inline double aether_math_max_float_inline(double a, double b) { return AETHER_MATH_FMAX(a, b); } + +static inline int aether_math_clamp_int_inline(int x, int min, int max) { + if (x < min) return min; + if (x > max) return max; + return x; +} +static inline double aether_math_clamp_float_inline(double x, double min, double max) { + if (x < min) return min; + if (x > max) return max; + return x; +} + +static inline double aether_math_sqrt_inline(double x) { return AETHER_MATH_SQRT(x); } +static inline double aether_math_floor_inline(double x) { return AETHER_MATH_FLOOR(x); } +static inline double aether_math_ceil_inline(double x) { return AETHER_MATH_CEIL(x); } +static inline double aether_math_round_inline(double x) { return AETHER_MATH_ROUND(x); } + +/* Single precision (#2151). fminf/fmaxf rather than a `<` pick: they + * return the non-NaN operand when one side is NaN, as the double versions + * do through fmin/fmax. */ +static inline float aether_math_sqrt_f32_inline(float x) { return AETHER_MATH_SQRTF(x); } +static inline float aether_math_abs_f32_inline(float x) { return AETHER_MATH_FABSF(x); } +static inline float aether_math_min_f32_inline(float a, float b) { return AETHER_MATH_FMINF(a, b); } +static inline float aether_math_max_f32_inline(float a, float b) { return AETHER_MATH_FMAXF(a, b); } +static inline float aether_math_clamp_f32_inline(float x, float min, float max) { + if (x < min) return min; + if (x > max) return max; + return x; +} +static inline float aether_math_floor_f32_inline(float x) { return AETHER_MATH_FLOORF(x); } +static inline float aether_math_ceil_f32_inline(float x) { return AETHER_MATH_CEILF(x); } + +#endif /* AETHER_MATH_INLINE_H */ diff --git a/std/math/module.ae b/std/math/module.ae index 69534f227..d3dcfaf1c 100644 --- a/std/math/module.ae +++ b/std/math/module.ae @@ -17,18 +17,26 @@ exports( math_floor_f32, math_ceil_f32, math_pow_f32, math_exp_f32 ) +@c_include("aether_math_inline.h") +// #3852: the scalar functions below marked @c_import are `static inline` in +// that header, so `math.sqrt(x)` is the operation itself rather than a call +// into libaether.a the C compiler cannot see through. The same bodies are +// exported under the math_* names for C and for modules that declare them +// themselves. @c_import: the header's definition is the only declaration of +// these names in the program. + // Basic operations (int/float variants) -extern math_abs_int(x: int) -> int -extern math_abs_float(x: float) -> float -extern math_min_int(a: int, b: int) -> int -extern math_max_int(a: int, b: int) -> int -extern math_min_float(a: float, b: float) -> float -extern math_max_float(a: float, b: float) -> float -extern math_clamp_int(x: int, min: int, max: int) -> int -extern math_clamp_float(x: float, min: float, max: float) -> float +@extern("aether_math_abs_int_inline") math_abs_int(x: int) -> int @c_import +@extern("aether_math_abs_float_inline") math_abs_float(x: float) -> float @c_import +@extern("aether_math_min_int_inline") math_min_int(a: int, b: int) -> int @c_import +@extern("aether_math_max_int_inline") math_max_int(a: int, b: int) -> int @c_import +@extern("aether_math_min_float_inline") math_min_float(a: float, b: float) -> float @c_import +@extern("aether_math_max_float_inline") math_max_float(a: float, b: float) -> float @c_import +@extern("aether_math_clamp_int_inline") math_clamp_int(x: int, min: int, max: int) -> int @c_import +@extern("aether_math_clamp_float_inline") math_clamp_float(x: float, min: float, max: float) -> float @c_import // Advanced math -extern math_sqrt(x: float) -> float +@extern("aether_math_sqrt_inline") math_sqrt(x: float) -> float @c_import extern math_pow(base: float, exp: float) -> float extern math_sin(x: float) -> float extern math_cos(x: float) -> float @@ -37,9 +45,9 @@ extern math_asin(x: float) -> float extern math_acos(x: float) -> float extern math_atan(x: float) -> float extern math_atan2(y: float, x: float) -> float -extern math_floor(x: float) -> float -extern math_ceil(x: float) -> float -extern math_round(x: float) -> float +@extern("aether_math_floor_inline") math_floor(x: float) -> float @c_import +@extern("aether_math_ceil_inline") math_ceil(x: float) -> float @c_import +@extern("aether_math_round_inline") math_round(x: float) -> float @c_import // Rounds half-to-EVEN and returns an integer directly (math_round rounds // half-away-from-zero and returns a float you must then cast). extern math_lrint(x: float) -> long @@ -51,17 +59,17 @@ extern math_exp(x: float) -> float // fminf, ...): an f32 argument stays a C float and the result is f32, so // `math.sqrt_f32(dot(v, v))` in an f32 pipeline is one float sqrt. The // double functions above take an f32 too, but widen it and return float. -extern math_sqrt_f32(x: f32) -> f32 -extern math_abs_f32(x: f32) -> f32 -extern math_min_f32(a: f32, b: f32) -> f32 -extern math_max_f32(a: f32, b: f32) -> f32 -extern math_clamp_f32(x: f32, min: f32, max: f32) -> f32 +@extern("aether_math_sqrt_f32_inline") math_sqrt_f32(x: f32) -> f32 @c_import +@extern("aether_math_abs_f32_inline") math_abs_f32(x: f32) -> f32 @c_import +@extern("aether_math_min_f32_inline") math_min_f32(a: f32, b: f32) -> f32 @c_import +@extern("aether_math_max_f32_inline") math_max_f32(a: f32, b: f32) -> f32 @c_import +@extern("aether_math_clamp_f32_inline") math_clamp_f32(x: f32, min: f32, max: f32) -> f32 @c_import extern math_sin_f32(x: f32) -> f32 extern math_cos_f32(x: f32) -> f32 extern math_tan_f32(x: f32) -> f32 extern math_atan2_f32(y: f32, x: f32) -> f32 -extern math_floor_f32(x: f32) -> f32 -extern math_ceil_f32(x: f32) -> f32 +@extern("aether_math_floor_f32_inline") math_floor_f32(x: f32) -> f32 @c_import +@extern("aether_math_ceil_f32_inline") math_ceil_f32(x: f32) -> f32 @c_import extern math_pow_f32(base: f32, exp: f32) -> f32 extern math_exp_f32(x: f32) -> f32 diff --git a/tests/integration/f32_arithmetic/test_f32_arithmetic.sh b/tests/integration/f32_arithmetic/test_f32_arithmetic.sh index 4ccbd2730..6cdf490ec 100644 --- a/tests/integration/f32_arithmetic/test_f32_arithmetic.sh +++ b/tests/integration/f32_arithmetic/test_f32_arithmetic.sh @@ -90,7 +90,8 @@ if [ "$got" != "1.5 4 0 2.5 10" ]; then fail=1 fi "$AETHERC" "$tmp/m.ae" "$tmp/m.c" >/dev/null 2>&1 -if ! grep -q "float r = (math_sqrt_f32(v) \* 0.5f)" "$tmp/m.c"; then +# The call is std.math's inline sqrt_f32 since #3852 (aether_math_inline.h). +if ! grep -q "float r = (aether_math_sqrt_f32_inline(v) \* 0.5f)" "$tmp/m.c"; then echo " [FAIL] f32_arithmetic: math.sqrt_f32 result is not a float local" fail=1 fi diff --git a/tests/regression/test_math_inline_scalars.ae b/tests/regression/test_math_inline_scalars.ae new file mode 100644 index 000000000..12c720e9f --- /dev/null +++ b/tests/regression/test_math_inline_scalars.ae @@ -0,0 +1,65 @@ +// std.math's scalar functions come from aether_math_inline.h (#3852): the +// program calls the header's static inline bodies, not libaether.a. The +// exported math_* functions are the same bodies, so the values must be the +// ones the out-of-line functions gave. This pins the semantics the header +// must keep: +// +// - min/max return the non-NaN operand whichever side the NaN is on (fmin, +// fmax), not what a `<` pick would; +// - abs_int(INT_MIN) is INT_MIN, Aether's int wrapping as in Java and Go; +// - round is half away from zero, floor/ceil as libm's; +// - the f32 forms the same, in single precision. +import std.math + +extern exit(code: int) + +fail(msg: string) { + println("FAIL: ${msg}") + exit(1) +} + +main() { + zero = 0.0 + nan = zero / zero + if nan == nan { fail("setup: 0/0 is not NaN") } + + // min/max with a NaN on either side give the other operand. + if math.min_float(nan, 1.0) != 1.0 { fail("min_float(NaN, 1)") } + if math.min_float(1.0, nan) != 1.0 { fail("min_float(1, NaN)") } + if math.max_float(nan, 2.0) != 2.0 { fail("max_float(NaN, 2)") } + if math.max_float(2.0, nan) != 2.0 { fail("max_float(2, NaN)") } + if math.min_float(-1.5, 3.0) != -1.5 { fail("min_float(-1.5, 3)") } + if math.max_float(-1.5, 3.0) != 3.0 { fail("max_float(-1.5, 3)") } + + // Integer forms, INT_MIN included. + int_min = -2147483647 - 1 + if math.abs_int(int_min) != int_min { fail("abs_int(INT_MIN) = ${math.abs_int(int_min)}") } + if math.abs_int(-7) != 7 { fail("abs_int(-7)") } + if math.min_int(3, -4) != -4 { fail("min_int") } + if math.max_int(3, -4) != 3 { fail("max_int") } + if math.clamp_int(12, 0, 10) != 10 { fail("clamp_int high") } + if math.clamp_int(-3, 0, 10) != 0 { fail("clamp_int low") } + if math.clamp_float(0.5, 1.0, 2.0) != 1.0 { fail("clamp_float") } + + // sqrt, abs, rounding. + if math.sqrt(2.25) != 1.5 { fail("sqrt(2.25) = ${math.sqrt(2.25)}") } + if math.sqrt(-1.0) == math.sqrt(-1.0) { fail("sqrt(-1) is not NaN") } + if math.abs_float(-0.25) != 0.25 { fail("abs_float") } + if math.floor(-2.5) != -3.0 { fail("floor(-2.5)") } + if math.ceil(-2.5) != -2.0 { fail("ceil(-2.5)") } + if math.round(2.5) != 3.0 { fail("round(2.5) = ${math.round(2.5)}, half away from zero") } + if math.round(-2.5) != -3.0 { fail("round(-2.5) = ${math.round(-2.5)}") } + + // Single precision. + f = 2.25 as f32 + if math.sqrt_f32(f) != 1.5 as f32 { fail("sqrt_f32") } + if math.abs_f32(-f) != f { fail("abs_f32") } + nf = nan as f32 + if math.min_f32(nf, f) != f { fail("min_f32(NaN, x)") } + if math.max_f32(f, nf) != f { fail("max_f32(x, NaN)") } + if math.clamp_f32(f, 0.0 as f32, 1.0 as f32) != 1.0 as f32 { fail("clamp_f32") } + if math.floor_f32(-f) != -3.0 as f32 { fail("floor_f32") } + if math.ceil_f32(-f) != -2.0 as f32 { fail("ceil_f32") } + + println("PASS: math scalars from the inline header") +}