3806 Commits
Author SHA1 Message Date
Undefine 7d522782c2 AK: Hide non fallible ByteBuffer and StringBuilder APIs in the kernel
This follows what we already do in structure like the Vector.
2026-11-06 20:34:06 +01:00
Undefine 9ae1edc095 AK: Improve error propagation in Stream code 2026-11-06 20:34:06 +01:00
Undefine 4160b111f3 AK: Explicitly ensure capacity in Hex and UUID code when in kernel
The implicit ensure capacity will be unavailable in kernel.
2026-11-06 20:34:06 +01:00
Undefine 8c2cef2490 AK: Make StringBuilder::trim use trim and not resize
The ByteBuffer trim function is not fallible but resize is, use trim to
keep this function from returning errors.
2026-11-06 20:34:06 +01:00
Undefine 6c5c413108 AK: Use more of the fallible StringBuilder APIs in formatting code
Unfortunately in some places MUST is used because the whole code really
doesn't handle errors at all and uses MUST all over the place.
2026-11-06 20:34:06 +01:00
Undefine 147b06ad2f AK: Add ByteBuffer::clone
This adds a function to clone a ByteBuffer with a way to propagate
errors if they occur.
2026-11-06 20:34:06 +01:00
UndefineandRatcheT2497 95ab8a7e1a LibACPI: Add a skeleton of a AML interpreter
This a simple skeleton for a non recursive AML parser + interpreter.
For now it support very little stuff, only a couple of opcodes but that
is enough to load a very very simple AML table.

Co-authored-by: RatcheT2497 <[email protected]>
2026-10-28 18:13:04 +01:00
Lucas Chollet 5184c07e01 AK/Math: Rely on lgamma to implement gamma
Note that the current implementation of lgamma doesn't work for negative
values of x. But given that Stirling's approximation is also only
defined for positive values of x, this patch don't change anything in
that regard.
2026-10-14 22:19:04 +02:00
Lucas Chollet b883cce6a2 AK/Math: Consider the long double == f64 case in tgamma's integer path
This code still looks weird and don't support f128, but should at least
be more correct.
2026-10-14 22:19:04 +02:00
Lucas Chollet 15a07a1073 LibC+AK: Move tgamma definition from LibC to AK
This makes the tgamma function accessible from constexpr contexts and
allows us to test it in CompareAKMathAgainstMPFR.
2026-10-14 22:19:04 +02:00
Lucas Chollet bfa440bbf8 AK: Define FloatExtractor for long double when it's an alias of double
On some platforms, like macOS on ARM, long double is a different type
but has the exact same characteristics as a normal double. When it is
the case, define FloatExtractor<long double> as an alias to
FloatExtractor<double>.
2026-10-14 22:19:04 +02:00
Lucas Chollet 933c0066ca AK/Math: Make lgamma a lot more precise on the (0, 8) range
Max ULP                Mean ULP
Before:              inf    998274894113382.2500
After:        7352987.00                 18.7027

While this is already a lot better, the error is still significant. This
is mainly due to the areas around the roots of lgamma(x) (x=1 and x=2)
where the results are close to zero and thus requires tiny absolute
errors. On (0, 0.7] ∪ [2.3, 8) the maximum error is 20 ULP.

This introduces a lot of polynomials, but I don't think we have another
choice here (CORE-MATH uses even more on the same interval). To find the
subdivisions, I simply searched for x so that a degree 12 polynomial
would have a small error (~1e-18) on [x, 8] and then repeated the
process until the full [0, 8] interval was filled.
2026-10-12 18:39:50 +02:00
Lucas Chollet 47685800ab AK/Math: Make lgamma a lot more precise on [8, 1e7)
This is done by using a polynomial to compensate the mathematical error
term of the stirling approximation.

                 Max ULP                Mean ULP
