mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:38:10 +00:00
LibM: Implement more rounding functions
This patch implements the entire rint family, while taking into account the current rounding mode, and implements ceil, round, trunc, and floor for types which they weren't before.
This commit is contained in:
parent
88d342d007
commit
efe4845c56
2 changed files with 83 additions and 6 deletions
|
@ -28,6 +28,7 @@
|
|||
#include <AK/Platform.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
#include <LibC/assert.h>
|
||||
#include <fenv.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -61,10 +62,10 @@ template<size_t value>
|
|||
constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
|
||||
|
||||
enum class RoundingMode {
|
||||
ToZero,
|
||||
Up,
|
||||
Down,
|
||||
ToEven
|
||||
ToZero = FE_TOWARDZERO,
|
||||
Up = FE_UPWARD,
|
||||
Down = FE_DOWNWARD,
|
||||
ToEven = FE_TONEAREST
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -307,6 +308,16 @@ double trunc(double x) NOEXCEPT
|
|||
return internal_to_integer(x, RoundingMode::ToZero);
|
||||
}
|
||||
|
||||
float truncf(float x) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(x, RoundingMode::ToZero);
|
||||
}
|
||||
|
||||
long double truncl(long double x) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(x, RoundingMode::ToZero);
|
||||
}
|
||||
|
||||
double cos(double angle) NOEXCEPT
|
||||
{
|
||||
return sin(angle + M_PI_2);
|
||||
|
@ -691,6 +702,11 @@ float roundf(float value) NOEXCEPT
|
|||
return internal_to_integer(value, RoundingMode::ToEven);
|
||||
}
|
||||
|
||||
long double roundl(long double value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode::ToEven);
|
||||
}
|
||||
|
||||
float floorf(float value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode::Down);
|
||||
|
@ -701,10 +717,54 @@ double floor(double value) NOEXCEPT
|
|||
return internal_to_integer(value, RoundingMode::Down);
|
||||
}
|
||||
|
||||
long double floorl(long double value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode::Down);
|
||||
}
|
||||
|
||||
long double rintl(long double value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
double rint(double value) NOEXCEPT
|
||||
{
|
||||
// This should be the current rounding mode
|
||||
return internal_to_integer(value, RoundingMode::ToEven);
|
||||
return internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
float rintf(float value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long lrintl(long double value) NOEXCEPT
|
||||
{
|
||||
return (long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long lrint(double value) NOEXCEPT
|
||||
{
|
||||
return (long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long lrintf(float value) NOEXCEPT
|
||||
{
|
||||
return (long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long long llrintl(long double value) NOEXCEPT
|
||||
{
|
||||
return (long long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long long llrint(double value) NOEXCEPT
|
||||
{
|
||||
return (long long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
long long llrintf(float value) NOEXCEPT
|
||||
{
|
||||
return (long long)internal_to_integer(value, RoundingMode { fegetround() });
|
||||
}
|
||||
|
||||
float ceilf(float value) NOEXCEPT
|
||||
|
@ -717,6 +777,11 @@ double ceil(double value) NOEXCEPT
|
|||
return internal_to_integer(value, RoundingMode::Up);
|
||||
}
|
||||
|
||||
long double ceill(long double value) NOEXCEPT
|
||||
{
|
||||
return internal_to_integer(value, RoundingMode::Up);
|
||||
}
|
||||
|
||||
double modf(double x, double* intpart) NOEXCEPT
|
||||
{
|
||||
double integer_part = internal_to_integer(x, RoundingMode::ToZero);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue