Commit graph

42 commits

Author SHA1 Message Date
Dennis Camera
1bc44376c0 AK: Implement floating-point conversions for big-endian 2024-07-05 09:47:08 -06:00
Zaggy1024
bbd8a218a5 AK: Prevent overflow of the min when clamping unsigned values to signed
Also, add some tests for the cases that were broken before.
2024-06-24 12:41:32 -06:00
Tim Ledbetter
5ca2f4dfd7 Everywhere: Remove all KERNEL #defines 2024-06-18 09:36:25 +02:00
Nico Weber
a1f70b39fa AK: Remove rsqrt()
At least on arm64, this isn't very preciese:
https://github.com/SerenityOS/serenity/issues/22739#issuecomment-1912909835

It is also now unused.
2024-01-30 10:02:33 +01:00
timmot
da3cfd5bbc AK+LibWeb: Make clamp_to_int generic over all integrals 2023-11-24 08:42:18 +01:00
Bastiaan van der Plaat
494a8cb816 AK: Add to_radians and to_degrees math functions 2023-09-10 08:38:29 +01:00
Andrew Kaster
f87a488b22 AK: Silence -Wimplicit-const-int-float-conversion in clamp_to_int 2023-08-19 21:05:06 -06:00
Hendiadyoin1
af161a8b83 AK+LibWeb: Round to int in clamp_to_int instead of truncating
This caused inaccuracies in float->CssPixel conversions
2023-08-05 20:03:09 +02:00
Hendiadyoin1
07e4358c63 AK: Use correct builtins for fmod and remainder
Similar to floor and ceil, we were forcing the values to be doubles here

Also adds a big FIXME about my findings trying to add a general
implementation for these
2023-07-31 05:22:12 +02:00
Hediadyoin1
c9808f0d4a AK: Use correct builtins for floor and ceil
We were using the double ones, forcing casts to and from them for floats
2023-07-31 05:22:12 +02:00
Hediadyoin1
29e0494e56 AK: Use builtins in fabs implementation and move it to the top
Both GCC and Clang inline this function to use bit-wise logic and/or
appropriate instructions even on -O0 and allow their use in a constexpr
context, see
https://godbolt.org/z/de1393vha
2023-07-31 05:22:12 +02:00
Hediadyoin1
594369121a AK: Add trunc and rint to AK/Math.h
These are useful in some algorithms, which require specific rounding.
2023-07-31 05:22:12 +02:00
Hediadyoin1
6573ace8f1 AK: Move rounding function to the top of AK/Math.h
These are useful in other algorithms, so lets move them up
2023-07-31 05:22:12 +02:00
Aliaksandr Kalenik
d216621d2a AK: Add clamp_to_int(value) in Math.h
clamp_to_int clamps value to valid range of int values so resulting
value does not overflow.

It is going to be used to clamp float or double values to int that
represents fixed-point value of CSSPixels.
2023-07-25 11:52:02 +02:00
Hendiadyoin1
467bc5b093 AK: Add a generic version of log2
This uses one of Sun OS's algorithms, for a comparison to other
algorithms please refer to
https://gist.github.com/Hendiadyoin1/f58346d66637deb9156ef360aa158bf9

This is used on aarch64 builds and for x86 floats and doubles
for performance gains check
https://quick-bench.com/q/_2jTykshP6cUqtgdepFaoQ53YC8
which shows approximately 2x gains

Co-Authored-By: Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
Co-Authored-By: kleines Filmröllchen <filmroellchen@serenityos.org>
Co-Authored-By: Dan Klishch <danilklishch@gmail.com>
2023-07-09 15:39:52 +01:00
wertzuz
f8527e1cad AK: Rename fixme to match other FIXMEs 2023-06-15 15:38:50 +01:00
Nico Weber
fc15fc36ce AK: Make math work on arm hosts again
957f89ce4a added some tweaks for serenity-on-aarch64.
It broke anythingelse-on-aarch64 hosts though, so only do these tweaks
when targeting serenity.

(I wonder if AK/Math.h should fall back to the system math routines
when not targeting serenity in general. Would probably help ladybird
performance. On the other hand, the serenity routines would see less
use and hence exposure and love.)
2023-04-14 16:16:42 +02:00
Timon Kruiper
957f89ce4a AK: Add very naive implementation of {sin,cos,tan} for aarch64
The {sin,cos,tan} functions in AK are used as the implementation of the
same function in libm. We cannot use the __builtin_foo functions as
these would just call the libc functions. This was causing an infinite
loop. Fix this by adding a very naive implementation of
AK::{sin, cos,tan}, that is only valid for small inputs. For the other
functions in this file, I added a TODO() such that we'll crash, instead
of infinite looping.
2023-04-13 20:24:25 +02:00
kleines Filmröllchen
b38beb106e AK: Add constexpr floor and round 2023-03-10 04:07:14 -07:00
Hendiadyoin1
1bdc4e6b29 AK: Reorder AK/Math after the removal of i686
Without i686 we can remove some implementations for sqrt and round_to.
2023-01-09 11:36:21 +01:00
Liav A
feeb25bcee AK: Remove i686 support 2022-12-28 11:53:41 +01:00
Andreas Kling
ae3ffdd521 AK: Make it possible to not using AK classes into the global namespace
This patch adds the `USING_AK_GLOBALLY` macro which is enabled by
default, but can be overridden by build flags.

