1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 01:58:12 +00:00

LibM: Implement floating point variants of various math functions

This commit is contained in:
Valtteri Koskivuori 2020-02-25 21:02:22 +02:00 committed by Andreas Kling
parent 3b8713a9df
commit 24b5fd4c4c
2 changed files with 55 additions and 0 deletions

View file

@ -69,6 +69,11 @@ double cos(double angle)
return sin(angle + M_PI_2); return sin(angle + M_PI_2);
} }
float cosf(float angle)
{
return sinf(angle + M_PI_2);
}
// This can also be done with a taylor expansion, but for // This can also be done with a taylor expansion, but for
// now this works pretty well (and doesn't mess anything up // now this works pretty well (and doesn't mess anything up
// in quake in particular, which is very Floating-Point precision // in quake in particular, which is very Floating-Point precision
@ -84,12 +89,27 @@ double sin(double angle)
return ret; return ret;
} }
float sinf(float angle)
{
float ret = 0.0f;
__asm__(
"fsin"
: "=t"(ret)
: "0"(angle));
return ret;
}
double pow(double x, double y) double pow(double x, double y)
{ {
//FIXME: Extremely unlikely to be standards compliant. //FIXME: Extremely unlikely to be standards compliant.
return exp(y * log(x)); return exp(y * log(x));
} }
float powf(float x, float y)
{
return (float)exp((double)y * log((double)x));
}
double ldexp(double x, int exp) double ldexp(double x, int exp)
{ {
// FIXME: Please fix me. I am naive. // FIXME: Please fix me. I am naive.
@ -139,6 +159,15 @@ double sqrt(double x)
return res; return res;
} }
float sqrtf(float x)
{
float res;
__asm__("fsqrt"
: "=t"(res)
: "0"(x));
return res;
}
double sinh(double x) double sinh(double x)
{ {
double exponentiated = exp(x); double exponentiated = exp(x);
@ -167,6 +196,11 @@ double log(double x)
return y + 2 * (x - exponentiated) / (x + exponentiated); return y + 2 * (x - exponentiated) / (x + exponentiated);
} }
float logf(float x)
{
return (float)log(x);
}
double fmod(double index, double period) double fmod(double index, double period)
{ {
return index - trunc(index / period) * period; return index - trunc(index / period) * period;
@ -208,6 +242,11 @@ double exp(double exponent)
return result * taylor_series_result; return result * taylor_series_result;
} }
float expf(float exponent)
{
return (float)exp(exponent);
}
double cosh(double x) double cosh(double x)
{ {
double exponentiated = exp(-x); double exponentiated = exp(-x);
@ -232,6 +271,11 @@ double atan2(double y, double x)
return atan(y / x) - M_PI; return atan(y / x) - M_PI;
} }
float atan2f(float y, float x)
{
return (float)atan2(y, x);
}
double atan(double x) double atan(double x)
{ {
if (x < 0) if (x < 0)
@ -265,11 +309,21 @@ double asin(double x)
return value; return value;
} }
float asinf(float x)
{
return (float)asin(x);
}
double acos(double x) double acos(double x)
{ {
return M_PI_2 - asin(x); return M_PI_2 - asin(x);
} }
float acosf(float x)
{
return M_PI_2 - asinf(x);
}
double fabs(double value) double fabs(double value)
{ {
return value < 0 ? -value : value; return value < 0 ? -value : value;

View file

@ -84,6 +84,7 @@ double ldexp(double, int exp);
float ldexpf(float, int exp); float ldexpf(float, int exp);
double pow(double x, double y); double pow(double x, double y);
float powf(float x, float y);
double log2(double); double log2(double);
float log2f(float); float log2f(float);