Commit graph

3631 commits

Author SHA1 Message Date
Pavlo Ivashkov
31daabcf0a AK: Improve performance of u64 randoms 2025-01-13 09:08:55 -05:00
Ali Mohammad Pur
df5640785b AK: Add Vector::get(index) convenience function
This is similar to HashMap::get(key), which returns an Optional, empty
if the index is out of bounds for the vector.

(cherry picked from commit 44798f44ef1c112aa9dbee7abee7ff6a5568c4f4)
2025-01-02 13:35:26 -05:00
Nico Weber
7f84a08012 AK: Remove commented-out code in fmod()
I hadn't meant to commit that. No behavior change.
2025-01-02 09:56:31 -05:00
Nico Weber
48a28cffd5 AK: Make FloatExtractor use bit_cast<>() instead of a union
The motivation is to allow functions that use FloatExtractor to be
constexpr.  Type punning through a union will never work in constexpr.

In practice, bit_cast<>()ing bit fields also does not yet work in clang,
but that's just a bug and it will work eventually (and it does already
work in gcc): https://github.com/llvm/llvm-project/issues/54018

No behavior change.
2025-01-02 09:56:31 -05:00
Sönke Holz
7467cf35bb AK: Implement rint() on RISC-V
The implementation is copy-pasted from LibC.
2024-12-30 14:46:27 -05:00
Sönke Holz
3bea78fc30 AK: Implement sqrt() on RISC-V 2024-12-30 14:46:27 -05:00
Nico Weber
17622bf0d0 AK: Add a better implementation of tan() on non-x86 2024-12-25 19:04:22 -05:00
Nico Weber
5152950ffa AK: Implement atan() in terms of asin() on non-x86 2024-12-25 19:04:22 -05:00
Nico Weber
f79ad30feb AK: Reduce > 0.5 arm of asin() to asin() instead of to atan() 2024-12-25 19:04:22 -05:00
Nico Weber
dcf88d9461 AK: Move atan() below asin()
Pure code move, no behavior change.
2024-12-25 19:04:22 -05:00
Nico Weber
69b2b86b29 AK: Implement atan2() in terms of atan() on non-x86
atan() isn't implemented yet on non-x86, so this isn't super useful yet.
But it does handle a couple corner cases correctly already, and those
are even enough to make all the atan2 tests in Tests/LibC/TestMath.cpp
pass 🙃
2024-12-24 13:11:07 -05:00
Nico Weber
982c8cc7fb AK+LibC: Move generic copysign() into AK 2024-12-24 13:11:07 -05:00
Nico Weber
d4f037cf0f AK: Make sin() and cos() a bit better
With this, sin() and cos() are fairly close to the correct values
for all x. It's neither fast nor very accurate, but much better than
what we had before.
2024-12-24 13:11:07 -05:00
Nico Weber
8756244d66 AK: Make sin() and cos() periodic
Now that we have fmod(), we can implement some symmetries for
sin() and cos(). The core approximation is still very simplistic,
but now it only has to approximate in [0, Pi/2]. So while its error
is still high, it's no longer unbounded.
2024-12-24 13:11:07 -05:00
Nico Weber
085dc27bdf AK: Add an implementation of fmod() on non-x86
This is a pretty naive implementation, but it mostly works.
2024-12-24 13:11:07 -05:00
Nico Weber
d6bdb9a3ed AK: Add implementations of exp2() and exp() on non-x86
This is a pretty naive implementation of exp2() that can be improved
a lot, but hey, it beats the sin() and cos() implementation on non-x86.

It also implements exp(x) as exp2(x * log2(e)), with the same
disclaimer.
2024-12-23 08:42:48 -05:00
Nico Weber
1edd0cd8e0 AK: Reorder exp() and exp2() in Math
No behavior change, just moving one function after the other,
to make an upcoming diff easier to read.
2024-12-23 08:42:48 -05:00
Nico Weber
e4c36876d2 AK: Make Function a little bit constexpr
Just enough to make it possible to pass a lambda to a constexpr function
taking an AK::Function parameter and have that constexpr function call
the passed-in AK::Function.

