From c44932fe03a5b15695bdd0cf5ec63481ad383f5f Mon Sep 17 00:00:00 2001 From: "YU-WEI,HSU" Date: Sat, 9 Aug 2025 23:14:31 +0800 Subject: [PATCH] Move abs function from memory.c to new math.c The abs() function is a mathematical utility and does not belong in lib/memory.c, which is intended for memory manipulation routines. This commit moves abs() to a new lib/math.c file to improve semantic consistency and maintainability, and to provide a dedicated place for future math-related functions. Fixes: #17 --- Makefile | 2 +- lib/math.c | 9 +++++++++ lib/memory.c | 6 ------ 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 lib/math.c diff --git a/Makefile b/Makefile index 060e9f2..61576a1 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ KERNEL_OBJS := timer.o mqueue.o pipe.o semaphore.o mutex.o error.o syscall.o tas KERNEL_OBJS := $(addprefix $(BUILD_KERNEL_DIR)/,$(KERNEL_OBJS)) deps += $(KERNEL_OBJS:%.o=%.o.d) -LIB_OBJS := ctype.o malloc.o memory.o random.o stdio.o string.o queue.o +LIB_OBJS := ctype.o malloc.o math.o memory.o random.o stdio.o string.o queue.o LIB_OBJS := $(addprefix $(BUILD_LIB_DIR)/,$(LIB_OBJS)) deps += $(LIB_OBJS:%.o=%.o.d) diff --git a/lib/math.c b/lib/math.c new file mode 100644 index 0000000..771ee22 --- /dev/null +++ b/lib/math.c @@ -0,0 +1,9 @@ +/* libc: Math function. */ + +#include + +/* Returns the absolute value of an integer. */ +int32_t abs(int32_t n) +{ + return n >= 0 ? n : -n; +} diff --git a/lib/memory.c b/lib/memory.c index 9a32e81..949fa1c 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -122,9 +122,3 @@ int32_t memcmp(const void *cs, const void *ct, uint32_t n) */ return (n == 0) ? 0 : ((*r1 < *r2) ? -1 : 1); } - -/* Returns the absolute value of an integer. */ -int32_t abs(int32_t n) -{ - return n >= 0 ? n : -n; -}