37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.
C avx2ccpu-inferenceggufinference-enginelinuxllmlocal-llmmixture-of-expertsmoenvmequantizationqwen3simdspeculative-decodingvulkan
Release 2026-10-17: the same answer at temperature 0 whatever the C library and the compiler - exp, log, pow, sin, cos and tanh of the engine's own, no multiply and add fused by the compiler (issue #38); test_quant says whether it compared the AVX2 SiLU (issue #26)
camauri4f00ce55cb10
diff --git a/ChangeLog.md b/ChangeLog.mdindex 82dc908..3c356a2 100644--- a/ChangeLog.md+++ b/ChangeLog.md@@ -2,6 +2,14 @@> Curated, user-facing summary of completed work, newest first.+## [2026-10-17] - The same answer whatever the C library and the compiler++- **The engine computes exp, log, pow, sin, cos and tanh itself** (`src/llm/jmath.c`), in double with plain IEEE-754 operations, where it called the C library's: the rotary angles, the softmax of the experts' router and of the attention's pieces, the DeltaNet's softplus, decay and gates, the soft cap of the logits, the sampler. The C library's functions differ between versions and systems - glibc 2.41 rounds several of them correctly where 2.39 did not - and one unit in the last place was enough to turn a near tie between two tokens the other way: Qwen3.6-35B-A3B's answer at temperature 0 was another text on Ubuntu 24.04 (issue #38, reported by martinambrus). The float results are the double ones rounded once; `tests/test_jmath` checks them against the C library in double over the ranges the engine uses them on (exp within 0 units in the last place, the others within one).+- **No compiler fuses a multiply and an add on its own** (`-ffp-contract=off`): gcc did in the kernels' functions compiled for FMA, each version where it saw fit - eight places, all in the scalar ends of vector loops; the fused operations the kernels write themselves stay.+- **janas-try's answer fingerprints**: medium's is `13e2f198c270b826` and gemma's `bf1626afffa7eeab` now, quick's and full's unchanged, each the same with the GPU, without it and with the AVX2 kernels forced. Against llama.cpp, on eleven texts of six models (Qwen3-0.6B, Qwen3.5-0.8B, 2B and 9B, Qwen3-4B against its own file and against the Q8_0's), the most likely token agrees at 4,111 of 4,400 positions, 4,108 before.+- **test_quant says whether it compared the scalar SiLU with the AVX2 one**: on a CPU without AVX2 and FMA it passed as if a million values had agreed, comparing none (issue #26, reported by phaedrus1992); it prints how many it compared, or that there was nothing to compare with.++## [2026-10-14] - MXFP4 on the GPU, and the experts of a large cache back on it- **A mixture's experts reach the GPU again when the expert cache holds more than 4 GiB of them.** Since the release of 13 October the memory the GPU reads is given to it in regions that start on 2 MB boundaries, each overlapping the one before, and the overlap was not counted in the 4 GiB a region may take: past the first, a region could come to a little more, which the GPU refused, and the expert cache was then not given to it at all. On an integrated GPU a mixture's experts stayed on the CPU, in the fast prompt and in the block read whole on the GPU, whenever the cache held more than about 4 GiB of experts, depending on the size of an expert: Qwen3.6-35B-A3B's MXFP4_MOE file beyond 4 GiB, its Q4_K_M at six bits beyond 8 GiB. Each region counts its overlap now, and `tests/test_import_regions` checks them.diff --git a/build.sh b/build.shindex 937a269..613a418 100755--- a/build.sh+++ b/build.sh@@ -125,7 +125,13 @@ SUFFIX=""# _GNU_SOURCE for every file, empty as the files that define it write it:# the C library of Ubuntu 22.04 (the binary package's) declares memmem,# strcasestr and the like only with it, where a newer one does by default-CFLAGS=(-std=gnu11 -D_GNU_SOURCE= -Wall -Wextra -Werror -pthread -Isrc -Iinclude)+# -ffp-contract=off: no multiply and add fused by the compiler on its own -+# gcc fuses them where FMA is enabled (the kernels' functions), clang within+# an expression, each version where it sees fit - so that one source gives+# the same bits with every compiler (issue #38); the FMAs the kernels write+# with intrinsics stay+CFLAGS=(-std=gnu11 -D_GNU_SOURCE= -Wall -Wextra -Werror -pthread+ -ffp-contract=off -Isrc -Iinclude)if [ "$OS" = windows ]; then# Windows 10 and later; printf with the C99 formats (%zu, %lld); what# MinGW's C library lacks, in one header every file starts withdiff --git a/src/llm/attention.c b/src/llm/attention.cindex f3a2044..88f9d26 100644--- a/src/llm/attention.c+++ b/src/llm/attention.c@@ -15,6 +15,7 @@#include <immintrin.h>#endif+#include "jmath.h"#include "quant.h"#include "vmath.h"@@ -550,7 +551,7 @@ static float att_weights_ref(float *row, uint32_t nt, uint32_t vis, float *mx)m = row[t];for (uint32_t t = 0; t < nt; t++) {float d = row[t] - m;- row[t] = t >= vis || d < -80.0f ? 0.0f : expf(d);+ row[t] = t >= vis || d < -80.0f ? 0.0f : janas_exp_det(d);sum[t % 8] += row[t];}*mx = m;@@ -861,7 +862,7 @@ static void merge_one(struct attn_job *a, uint32_t j, uint32_t h)memset(out, 0, hd * sizeof(float));for (uint32_t c = a->c0; c < nc; c++) {const float *pm = att_partial(A, kvh, c, j, g);- float w = expf(pm[0] - mx);+ float w = janas_expf(pm[0] - mx);sum += w * pm[1];janas_axpy_f32(out, w, pm + 2, hd);}diff --git a/src/llm/jmath.c b/src/llm/jmath.cnew file mode 100644index 0000000..da31c44--- /dev/null+++ b/src/llm/jmath.c@@ -0,0 +1,215 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * jmath.c - the engine's own exp, log, pow, sin, cos and tanh (see jmath.h).+ *+ * Built with the project's common flags, which never enable FMA and turn+ * contraction off (-ffp-contract=off), so that no compiler fuses a multiply+ * and an add here; nothing in this file may carry a target attribute. Every+ * polynomial is a Taylor series cut where the next term is below 1e-17 of the+ * result on the reduced range.+ */+#include "jmath.h"++#include <stdint.h>+#include <string.h>++/* ln 2 and pi/2 in parts whose leading ones have their low bits zero, so+ that k times them is exact for the k used here (fdlibm's constants) */+static const double LN2_HI = 6.93147180369123816490e-01; /* 32 bits */+static const double LN2_LO = 1.90821492927058770002e-10;+static const double INV_LN2 = 1.44269504088896338700e+00;+static const double PIO2_1 = 1.57079632673412561417e+00; /* 33 bits */+static const double PIO2_2 = 6.07710050630396597660e-11; /* 33 bits */+static const double PIO2_2T = 2.02226624879595063154e-21;+static const double INV_PIO2 = 6.36619772367581382433e-01;+/* round to the nearest integer, ties to even, for |x| < 2^51 */+static const double ROUNDER = 0x1.8p52;++static double from_bits(uint64_t u)+{+ double d;+ memcpy(&d, &u, sizeof d);+ return d;+}++static uint64_t to_bits(double d)+{+ uint64_t u;+ memcpy(&u, &d, sizeof u);+ return u;+}++/* 2^k for -1022 <= k <= 1023 */+static double pow2i(int k)+{+ return from_bits((uint64_t)(k + 1023) << 52);+}++double janas_exp(double x)+{+ if (x != x)+ return x;+ if (x > 709.782712893384)+ return from_bits(0x7ff0000000000000ull);+ if (x < -745.1332191019412)+ return 0.0;+ double kd = (x * INV_LN2 + ROUNDER) - ROUNDER;+ int k = (int)kd;+ double r = (x - kd * LN2_HI) - kd * LN2_LO; /* |r| <= 0.347 */+ double p =+ 1.0 / 6227020800.0; /* 1/13!, then Horner down to the constant 1 */+ p = p * r + 1.0 / 479001600.0;+ p = p * r + 1.0 / 39916800.0;+ p = p * r + 1.0 / 3628800.0;+ p = p * r + 1.0 / 362880.0;+ p = p * r + 1.0 / 40320.0;+ p = p * r + 1.0 / 5040.0;+ p = p * r + 1.0 / 720.0;+ p = p * r + 1.0 / 120.0;+ p = p * r + 1.0 / 24.0;+ p = p * r + 1.0 / 6.0;+ p = p * r + 0.5;+ p = p * r + 1.0;+ p = p * r + 1.0;+ /* 2^k in two halves, each a normal number: k runs from -1075 to 1024 */+ int k1 = k / 2;+ return p * pow2i(k1) * pow2i(k - k1);+}++double janas_log(double x)+{+ if (x != x || x < 0.0)+ return from_bits(0x7ff8000000000000ull);+ if (x == 0.0)+ return -from_bits(0x7ff0000000000000ull);+ uint64_t u = to_bits(x);+ if (u >= 0x7ff0000000000000ull)+ return x; /* infinity */+ int e = 0;+ if (u < 0x0010000000000000ull) { /* subnormal */+ u = to_bits(x * 0x1p54);+ e = -54;+ }+ e += (int)(u >> 52) - 1023;+ double m = from_bits((u & 0x000fffffffffffffull) | 0x3ff0000000000000ull);+ if (m > 1.4142135623730951) { /* m in [sqrt(1/2), sqrt(2)] */+ m *= 0.5;+ e++;+ }+ /* log m = 2 atanh(s), s = (m - 1)/(m + 1), |s| <= 0.1716 */+ double s = (m - 1.0) / (m + 1.0), s2 = s * s;+ double p = 1.0 / 23.0;+ p = p * s2 + 1.0 / 21.0;+ p = p * s2 + 1.0 / 19.0;+ p = p * s2 + 1.0 / 17.0;+ p = p * s2 + 1.0 / 15.0;+ p = p * s2 + 1.0 / 13.0;+ p = p * s2 + 1.0 / 11.0;+ p = p * s2 + 1.0 / 9.0;+ p = p * s2 + 1.0 / 7.0;+ p = p * s2 + 1.0 / 5.0;+ p = p * s2 + 1.0 / 3.0;+ double lm = 2.0 * s + 2.0 * s * s2 * p;+ double ed = (double)e;+ return ed * LN2_HI + (lm + ed * LN2_LO);+}++float janas_expf(float x)+{+ return (float)janas_exp((double)x);+}++float janas_softplusf(float x)+{+ double y = janas_exp((double)x);+ if (y < 0x1p-20) /* log(1 + y) = y - y^2/2 + y^3/3 - ... */+ return (float)(y - y * y * 0.5 + y * y * y * (1.0 / 3.0));+ return (float)janas_log(1.0 + y);+}++float janas_powf(float b, float t)+{+ return (float)janas_exp((double)t * janas_log((double)b));+}++/* sin r and cos r for |r| <= pi/4 */+static void sincos_kernel(double r, double *s, double *c)+{+ double r2 = r * r;+ double ps = -1.0 / 121645100408832000.0; /* -1/19! */+ ps = ps * r2 + 1.0 / 355687428096000.0;+ ps = ps * r2 - 1.0 / 1307674368000.0;+ ps = ps * r2 + 1.0 / 6227020800.0;+ ps = ps * r2 - 1.0 / 39916800.0;+ ps = ps * r2 + 1.0 / 362880.0;+ ps = ps * r2 - 1.0 / 5040.0;+ ps = ps * r2 + 1.0 / 120.0;+ ps = ps * r2 - 1.0 / 6.0;+ *s = r + r * r2 * ps;+ double pc = 1.0 / 2432902008176640000.0; /* 1/20! */+ pc = pc * r2 - 1.0 / 6402373705728000.0;+ pc = pc * r2 + 1.0 / 20922789888000.0;+ pc = pc * r2 - 1.0 / 87178291200.0;+ pc = pc * r2 + 1.0 / 479001600.0;+ pc = pc * r2 - 1.0 / 3628800.0;+ pc = pc * r2 + 1.0 / 40320.0;+ pc = pc * r2 - 1.0 / 720.0;+ pc = pc * r2 + 1.0 / 24.0;+ pc = pc * r2 - 0.5;+ *c = 1.0 + r2 * pc;+}++void janas_sincosf(float xf, float *s, float *c)+{+ double x = (double)xf;+ if (x != x || x - x != 0.0) { /* NaN or infinity */+ *s = *c = (float)(x - x);+ return;+ }+ /* whole turns first, by 2^20 quarter turns (2^18 turns), then quarter+ turns, with the leading part of pi/2 only: x float, |j| < 2^20 and+ |k| < 2^20 make both subtractions exact; the rest of pi/2 times all+ the quarter turns, m, is a small number (m * PIO2_2 under 0.04 for+ |x| up to 1e9) taken off last, in that order */+ double j = (x * (INV_PIO2 / 0x1p20) + ROUNDER) - ROUNDER;+ double y = x - j * (PIO2_1 * 0x1p20);+ double kd = (y * INV_PIO2 + ROUNDER) - ROUNDER;+ double m = j * 0x1p20 + kd;+ double r = ((y - kd * PIO2_1) - m * PIO2_2) - m * PIO2_2T;+ double sr, cr;+ sincos_kernel(r, &sr, &cr);+ switch ((int64_t)kd & 3) {+ case 0:+ *s = (float)sr;+ *c = (float)cr;+ break;+ case 1:+ *s = (float)cr;+ *c = (float)-sr;+ break;+ case 2:+ *s = (float)-sr;+ *c = (float)-cr;+ break;+ default:+ *s = (float)-cr;+ *c = (float)sr;+ break;+ }+}++float janas_tanhf(float xf)+{+ double x = (double)xf, a = x < 0 ? -x : x, t;+ if (x != x)+ return xf;+ if (a > 20.0)+ t = 1.0;+ else if (a < 0.0625) /* x - x^3/3 + 2x^5/15 - 17x^7/315 */+ t = a - a * a * a *+ (1.0 / 3.0 - a * a * (2.0 / 15.0 - a * a * (17.0 / 315.0)));+ else+ t = 1.0 - 2.0 / (janas_exp(2.0 * a) + 1.0);+ return (float)(x < 0 ? -t : t);+}diff --git a/src/llm/jmath.h b/src/llm/jmath.hnew file mode 100644index 0000000..90f8af4--- /dev/null+++ b/src/llm/jmath.h@@ -0,0 +1,41 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * jmath.h - the engine's own exp, log, pow, sin, cos and tanh (private).+ *+ * The C library's functions are not the same bits everywhere: glibc 2.41+ * rounds log1pf, tanhf and others correctly where 2.39 did not, and+ * Windows's are other code again. One unit in the last place of a rotary+ * angle or a DeltaNet decay is enough to turn a near tie between two+ * tokens the other way, and the answer at temperature 0 changed with the+ * machine's C library (issue #38). These are computed in double with plain+ * IEEE-754 operations only - add, subtract, multiply, divide, bit moves -+ * in a file of their own, compiled without FMA, so that every x86-64 and+ * arm64 machine and every compiler gives the same bits. The float functions+ * are the double result rounded once: within a unit in the last place,+ * nearly always the correctly rounded value.+ */+#ifndef JANAS_LLM_JMATH_H+#define JANAS_LLM_JMATH_H++/* e^x; 0 below -745.2, infinity above 709.8 */+double janas_exp(double x);++/* natural logarithm; -infinity at 0, NaN below 0 */+double janas_log(double x);++float janas_expf(float x);++/* log(1 + e^x), the softplus */+float janas_softplusf(float x);++/* b^t for b > 0 (the rotary frequencies) */+float janas_powf(float b, float t);++/* sin and cos of x together, for |x| up to about 1.7e12 (a million+ positions make angles up to 1e6) */+void janas_sincosf(float x, float *s, float *c);++float janas_tanhf(float x);++#endifdiff --git a/src/llm/model.c b/src/llm/model.cindex ae8a8b7..a0b566f 100644--- a/src/llm/model.c+++ b/src/llm/model.c@@ -46,6 +46,7 @@#include "deltanet.h"#include "tuner.h"#include "gpu.h"+#include "jmath.h"#include "matvec.h"#include "quant.h"#include "vmath.h"@@ -96,12 +97,12 @@ static void rope_table(float *cs, float *sn, uint32_t n, uint32_t pos0,uint32_t half = n_rot / 2;for (uint32_t j = 0; j < n; j++)for (uint32_t i = 0; i < half; i++) {- float theta =- (float)(pos0 + j) * powf(base, -2.0f * (float)i / (float)n_rot);+ float theta = (float)(pos0 + j) *+ janas_powf(base, -2.0f * (float)i / (float)n_rot);if (ff)theta /= ff[i];- cs[(size_t)j * half + i] = cosf(theta);- sn[(size_t)j * half + i] = sinf(theta);+ janas_sincosf(theta, &sn[(size_t)j * half + i],+ &cs[(size_t)j * half + i]);}}@@ -202,7 +203,7 @@ static void softcap_worker(void *arg, int tid, int n_threads)size_t per = (len + (size_t)n_threads - 1) / (size_t)n_threads;size_t a = (size_t)tid * per, b = a + per < len ? a + per : len;for (size_t i = a; i < b; i++)- l[i] = c * tanhf(l[i] / c);+ l[i] = c * janas_tanhf(l[i] / c);}/* A key row turned back by the angles in cs, sn (half pairs, NeoX) and put@@ -263,11 +264,10 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,const float *ff = swa ? NULL : m->rope_freqs;for (uint32_t i = 0; i < half; i++) {float theta =- (float)drop * powf(base, -2.0f * (float)i / (float)n_rot);+ (float)drop * janas_powf(base, -2.0f * (float)i / (float)n_rot);if (ff)theta /= ff[i];- cs[i] = cosf(theta);- sn[i] = sinf(theta);+ janas_sincosf(theta, &sn[i], &cs[i]);}size_t row = hd * elt;for (uint32_t h = 0; h < m->kv_heads[sl]; h++) {@@ -642,7 +642,7 @@ double janas_m_exp_sum(const float *l, uint32_t n, float mx)sum = exp_sum_avx2(l, n, mx, &i);#endiffor (; i < n; i++)- sum += expf(l[i] - mx);+ sum += janas_exp_det(l[i] - mx);return sum;}@@ -703,7 +703,7 @@ int janas_llm_mtp_draft(struct janas_llm_model *m, const int32_t *tokens,sum = exp_sum_avx2(l, m->dh_n, mx, &i);#endiffor (; i < m->dh_n; i++)- sum += expf(l[i] - mx);+ sum += janas_exp_det(l[i] - mx);*token = m->dh_ids[best];*conf = (float)(1.0 / sum);m->phase[JANAS_PH_OUTPUT] += now() - td;diff --git a/src/llm/model_assist.c b/src/llm/model_assist.cindex 3472398..1ce185d 100644--- a/src/llm/model_assist.c+++ b/src/llm/model_assist.c@@ -25,6 +25,7 @@#include <string.h>#include <unistd.h>+#include "jmath.h"#include "matvec.h"#include "vmath.h"@@ -360,11 +361,11 @@ static void rope_at(float *cs, float *sn, uint32_t pos, uint32_t n_rot,float base, const float *ff){for (uint32_t i = 0; i < n_rot / 2; i++) {- float theta = (float)pos * powf(base, -2.0f * (float)i / (float)n_rot);+ float theta =+ (float)pos * janas_powf(base, -2.0f * (float)i / (float)n_rot);if (ff)theta /= ff[i];- cs[i] = cosf(theta);- sn[i] = sinf(theta);+ janas_sincosf(theta, &sn[i], &cs[i]);}}diff --git a/src/llm/model_ffn.c b/src/llm/model_ffn.cindex e15b419..50477e4 100644--- a/src/llm/model_ffn.c+++ b/src/llm/model_ffn.c@@ -368,7 +368,7 @@ static void route_tok(struct janas_llm_model *m, uint32_t j)if (r[e] > mx)mx = r[e];for (uint32_t e = 0; e < ne; e++) {- r[e] = expf(r[e] - mx);+ r[e] = janas_expf(r[e] - mx);sum += r[e];}float wsum = 0.0f;diff --git a/src/llm/model_impl.h b/src/llm/model_impl.hindex 879a083..1ee4cb8 100644--- a/src/llm/model_impl.h+++ b/src/llm/model_impl.h@@ -20,6 +20,7 @@#include "gpu.h"#include "gpu_token.h"#include "jns.h"+#include "jmath.h"#include "quant.h"#include "tuner.h"@@ -365,7 +366,7 @@ static inline void rms_norm(float *out, const float *x, const float *w,static inline float sigmoid(float x){- return 1.0f / (1.0f + expf(-x));+ return 1.0f / (1.0f + janas_expf(-x));}/*diff --git a/src/llm/model_rec.c b/src/llm/model_rec.cindex d421380..5b5c62d 100644--- a/src/llm/model_rec.c+++ b/src/llm/model_rec.c@@ -58,7 +58,7 @@ static void conv_head_ref(struct janas_llm_model *m, const float *wt,float s = 0.0f;for (uint32_t i = 0; i < dc; i++)s += win[i] * wt[(size_t)i * cc + c];- m->conv_out[(size_t)t * cc + c] = s / (1.0f + expf(-s));+ m->conv_out[(size_t)t * cc + c] = s / (1.0f + janas_expf(-s));for (uint32_t i = 0; i + 1 < dc; i++)win[i] = win[i + 1];}@@ -239,7 +239,7 @@ static void gate_worker(void *arg, int tid, int n_threads)#endifrms_norm(o, o, w, ds, m->eps);for (uint32_t i = 0; i < ds; i++)- o[i] *= z[i] / (1.0f + expf(-z[i]));+ o[i] *= z[i] / (1.0f + janas_expf(-z[i]));}}@@ -345,9 +345,9 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,memcpy(mx, src, (size_t)cc * sizeof(float));for (uint32_t h = 0; h < nv; h++) {float al = b[nv + h] + dt[h];- float sp = al > 20.0f ? al : log1pf(expf(al));+ float sp = al > 20.0f ? al : janas_softplusf(al);m->beta[(size_t)(a + j) * nv + h] = sigmoid(b[h]);- m->decay[(size_t)(a + j) * nv + h] = expf(sp * A[h]);+ m->decay[(size_t)(a + j) * nv + h] = janas_expf(sp * A[h]);}continue;}@@ -364,9 +364,9 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,for (uint32_t r = 0; r < rf; r++) {uint32_t h = g * rf + r;float al = b[2 * g * rf + rf + r] + dt[h];- float sp = al > 20.0f ? al : log1pf(expf(al));+ float sp = al > 20.0f ? al : janas_softplusf(al);m->beta[(size_t)(a + j) * nv + h] = sigmoid(b[2 * g * rf + r]);- m->decay[(size_t)(a + j) * nv + h] = expf(sp * A[h]);+ m->decay[(size_t)(a + j) * nv + h] = janas_expf(sp * A[h]);}}}diff --git a/src/llm/sample.c b/src/llm/sample.cindex 2d02f38..b59dbbb 100644--- a/src/llm/sample.c+++ b/src/llm/sample.c@@ -9,6 +9,8 @@#include <stdlib.h>#include <string.h>+#include "jmath.h"+struct cand {float logit;int32_t id;@@ -263,8 +265,8 @@ static void learn(struct janas_sampler *s, const float *lg, int32_t t,float mx = lg[argmax(lg, s->nv)];double sum = 0;for (uint32_t i = 0; i < s->nv; i++)- sum += exp((double)(lg[i] - mx));- float lse = mx + (float)log(sum);+ sum += janas_exp((double)(lg[i] - mx));+ float lse = mx + (float)janas_log(sum);lp->logprob = lg[t] - lse;lp->n_top = 0;if (s->p.logprobs > 0) {@@ -308,7 +310,8 @@ static int32_t draw(struct janas_sampler *s, const float *lg, int filtered)double sum = 0;for (uint32_t i = 0; i < nc; i++) {/* the probability, unnormalized, in place of the logit */- s->cands[i].logit = expf((s->cands[i].logit - top) / o->temperature);+ s->cands[i].logit =+ janas_expf((s->cands[i].logit - top) / o->temperature);sum += s->cands[i].logit;}if (o->top_p > 0 && o->top_p < 1) {diff --git a/tests/test_jmath.c b/tests/test_jmath.cnew file mode 100644index 0000000..2c5126e--- /dev/null+++ b/tests/test_jmath.c@@ -0,0 +1,134 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_jmath.c - the engine's own exp, log, pow, sin, cos and tanh against+ * the C library's double functions, rounded to float: within one unit in+ * the last place over the ranges the engine uses them on (the softmax and+ * sigmoid arguments, the DeltaNet's softplus, the rotary frequencies and+ * angles up to a million positions, the soft cap), and the double exp and+ * log within a few units of the double's last place.+ */+#include <math.h>+#include <stdint.h>+#include <stdio.h>+#include <string.h>+#include <unistd.h>++#include "llm/jmath.h"++static int failures;++#define CHECK(c, ...) \+ do { \+ if (!(c)) { \+ if (failures < 20) { \+ printf(__VA_ARGS__); \+ printf("\n"); \+ } \+ failures++; \+ } \+ } while (0)++/* distance in units in the last place of two finite floats */+static int64_t ulps(float a, float b)+{+ int32_t ia, ib;+ memcpy(&ia, &a, 4);+ memcpy(&ib, &b, 4);+ if (ia < 0)+ ia = INT32_MIN - ia;+ if (ib < 0)+ ib = INT32_MIN - ib;+ int64_t d = (int64_t)ia - (int64_t)ib;+ return d < 0 ? -d : d;+}++/* one ulp, or an absolute 1e-12 where the result is near zero (a sine+ at a multiple of pi: the reference has no more digits there either) */+static int close_enough(float got, double want)+{+ float w = (float)want;+ return ulps(got, w) <= 1 || fabs((double)got - want) <= 1e-12;+}++int main(void)+{+ alarm(120);+ int64_t worst;++ worst = 0;+ for (int i = -1030000; i <= 885000; i++) {+ float x = (float)i * 1e-4f;+ float got = janas_expf(x);+ CHECK(close_enough(got, exp((double)x)), "expf(%.9g) = %.9g, want %.9g",+ x, got, exp((double)x));+ int64_t u = ulps(got, (float)exp((double)x));+ if (u > worst)+ worst = u;+ }+ CHECK(janas_expf(-200.0f) == 0.0f && isinf(janas_expf(200.0f)),+ "expf out of range");++ for (int i = -100000; i <= 100000; i++) {+ double x = (double)i * 7.1e-3;+ double got = janas_exp(x), want = exp(x);+ /* an infinity must be one, a subnormal (below e^-708, where the+ engine never goes) is rounded twice: a few of its units */+ CHECK(isinf(want) ? got == want+ : fabs(got - want) <= 4e-16 * want + 1e-321,+ "exp(%.17g) = %.17g, want %.17g", x, got, want);+ }+ for (int i = 1; i <= 200000; i++) {+ double x = (double)i * (double)i * 1.37e-3;+ double got = janas_log(x), want = log(x);+ CHECK(fabs(got - want) <= 4e-16 * fabs(want) + 1e-300,+ "log(%.17g) = %.17g, want %.17g", x, got, want);+ }+ CHECK(fabs(janas_log(1e-310) - log(1e-310)) <= 1e-13, "log of a subnormal");+ CHECK(janas_log(1.0) == 0.0, "log(1) = %.17g", janas_log(1.0));++ for (int i = -1000000; i <= 200000; i++) {+ float x = (float)i * 1e-4f;+ double want = log1p(exp((double)x));+ CHECK(close_enough(janas_softplusf(x), want),+ "softplus(%.9g) = %.9g, want %.9g", x, janas_softplusf(x), want);+ }++ static const float bases[] = {10000.0f, 500000.0f, 1000000.0f, 5000000.0f,+ 10000000.0f};+ for (int b = 0; b < 5; b++)+ for (int i = 0; i < 512; i++) {+ float t = -2.0f * (float)i / 512.0f;+ double want = pow((double)bases[b], (double)t);+ CHECK(close_enough(janas_powf(bases[b], t), want),+ "pow(%.9g, %.9g) = %.9g, want %.9g", bases[b], t,+ janas_powf(bases[b], t), want);+ }++ for (int i = 0; i < 2000000; i++) {+ /* angles as the rotary tables make them, and some far larger */+ float x =+ i < 1000000 ? (float)i * 1.0001f : (float)(i - 1000000) * 997.13f;+ if (i & 1)+ x = -x;+ float s, c;+ janas_sincosf(x, &s, &c);+ CHECK(close_enough(s, sin((double)x)), "sin(%.9g) = %.9g, want %.9g", x,+ s, sin((double)x));+ CHECK(close_enough(c, cos((double)x)), "cos(%.9g) = %.9g, want %.9g", x,+ c, cos((double)x));+ }+ for (int i = -60000; i <= 60000; i++) {+ float x = (float)i * 5e-4f;+ CHECK(close_enough(janas_tanhf(x), tanh((double)x)),+ "tanh(%.9g) = %.9g, want %.9g", x, janas_tanhf(x),+ tanh((double)x));+ }++ if (failures) {+ printf("test_jmath: %d failures\n", failures);+ return 1;+ }+ printf("test_jmath: ok (expf within %lld ulp)\n", (long long)worst);+ return 0;+}diff --git a/tests/test_quant.c b/tests/test_quant.cindex 5f1182a..5d96b3f 100644--- a/tests/test_quant.c+++ b/tests/test_quant.c@@ -479,7 +479,7 @@ det8(const float *g, const float *u, float *h, int n)static void test_det(void){float g[1024], u[1024], a[1024], b[1024];- int bad = 0;+ int bad = 0, compared = 0;for (int round = 0; round < 1000; round++) {for (int i = 0; i < 1024; i++) {/* wide range, and the clamp edges */@@ -495,10 +495,18 @@ static void test_det(void)det8(g, u, b, 1024);for (int i = 0; i < 1024; i++)bad += memcmp(&a[i], &b[i], 4) != 0;+ compared += 1024;}#endif}- CHECK(bad == 0, "silu_mul_det: %d scalar/AVX2 differences", bad);+ /* without AVX2 and FMA there is nothing to compare it with: say so,+ rather than pass as if 1000 rounds had agreed (issue #26) */+ if (compared)+ printf("silu_mul_det: scalar against AVX2 on %d values\n", compared);+ else+ printf("silu_mul_det: no AVX2 with FMA here, scalar not compared\n");+ CHECK(bad == 0, "silu_mul_det: %d scalar/AVX2 differences of %d", bad,+ compared);double worst = 0;for (int i = 0; i < 100000; i++) {float d = ldexpf(rndf(1.0f, 2.0f), (int)(rnd() % 200) - 100);diff --git a/tools/janas-try.sh b/tools/janas-try.shindex 9adb06f..f844edc 100755--- a/tools/janas-try.sh+++ b/tools/janas-try.sh@@ -133,16 +133,19 @@ one 5476083302 11811160064 8" ;;# machine with less memory would compute other numbers, rightly. And so is# the prompt: no MCP servers, no saved settings (see level()).FP_PROMPT="Write a short paragraph about the island of Sardinia, its history and its landscape."-# medium's since 27 Sep 2026: its Q8_0 tensors take the activations with a-# scale per 32 since then (13e2f198c270b826 before), checked again on 14 Oct+# since 17 Oct 2026 the engine computes exp, log, pow, sin, cos and tanh+# itself and no compiler fuses a multiply and an add on its own: the C+# library and the compiler no longer change the answer (issue #38: medium+# came out otherwise with glibc 2.39 and gcc 13). medium's and gemma's+# changed with it (e49b0016cedf6c50 and b2c01def5ff6bd5c before), checked# with the GPU, without it and with the AVX2 kernels forcedfp_expected(){case "$1" inquick) echo "c08f0f936eeba5cb" ;;- medium) echo "e49b0016cedf6c50" ;;+ medium) echo "13e2f198c270b826" ;;full) echo "4aae064b918a0e75" ;;- gemma) echo "b2c01def5ff6bd5c" ;;+ gemma) echo "bf1626afffa7eeab" ;;esac}