The main thing is that bit_cast<>s of pointers aren't ok in constexpr
functions, and neither is placement new. So add a union with stronger
typing for the constexpr case.

The body of the `if (is_constexpr_evaluated())` in
`init_with_callable()` is identical to the `if constexpr` right
after it. But `if constexpr (is_constexpr_evaluated())` always
evaluates `is_constexpr_evaluated()` in a constexpr context, so
this can't just add ` || is_constexpr_evaluated()` to that
`if constexpr`.
2024-12-20 12:05:06 -05:00
Nico Weber
761320c4d2 AK: Make parts of Atomic constexpr
These new constexpr functions are for the most part identical to
the same non-constexpr functions, except they drop the `volatile`
qualifier on `this`.

`fetch_add()`, `fetch_sub()`, `load()` have explicit
`is_constant_evaluated()` branches that don't call (non-constexpr)
atomic built-ins. Off the constexpr path, they forward to the
volatile functions.
2024-12-20 12:05:06 -05:00
Sönke Holz
ddd9ab21e6 LibDeviceTree: Add functions for getting and translating addresses
I created this test file by running the following command:

    dtc -o address-translation.dtb <<EOF
    /dts-v1/;

    / {
        compatible = "serenity,address-translation-test";
        #address-cells = <2>;
        #size-cells = <1>;

        soc {
            compatible = "simple-bus";
            #address-cells = <1>;
            #size-cells = <1>;
            ranges = <0xa0000000 0xfe 0xd0000000 0x40000000>;

            usb@a0010000 {
                reg = <0xa0010000 0x100000>;
            };

            some-bus@b0000000 {
                compatible = "simple-bus";
                #address-cells = <2>;
                #size-cells = <2>;
                ranges = <0x02 0x00 0xb0000000 0x00 0x200000>;

                leds@200100000 {
                    reg = <0x02 0x100000 0x00 0x1000>;
                };
            };
        };
    };
    EOF
2024-12-10 16:25:46 +01:00
Pavel Shliak
4f6f65d1f9 AK: Fix InsertionSort to respect specified bounds
Previously, it ignored 'start', sorting from the array's
beginning. This caused unintended changes and slower
performance. Fix ensures sorting stays within 'start'
and 'end' indices only.

(cherry picked from commit 356016c626546aeb51722fe9ca5bb6a769bfe762)
2024-12-08 13:24:15 -05:00
Sönke Holz
240db03a27 AK: Make to_array work with zero-sized arrays
The other overload doesn't work for zero-sized arrays.

Co-authored-by: Timothy Flynn <trflynn89@pm.me>
2024-12-07 16:56:12 +01:00
Andreas Kling
073b3b992d AK: Make String::number() infallible
This API will always succeed in creating a String representing the
provided number in base-10.

(cherry picked from commit dd419b5a8df3b9a32478c4a8f0ea9f70334214cd;
amended to update the rest of the system. No conflicts though!)
2024-11-29 16:03:30 -05:00
Gingeh
a630c67a32 AK: Avoid returning null StringViews instead of empty views
This was error-prone and most users were just checking the length anyway

(cherry picked from commit 57ba720fb184ddf59fb09d3290e07cd425be450e)
2024-11-25 09:21:14 -05:00
Nico Weber
b00cfb54e8 AK: Don't define IsFloatingPoint and the FloatingPoint concept in KERNEL
We build KERNEL without floats, so that context switches don't have
to save float registers. As a side effect of that, _Float16 results
in a "_Float16 is not supported on this target" diagnostic.

Having IsFloatingPoint<> be false for the upcoming f16 type but
true for other floating point types seems strange, so just stop
having these concepts in kernel code altogether.
2024-11-19 19:20:44 -05:00
Andreas Kling
e1ba881587 AK+LibWeb: Add {Fly,}String::to_ascii_{upper,lower}_case()
These don't have to worry about the input not being valid UTF-8 and
so can be infallible (and can even return self if no changes needed.)