Before: 6697252309907.00       725249701724.2207
After:              2.00                  0.4674
2026-10-12 18:39:50 +02:00
Lucas Chollet 8b823af638 AK/Math: Add a special path for x >= 1e7 in lgamma 2026-10-12 18:39:50 +02:00
Lucas Chollet 606b5fcaa8 AK/Math: Use a more precise stirling approximation in lgamma
This directly yields to better results for values of x > 2.
2026-10-12 18:39:50 +02:00
Lucas Chollet d0d79c2bcb AK/Math: Make lgamma_r's sign computation more sound
This still doesn't always produce the correct result for negative values
of x, but at least this fixes the reasoning.
2026-10-12 18:39:50 +02:00
Lucas Chollet 101c3c50fa AK/Math: Add a comment that describes how to better implement lgamma
And reshape lgamma to look a bit more like what's described in the
comment.
2026-10-12 18:39:50 +02:00
Lucas Chollet f788e89ffb AK/Math: Introduce a lgamma function and implement lgamma_r on top of it
Non-functional change for lgamma_r users but will allow future user of
lgamma to not pay the (low) price of the sign computation.
2026-10-12 18:39:50 +02:00
Lucas Chollet 5b636c2d1d AK+LibC: Move lgamma_r definition from LibC to AK
This allows us to have a constexpr lgamma_r function and also to test it
with CompareAKMathAgainstMPFR.
2026-10-12 18:39:50 +02:00
Lucas Chollet 30fddf9f18 AK/Math: Improve the precision of cosh and sinh
Taking the square of e^x quickly resulted in overflows with the old
formula. See the PR description for benchmark results.
2026-09-27 22:09:41 +02:00
Jamie Mansfield 62aeff0410 LibWeb/WebAssembly: Use a debug flag for dbgln calls
This prevents the horrendous console spam when functions are resolved,
e.g. on the Royal Albert Hall website.

(cherry picked from commit 48366ddddf3e1231abf116fc38d9b56b3c259229)
2026-09-21 10:32:03 +02:00
Lucas Chollet 6f12afa77b AK: Add basic support %a formatting
No modifiers are currently supported.
2026-09-20 22:06:41 +02:00
Lucas Chollet 8243759bb2 AK: Apply modifiers when printing nan or inf
Before this patch, we would incorrectly handle the '+' (always_sign),
'-' (left padding) and padding size modifiers.
2026-09-20 22:06:41 +02:00
Lucas Chollet 47ce79685f AK: Extract a print_nan_and_inf function 2026-09-20 22:06:41 +02:00
Sönke Holz 8f8c61b70d AK: Silence false positive -Wstringop-overflow GCC warning in Vector
This silences the following warning when building lagom with recent GCC
versions:
"error: writing 1 byte into a region of size 0
[-Werror=stringop-overflow=]"

It only seems to be reported when this function is inlined into
GIFLoader's usage of LzwDecompressor::get_output().
2026-09-19 11:26:44 +02:00
Timothy FlynnandJamie Mansfield bff7eccf6b AK: Add an option to the base64 encoder to omit padding
Will be used by an upcoming JS prototype

(cherry picked from commit 41e14e3fc3aab8417b8687df7e4242982e97ba23)

