mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
AK: Add AK::SIMD::exp_approximate
This approximation tries to generate values within 0.1% of their actual expected value. Microbenchmarks indicate that this iterative SIMD version can be up to 60x faster than `AK::SIMD::exp`.
This commit is contained in:
parent
3275d659bf
commit
f4342c9118
Notes:
sideshowbarker
2024-07-17 17:06:59 +09:00
Author: https://github.com/gmta Commit: https://github.com/SerenityOS/serenity/commit/f4342c9118 Pull-request: https://github.com/SerenityOS/serenity/pull/17504
3 changed files with 43 additions and 0 deletions
|
@ -66,6 +66,15 @@ ALWAYS_INLINE static f32x4 exp(f32x4 v)
|
|||
};
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static f32x4 exp_approximate(f32x4 v)
|
||||
{
|
||||
static constexpr int number_of_iterations = 10;
|
||||
auto result = 1.f + v / (1 << number_of_iterations);
|
||||
for (int i = 0; i < number_of_iterations; ++i)
|
||||
result *= result;
|
||||
return result;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE static f32x4 sqrt(f32x4 v)
|
||||
{
|
||||
#if ARCH(x86_64)
|
||||
|
|
|
@ -61,6 +61,7 @@ set(AK_TEST_SOURCES
|
|||
TestQuickSort.cpp
|
||||
TestRedBlackTree.cpp
|
||||
TestRefPtr.cpp
|
||||
TestSIMD.cpp
|
||||
TestSinglyLinkedList.cpp
|
||||
TestSourceGenerator.cpp
|
||||
TestSourceLocation.cpp
|
||||
|
|
33
Tests/AK/TestSIMD.cpp
Normal file
33
Tests/AK/TestSIMD.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/SIMD.h>
|
||||
#include <AK/SIMDMath.h>
|
||||
|
||||
TEST_CASE(exp)
|
||||
{
|
||||
AK::SIMD::f32x4 v = { .2f, .4f, .6f, .8f };
|
||||
auto result = AK::SIMD::exp(v);
|
||||
|
||||
EXPECT_APPROXIMATE(result[0], 1.22140276f);
|
||||
EXPECT_APPROXIMATE(result[1], 1.49182470f);
|
||||
EXPECT_APPROXIMATE(result[2], 1.82211880f);
|
||||
EXPECT_APPROXIMATE(result[3], 2.22554093f);
|
||||
}
|
||||
|
||||
TEST_CASE(exp_approximate)
|
||||
{
|
||||
AK::SIMD::f32x4 v = { .2f, .4f, .6f, .8f };
|
||||
auto result = AK::SIMD::exp_approximate(v);
|
||||
constexpr float accuracy = .001f;
|
||||
|
||||
EXPECT(fabsf(result[0] - 1.22140276f) <= accuracy);
|
||||
EXPECT(fabsf(result[1] - 1.49182470f) <= accuracy);
|
||||
EXPECT(fabsf(result[2] - 1.82211880f) <= accuracy);
|
||||
EXPECT(fabsf(result[3] - 2.22554093f) <= accuracy);
|
||||
}
|
Loading…
Reference in a new issue