We use this instead of Infra::to_ascii_{upper,lower}_case in LibWeb.

(cherry picked from commit 073bcfd3866852a4c4bcca2bd131bd65ae53541f)
2024-11-18 20:22:45 -05:00
Timothy Flynn
814f13db04 AK: Add a comparison operator for Utf32View
(cherry picked from commit 0703ba118b56aaa924346ae36c8826764dadd409)
2024-11-17 11:03:57 -05:00
Timothy Flynn
d0f3347017 AK: Allow constructing Utf32 from a span of u32
(cherry picked from commit cf9693169cabfd23fe6b638fc7e9fcde1c3ecad8)
2024-11-17 11:03:57 -05:00
Sam Atkins
b647a74557 AK: Add Utf8View::for_each_split_view() method
Returns one Utf8View at a time, using a callback function to identify
code points to split on.

(cherry picked from commit 3f10a5701d9634e47111203b837283bdfc2d8b18)
2024-11-15 23:09:44 -05:00
Sam Atkins
4548cd6e5f AK: Ensure empty StringViews all compare as equal
Before this change, a StringView with a character-data pointer would
never compare as equal to one with a null pointer, even if they were
both length 0. This could happen for example if one is
default-initialized, and the other is created as a substring.

(cherry picked from commit ec5101a1d3cc7bc9068fa4863e16aa482536929a)
2024-11-15 23:09:44 -05:00
Jonne Ransijn
9614721bde AK: Pass (Deprecated)FlyString::is_one_of arguments by reference
This avoid unnecessairy reference counting.

(cherry picked from commit 22a66bb1c2afde93096d9866d6386e2bca955f71)
2024-11-14 23:38:18 -05:00
Timothy Flynn
60782cb219 AK: Do not coerce i64 and u64 values to i32 and u32
First, this isn't actually helpful, as we no longer store 32-bit values
in JsonValue. They are stored as 64-bit values anyways.

But more imporatantly, there was a bug here when trying to coerce an i64
to an i32. All negative values were cast to an i32, without checking if
the value is below NumericLimits<i32>::min.

(cherry picked from commit 7b3b608cafbed8049ac7a34104c66622c1445ffc)
2024-11-14 19:52:23 -05:00
Jonne Ransijn
de1ecc17ce AK: Add ASCII fast path for Utf8View::contains
This also makes `Utf8View::trim` significantly faster, since most
strings start and end with ASCII.

(cherry picked from commit 7f3269fb025051c3eb2794b15e785b85b0ce37f3)
2024-11-12 07:14:25 -05:00
Andreas Kling
b57e502d21 AK: Add ASCII fast path in StringBuilder::append(Utf16View)
And let's at least try to pre-allocate an appropriate amount of
buffer space in the builder instead of appending and growing one
code point at a time.

(cherry picked from commit b6e28ff80779520ccb49e6c15ae1866bbc8713a7)
2024-11-12 07:14:25 -05:00
Ali Mohammad Pur
bc9e03ea38 AK: Cache all the line positions in LineTrackingLexer
Also updates a LibWeb text test that used to report the wrong line
number.

(cherry picked from commit 02b50d463b174e5d525c7ab8ce8dd173d550de28;
amended to exclude LineTrackingLexer from KERNEL, since that now use
make<>)
2024-11-12 04:25:50 -05:00
Ali Mohammad Pur
2502b5713d AK: Make TemporaryChange not copy the old value twice
This is necessary for the next commit (and might even help performance
in some very weird cases).

(cherry picked from commit e5f87eb12bdad9dfbf8a825461ac17fdb8457f50;
amended to add a missing include of StdLibExtras.h for move())
2024-11-12 04:25:50 -05:00
Andreas Kling
f670b2ca32 AK: Use getrlimit() to find the correct main thread stack size on macOS
This is what JavaScriptCore does as well.