Co-authored-by: Jamie Mansfield <[email protected]>
2026-09-18 20:04:39 -04:00
Jamie Mansfield 651f72cab4 AK: Support decoding base64 strings without padding
This is to support future changes that will be merged from Ladybird,
which gained this functionality through moving to simdutf.
2026-09-18 20:04:39 -04:00
Lucas Chollet 8492696450 AK: Handle edge cases correctly in exp 2026-08-31 19:39:31 +02:00
Lucas Chollet 184041fca2 AK: Handle edge cases correctly in cosh 2026-08-31 19:39:31 +02:00
Lucas Chollet 35b6cf0043 AK: Handle edge cases correctly in sinh 2026-08-31 19:39:31 +02:00
Lucas Chollet ca8c7e8e3b AK: Handle edge cases correctly in tanh 2026-08-31 19:39:31 +02:00
Lucas Chollet 0edf35889a AK+printf: Add support for formatting long double
This shares the same implementation as the double formatter, so it has
all the same limitations :^)
2026-08-30 21:52:53 +02:00
Lucas Chollet a36d1178c2 AK: Handle edge cases correctly in hypot 2026-08-29 17:18:05 +02:00
Lucas Chollet f4bde6ab14 Everywhere: Fix -Wunused-template warnings emited by recent Clang
The latest version of Clang got better at detecting unused templates. It
detected one truly unused template in our codebase. The rest are either
from templated functions that are not always used (depending on the
compile-time configuration) or from templated functions that are
incorrectly annotated with static.
2026-08-22 21:48:49 +02:00
Sönke Holz 20295abc11 AK: Allow constructing MACAdress from an Array 2026-08-16 16:57:03 +02:00
Sönke Holz 006de308b5 AK+DynamicLoader: Move optimizer_fence() into AK
This will allow us to use this function in other places.
2026-08-16 16:57:03 +02:00
Sönke Holz 1a3eb40582 AK: Put fence functions in the global namespace
In some places, we already didn't use an "AK::" prefix, most likely due
to ADL. Instead of having to rely on ADL, let's just make this explicit.
2026-08-16 16:57:03 +02:00
Sönke Holz 1320a0512a AK: Move fence functions to AK/Fences.h 2026-08-16 16:57:03 +02:00
Hendiadyoin1 d11a28576d AK: Make the Variant constructor use proper instantiation patterns
Previously we inherited from a set of constructors, which casted
themselves to the final Variant type to initialize the wanted storage,
which is very UB.
Now we choose the best overload by checking for which Variant member <T>
we can call a function taking a <T>, while passing in a fully qualified
argument <U>, as passed into the Variant constructor, essentially
emulating the normal constructor lookup.
2026-08-15 08:04:07 +02:00
Hendiadyoin1 4ce1dfe6dc AK: Use Variants data accessors in the VisitImpl
By just accessing the data in the Variant through the normal means, we
avoid a bunch of dubious casts, and need to pass less info directly.
2026-08-15 08:04:07 +02:00
Hendiadyoin1 7c2776b344 AK: Make Variant's type index getter non recursive
Also remove users of the old recursive version, which were mostly
redundantly checking the index again.
The remaining users have been renamed to be a bit more descriptive.
2026-08-15 08:04:07 +02:00
Hendiadyoin1 7e820f2beb AK: Simplify Variant::can_contain
No need to look up the index, we can directly check with `IsOneOf`
2026-08-15 08:04:07 +02:00
Lucas Chollet 69df83dcf7 AK: Include StdShim.h in NeverDestroyed.h
This provides the otherwise missing definition for `forward`.
2026-06-26 23:07:35 +02:00
Nico Weber c5af100c9e Everywhere: Use NaN<T> instead of __builtin_nan{f,,l}("")
No behavior change.
2026-06-26 13:15:59 +02:00
Nico Weber 38c474314f AK: Do not use recursion in asin()
Takes asinf() from 432.0M/s to 717.6M/s in `./bench-float-exhaustive`.
Takes asin() from 108.0M/s to 189.4M/s in `./ak-math-bench`.

(For reference, libc++ at 259.1M/s for asinf,
and macOS system libm is at 225.6M/s for asin.)

No behavior change.
2026-06-21 17:52:25 -04:00
Nico Weber a826321e87 AK: Replace asin() implementation with a lolremez-based one
Before:

float:

    % ./bench-float-exhaustive -f acosf -f asinf
    acosf  max ULP: 1267.00  mean ULP: 0.039223  CR: 96.1%  FR: 100.0%
           AK: 218.9M/s  ref: 211.2M/s
    asinf  max ULP: 8.00     mean ULP: 0.010256  CR: 99.1%  FR:  99.9%
           AK: 224.3M/s  ref: 259.1M/s

double:

    % ./ak-math-bench --format=wide -f acos -f asin
    Function     Max ULP       Mean ULP   % CR   % FR  Accuracy  Ops/sec
    --------------------------------------------------------------------
    acos()  184318822.00   2465877.8443  58.7%  82.7%       0.0    91.4M
    asin()  390846002.00  11969221.1695  51.3%  83.0%       0.0   113.6M

After:

