From 6f320f593c9c16908c36f7ecf2fdfdd62ad19e96 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 7 Sep 2023 19:39:35 +0200 Subject: [PATCH] Added function for array comparison. Fixed mistake in Makefile. --- Makefile | 16 ++++++---------- tensor.c | 26 ++++++-------------------- tensor.h | 1 + tensorarray.c | 37 +++++++++++++++++++++++++++++++++++++ tensorarray.h | 12 ++++++++++++ 5 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 tensorarray.c create mode 100644 tensorarray.h diff --git a/Makefile b/Makefile index d5012a5..37e7783 100644 --- a/Makefile +++ b/Makefile @@ -13,31 +13,27 @@ test: $(TARGET)/test build: $(TARGET)/ctensor.o -$(TARGET)/test: $(FILES_TEST) $(TARGET)/ctensor.o +$(TARGET)/test: $(FILES_TEST) $(TARGET)/ctensor.o | dirs $(CC) $(LD_FLAGS) $^ -o $@ $(TARGET)/ctensor.o: $(FILES) ld $(LD_FLAGS) -r $^ -o $@ +dirs: + mkdir -p $(TARGET)/build/$(DIR_TESTS) # --- Source --- -$(TARGET)/build: - mkdir -p $@ - -$(TARGET)/build/%.o: %.c %.h | $(TARGET)/build +$(TARGET)/build/%.o: %.c %.h | dirs $(CC) $(CC_FLAGS) -c $< -o $@ # --- Tests --- OBJ_DIR_TESTS = $(TARGET)/build/$(DIR_TESTS) -$(OBJ_DIR_TESTS): - mkdir -p $@ - -$(OBJ_DIR_TESTS)/main.o: $(DIR_TESTS)/main.c $(DIR_TESTS)/main.h | $(OBJ_DIR_TESTS) +$(OBJ_DIR_TESTS)/main.o: $(DIR_TESTS)/main.c $(DIR_TESTS)/main.h | dirs $(CC) $(CC_FLAGS) -c $< -o $@ -$(OBJ_DIR_TESTS)/tensor_test.o: $(DIR_TESTS)/tensor_test.c $(DIR_TESTS)/tensor_test.h | $(OBJ_DIR_TESTS) +$(OBJ_DIR_TESTS)/tensor_test.o: $(DIR_TESTS)/tensor_test.c $(DIR_TESTS)/tensor_test.h | dirs $(CC) $(CC_FLAGS) -c $< -o $@ diff --git a/tensor.c b/tensor.c index 92886c4..1172fe3 100644 --- a/tensor.c +++ b/tensor.c @@ -46,15 +46,9 @@ bool tensor_is_equal(const tensor t1, const tensor t2) assert(!tensor_is_empty(t1)); assert(!tensor_is_empty(t2)); - uint32_t i; if (t1->rank != t2->rank) return false; - for (i = 0; i < t1->rank; i++) { - if (t1->size[i] != t2->size[i]) return false; - } - for (i = 0; i < t1->num_elem; i++) { - if (DTYPE_NE(t1->elements[i], t2->elements[i])) return false; - } - return true; + if (!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false; + return tarray_equals(t1->elements, t2->elements, t1->num_elem); } bool _tensor_check_size(const uint32_t *size, uint8_t rank) @@ -287,9 +281,7 @@ bool tensor_add_inplace(tensor t1, const tensor t2) uint32_t i; if(t1->rank != t2->rank) return false; - for(i = 0; i < t1->rank; i++) { - if(t1->size[i] != t2->size[i]) return false; - } + if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false; for(i = 0; i < t1->num_elem; i++) { t1->elements[i] = DTYPE_ADD(t1->elements[i], t2->elements[i]); } @@ -312,9 +304,7 @@ bool tensor_sub_inplace(tensor t1, const tensor t2) uint32_t i; if(t1->rank != t2->rank) return false; - for(i = 0; i < t1->rank; i++) { - if(t1->size[i] != t2->size[i]) return false; - } + if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false; for(i = 0; i < t1->num_elem; i++) { t1->elements[i] = DTYPE_SUB(t1->elements[i], t2->elements[i]); } @@ -337,9 +327,7 @@ bool tensor_mul_inplace(tensor t1, const tensor t2) uint32_t i; if(t1->rank != t2->rank) return false; - for(i = 0; i < t1->rank; i++) { - if(t1->size[i] != t2->size[i]) return false; - } + if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false; for(i = 0; i < t1->num_elem; i++) { t1->elements[i] = DTYPE_MUL(t1->elements[i], t2->elements[i]); } @@ -362,9 +350,7 @@ bool tensor_div_inplace(tensor t1, const tensor t2) uint32_t i; if(t1->rank != t2->rank) return false; - for(i = 0; i < t1->rank; i++) { - if(t1->size[i] != t2->size[i]) return false; - } + if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false; for(i = 0; i < t1->num_elem; i++) { t1->elements[i] = DTYPE_DIV(t1->elements[i], t2->elements[i]); } diff --git a/tensor.h b/tensor.h index 02b682e..e5a1737 100644 --- a/tensor.h +++ b/tensor.h @@ -2,6 +2,7 @@ #define _TENSOR_H_INCLUDED_ #include "dtype.h" +#include "tensorarray.h" #include #include diff --git a/tensorarray.c b/tensorarray.c new file mode 100644 index 0000000..2371568 --- /dev/null +++ b/tensorarray.c @@ -0,0 +1,37 @@ +#include "tensorarray.h" + +bool tarray_equals(dtype* a1, dtype* a2, uint32_t len) +{ + /* Checks whether to arrays are equal. If one or both arrays are array + * pointers are NULL, false is returned. + * + * @param a1 The first array to compare + * @param a2 The second array to compare + * @param len The length of the arrays + * + * @return true if the arrays are equal, false otherwise + */ + if (a1 == NULL || a2 == NULL) return false; + for (uint32_t i = 0; i < len; i++) { + if (DTYPE_NE(a1[i], a2[i])) return false; + } + return true; +} + +bool tarray_uint32_equals(uint32_t* a1, uint32_t* a2, uint32_t len) +{ + /* Checks whether to arrays of the type uint32_t are equal. If one or both + * arrays are array pointers are NULL, false is returned. + * + * @param a1 The first array to compare + * @param a2 The second array to compare + * @param len The length of the arrays + * + * @return true if the arrays are equal, false otherwise + */ + if (a1 == NULL || a2 == NULL) return false; + for (uint32_t i = 0; i < len; i++) { + if (a1[i] != a2[i]) return false; + } + return true; +} diff --git a/tensorarray.h b/tensorarray.h new file mode 100644 index 0000000..8663baa --- /dev/null +++ b/tensorarray.h @@ -0,0 +1,12 @@ +#ifndef _TENSORARRAY_H_INCLUDED_ +#define _TENSORARRAY_H_INCLUDED_ + +#include +#include +#include +#include "dtype.h" + +inline bool tarray_equals(dtype* a1, dtype* a2, uint32_t len); +inline bool tarray_uint32_equals(uint32_t* a1, uint32_t* a2, uint32_t len); + +#endif // _TENSORARRAY_H_INCLUDED_