From a9365126b685c434d9cc557454863b24fa6f6421 Mon Sep 17 00:00:00 2001 From: Maxime Chevalier-Boisvert Date: Sat, 30 Sep 2023 14:49:01 -0400 Subject: [PATCH] Add floorf() function to include/math.h --- ncc/include/math.h | 10 ++++++++++ ncc/tests/floats.c | 3 +++ 2 files changed, 13 insertions(+) diff --git a/ncc/include/math.h b/ncc/include/math.h index 53d81e19..995f03da 100644 --- a/ncc/include/math.h +++ b/ncc/include/math.h @@ -12,4 +12,14 @@ #define atanf(__f) (asm (__f) -> float { atans_f32; }) #define sqrtf(__f) (asm (__f) -> float { sqrt_f32; }) +float floorf(float x) +{ + float xi = (float)(int)x; + + if (x < xi) + return xi - 1.0f; + + return xi; +} + #endif diff --git a/ncc/tests/floats.c b/ncc/tests/floats.c index 7925c392..63e79285 100644 --- a/ncc/tests/floats.c +++ b/ncc/tests/floats.c @@ -38,5 +38,8 @@ int main() assert(sqrtf(4.0f) == 2.0f); assert(sinf(0.0f) == 0.0f); + assert(floorf(4.5f) == 4.0f); + assert(floorf(-4.5f) == -5.0f); + return 0; }