float:

    % ./bench-float-exhaustive -f asinf -f acosf
    acosf  max ULP: 1267.00  mean ULP: 0.038492  CR: 96.2%  FR: 100.0%
           AK: 393.7M/s  ref: 211.2M/s  (2.3s)
    asinf  max ULP: 2.00     mean ULP: 0.002522  CR: 99.8%  FR: 100.0%
           AK: 432.0M/s  ref: 259.1M/s

double:

    % ./ak-math-bench -f acos -f asin
    Function     Max ULP    Mean ULP    % CR    % FR  Accuracy   Ops/sec
    --------------------------------------------------------------------
    acos()   25216051.00  20936.4458   70.3%   98.7%       0.0     88.9M
    asin()          1.00      0.0791   92.1%  100.0%      92.7    108.0M

With this, asin() is somewhat decent in accuracy, and the float version
is even pretty fast.

For reference, the double version of asin with macOS system libm:

    asin()          1.00      0.0318   96.8%  100.0%      96.9    225.6M

That is about twice as accurate and about twice as fast as ours.
2026-06-21 17:52:25 -04:00
Nico Weber e897b9d744 Everywhere: Use more granular AK/Math/ includes in a few headers
...and in a few cpp files too, and clean up missing includes.

Reduces number of files recompiled after touching AK/Math/Hyperbolic.h
from a couple thousand to a handful.

No behavior change.
2026-06-21 15:26:06 -04:00
Nico Weber c717b91897 AK: Granularize AK/Math.h
No behavior change.
2026-06-21 15:26:06 -04:00
Nico Weber 40b60fa7a5 AK: Make log{,2,10}(inf) return inf
Previously, e.g. log2(inf) = 0x1p+7, which was wrong.
2026-06-21 10:11:57 -04:00
Nico Weber 7a627a8f89 AK: Improve atan(), atan2() for doubles
Before:

    % ./ak-math-bench -v  --format=wide -f atan -f atan2
    Running libm benchmark...

    Function   Max ULP  Mean ULP    % CR Accuracy  Ops/sec
    ------------------------------------------------------
    atan()      194.00    6.7815   57.0%     12.9   180.0M
                   ^ input: 0x1.bf40883ce64c4p-4
                  expected: 0x1.bd7cb69f7eba8p-4
                       got: 0x1.bd7cb69f7ec6ap-4
    atan2()     193.00   15.4890    8.7%      6.1    89.2M
                   ^ input: 0x1.a9bbbb530b80ap+6, 0x1.e069d3ea5d36ap+9
                  expected: 0x1.c3e1ec33ee4ecp-4
                       got: 0x1.c3e1ec33ee5adp-4
    ======================================================

    Overall Score: 11.4

After:

    % ./ak-math-bench -v  --format=wide -f atan -f atan2
    Running libm benchmark...

    Function   Max ULP  Mean ULP    % CR Accuracy  Ops/sec
    ------------------------------------------------------
    atan()        1.00    0.2584   74.2%     79.5   176.7M
                   ^ input: 0x1.00e99e0bbf8d4p+7
                  expected: 0x1.9021897ae0934p+0
                       got: 0x1.9021897ae0933p+0
    atan2()       2.00    0.3205   68.2%     75.7    84.8M
                   ^ input: 0x1.9f673c81fbd1cp+51, 0x1.17c16d619e0dep+51
                  expected: 0x1.f4caa48ff62dap-1
                       got: 0x1.f4caa48ff62d8p-1
    ======================================================

    Overall Score: 80.2

The float version is already decent:

    % ./bench-float-exhaustive -f atanf -f atan2f
    atanf   max ULP: 2.00  mean ULP: 0.045692 CR: 95.4%
            AK: 316.4M/s  ref: 382.8M/s
               worst: atanf(0x1.2c00c2p+0)
                        = 0x1.ba8f64p-1,
                 expected 0x1.ba8f6p-1
    atan2f  max ULP: 2.00  mean ULP: 0.060975 CR: 93.9%
            AK: 154.6M/s  ref: 271.3M/s
               worst: atan2f(0x1.514008p-128, 0x1.500054p-125)
                        = 0x1.ff3b52p-4,
                 expected 0x1.ff3b4ep-4
2026-06-20 17:40:06 -04:00