(cherry picked from commit 11458f0d91654feed9b4e225c4e6ac8528057355)
2024-11-10 19:39:05 -05:00
Andreas Kling
1030034130 LibWeb: Put CSS transitions debug spam behind CSS_TRANSITIONS_DEBUG
(cherry picked from commit 42a1a0bd73fecd22452e224859d27221588c3f5f;
amended to fix conflicts in Debug.h.in and all_the_debug_macros.cmake)
2024-11-04 13:15:58 -05:00
Andreas Kling
1733d84cf6 AK+LibURL: Move CopyOnWrite<T> from LibURL to AK
(cherry picked from commit af68771dda8e28a27d93933d25f141e4752d35ad)
2024-10-20 16:38:41 -04:00
Andreas Kling
1b73445f55 LibWeb: Note what's causing a style invalidation to happen
You can now build with STYLE_INVALIDATION_DEBUG and get a debug stream
of reasons why style invalidations are happening and where.

I've rewritten this code many times, so instead of throwing it away once
again, I figured we should at least have it behind a flag.

(cherry picked from commit ddbfac38b0074819470766846fca08fd78630eb0;
minorly amended for conflicts in AK/Debug.h.in and
Meta/CMake/all_the_debug_macros.cmake due to us having more debug
macros. Also, downstream got alphabetical order for
STYLE_INVALIDATION_DEBUG wrong.)
2024-10-20 01:32:53 -04:00
Timothy Flynn
59040d9bb8 AK: Add a method to compute UTF-16 length from a UTF-8 string
(cherry picked from commit 7a17c654d293c4afaf3086dc94e8cd4bceac48b1;
amended to resolve minor conflict in TestUtf16.cpp due to us not
(yet?) having `TEST_CASE(null_view)`. Also amended to make the new
method not call simdutf -- it's now also inefficient, but at least
the inefficient code is now only in one place instead of in several)
2024-10-18 18:21:18 -04:00
Shannon Booth
bcdb23ba3e AK: Add BOM handling to String::from_utf8_with_replacement_character
(cherry picked from commit b3bf5c4ea84bfbd0ba14fdbc3a86e5d54e053702)
2024-10-16 23:56:40 -04:00
Shannon Booth
6a9222ffd3 AK: Add fast-path in from_utf8_with_replacement_character for utf-8
This ports the same optimization which was made in
1a46d8df5fc81eb2c320d5c8a5597285d3d8fb3a to this function as well.

(cherry picked from commit 1e8cc97b731871409316c121e637c32806135122)
2024-10-16 23:56:40 -04:00
Shannon Booth
997ce4644f AK: Add String::from_utf8_with_replacement_character
This takes a byte sequence and converts it to a UTF-8 string with the
replacement character.

(cherry picked from commit 033ea0e7fb0f72338ae95aa0413da838206440bb)
2024-10-16 23:56:40 -04:00
Shannon Booth
fbb1db21b9 AK: Add is_ascii_c0_control_or_space
(cherry picked from commit cfa8a8cea25dfdc620ab1162ad853197014526ff)
2024-10-15 12:08:50 -04:00
kleines Filmröllchen
f23a1c6e97 AK: Use fast exponentiation for pow()
Whatever we were doing beforehand is *really*
slow.
2024-10-13 03:44:49 +02:00
kleines Filmröllchen
1fcb1d81cc AK: Avoid UB from pow() integer fast path
Only take this fast path if the power actually fits in an integer type.
Also, use 64-bit integers in all cases so that the fast path can be
taken more often.
2024-10-13 03:44:49 +02:00
Ali Mohammad Pur
6ac71a6623 AK: Don't show '0 seconds' unless seconds is the only available element 2024-10-02 08:10:54 +02:00
Ali Mohammad Pur
9a80f2f9ff AK: Add a fallible try_resolve() function to JsonPath
The existing version simply crashed if the expected layout wasn't in
line with reality.
2024-10-02 08:10:54 +02:00
Tim Ledbetter
d11ca444d0 AK: Assert that is<T>() input and output types are not the same
This makes it a compile error to use is<T>() where the input and output
types are known to be the same at compile time.

(cherry picked from commit 82a63e350cb9a67cbd3aa659d909ae9e6599e93f)
2024-09-28 14:14:28 -04:00