This is a step towards integrating Jakt and AK types.
2022-11-26 15:51:34 +01:00
MacDue
ce6dacbc46 AK: Fix double promotion error when using AK:ceil on floats 2022-11-26 01:17:04 +01:00
Hendiadyoin1
a143d666db AK: Fix aarch64 versions of math functions
These were incorrectly assumed to compile, but did indeed still have
a few issues.
2022-10-14 11:06:28 +02:00
Hendiadyoin1
eb5651870e AK: Add support for some trivial math functions on aarch64
These all require just a single instruction each, we only have to
differentiate between register prefixes accordingly.
2022-10-14 00:28:34 +02:00
MacDue
072a78b958 AK: Add AK::ceil(float) and AK::ceil_log2(integer)
Co-authored-by: Leon Albrecht <leon2002.la@gmail.com>
2022-06-30 11:16:22 +02:00
Hediadyoin1
e6a3cac5ce AK: Add sqrt(2) and sqrt(1/2) constants 2022-06-14 23:00:52 +02:00
EWouters
0317882b61 AK: Use AK:: sin and cos on aarch64 build
This fixes the lagom build on aarch64, as `__builtin_sincosf` doesn't
take double arguments.
2022-05-08 16:51:23 +02:00
EWouters
6ea89c35fe AK: Use builtin versions of llrint{,l,f}
This fixes the build on M1 Macs.
2022-05-07 23:36:49 +02:00
Hendiadyoin1
d4fe02152a AK: Add an SSE2 specific implementation of sqrt(double) 2022-05-07 20:27:05 +02:00
Hendiadyoin1
37ff2b9bd2 AK: Add an helper for quick hardware based rounding
This uses the `fistp` and `cvts[sd]2si` respectively, to potentially
round floating point values with just one instruction.

This falls back to `llrint[fl]?` on aarch64 for now.
2022-05-07 20:25:39 +02:00
Andrew Kaster
83603d68d2 AK: Make sure we don't include Math.h or math.h from KERNEL 2022-04-23 10:43:32 -07:00
serenityosrocks
4a6a7cf3c8 AK: Add generic sincos solution for non-x86 platforms 2022-04-03 00:31:41 +01:00
Hendiadyoin1
b1db1e4e4f AK: Add rsqrt and a SSE specific implementation for sqrt 2022-04-02 18:37:38 +02:00
Hendiadyoin1
cd21e03225 AK+Everywhere: Add sincos and use it in some places
Calculating sin and cos at once is quite a bit cheaper than calculating
them individually.
x87 has even a dedicated instruction for it: `fsincos`.
2022-03-15 11:39:42 +01:00
Gunnar Beutner
7a65bd8efb AK: Don't use x86 assembly when building for non-x86 targets
This allows Lagom to be built successfully for Apple M1.

Fixes #12644.
2022-02-19 13:13:22 +02:00
Hendiadyoin1
fbb798f98c AK: Move integral log2 and exp to IntegerMath.h 2022-02-06 17:52:33 +00:00
Pankaj Raghav
24c66b8442 AK: Fix log2 calculation for Integer
For a `N` bit integer X, the log2 calculation should be as follows:
(N - 1) - ctz(X)
2022-01-29 17:41:06 +02:00
Nick Johnson
08e4a1a4dc AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
2021-12-21 22:13:51 +01:00
Jelle Raaijmakers
dfbdd035da AK: Implement acos<T> correctly
This is a naive implementation based on the symmetry with `asin`.

Before, I'm not really sure what we were doing, but it was returning
wildly incorrect results.
2021-11-18 21:10:30 +01:00
Daniel Bertalan
5d32f543ec AK: Handle partial remainders
On x86, the `fprem` and `fmprem1` instructions may produce a 'partial
remainder', for which we should check by reading a FPU flag. If we don't
check for it, we may end up using values that are outside the expected
range of values.
2021-08-08 10:55:36 +02:00
Hendiadyoin1
c5f6ba6e71 AK: Introduce Math.h
This is to implement constexpr template based implementations for
mathematical functions

This also changes math.cpp to use these implementations.

Also adds a fastpath for floating point trucation for values smaller
than the signed 64 bit limit.
2021-07-19 16:34:21 +04:30