From 21290535d40530e15d7eca4f0b80a9689bbd1000 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Wed, 7 Jun 2023 10:14:55 +0200 Subject: [PATCH] feature (libnx): implemented basic math utils --- libnx/inc/public/nox/math.h | 6 ++++-- libnx/src/math.c | 25 +++++++++++++++++++++++-- libnx/src/stdio.c | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/libnx/inc/public/nox/math.h b/libnx/inc/public/nox/math.h index 84f62a5..ee7a466 100644 --- a/libnx/inc/public/nox/math.h +++ b/libnx/inc/public/nox/math.h @@ -5,7 +5,9 @@ #include "stdtypes.h" -uint64_t min(uint64_t a, uint64_t b); -uint64_t max(uint64_t a, uint64_t b); +int64_t min(int64_t a, int64_t b); +int64_t max(int64_t a, int64_t b); +double pow(double base, int64_t exponent); +int64_t abs(int64_t n); #endif //LIBNX_MATH_H diff --git a/libnx/src/math.c b/libnx/src/math.c index c539b33..adea5ff 100644 --- a/libnx/src/math.c +++ b/libnx/src/math.c @@ -2,10 +2,31 @@ #include "public/nox/math.h" -uint64_t min (uint64_t a, uint64_t b) { +int64_t min(int64_t a, int64_t b) { return a < b ? a : b; } -uint64_t max (uint64_t a, uint64_t b) { +int64_t max(int64_t a, int64_t b) { return a > b ? a : b; } +double pow(double base, int64_t exponent) { + double result = 1; + + if (exponent >= 0) { + for (int i = 0; i < exponent; i++) { + result *= base; + } + } else { + for (int i = 0; i < abs(exponent); i++) { + result /= base; + } + } + return result; +} + +int64_t abs(int64_t n) { + if (n < 0) { + return n * -1; + } + return n; +} diff --git a/libnx/src/stdio.c b/libnx/src/stdio.c index 6846c2a..741166b 100644 --- a/libnx/src/stdio.c +++ b/libnx/src/stdio.c @@ -4,7 +4,7 @@ #include "public/nox/syscall.h" #include "public/nox/math.h" -struct file_T{ +struct file_T { uint64_t fd; uint64_t cursor; };