Added function for array comparison. Fixed mistake in Makefile.

This commit is contained in:
2023-09-07 19:39:35 +02:00
parent f942ef25c2
commit 6f320f593c
5 changed files with 62 additions and 30 deletions

View File

@@ -13,31 +13,27 @@ test: $(TARGET)/test
build: $(TARGET)/ctensor.o build: $(TARGET)/ctensor.o
$(TARGET)/test: $(FILES_TEST) $(TARGET)/ctensor.o $(TARGET)/test: $(FILES_TEST) $(TARGET)/ctensor.o | dirs
$(CC) $(LD_FLAGS) $^ -o $@ $(CC) $(LD_FLAGS) $^ -o $@
$(TARGET)/ctensor.o: $(FILES) $(TARGET)/ctensor.o: $(FILES)
ld $(LD_FLAGS) -r $^ -o $@ ld $(LD_FLAGS) -r $^ -o $@
dirs:
mkdir -p $(TARGET)/build/$(DIR_TESTS)
# --- Source --- # --- Source ---
$(TARGET)/build: $(TARGET)/build/%.o: %.c %.h | dirs
mkdir -p $@
$(TARGET)/build/%.o: %.c %.h | $(TARGET)/build
$(CC) $(CC_FLAGS) -c $< -o $@ $(CC) $(CC_FLAGS) -c $< -o $@
# --- Tests --- # --- Tests ---
OBJ_DIR_TESTS = $(TARGET)/build/$(DIR_TESTS) OBJ_DIR_TESTS = $(TARGET)/build/$(DIR_TESTS)
$(OBJ_DIR_TESTS): $(OBJ_DIR_TESTS)/main.o: $(DIR_TESTS)/main.c $(DIR_TESTS)/main.h | dirs
mkdir -p $@
$(OBJ_DIR_TESTS)/main.o: $(DIR_TESTS)/main.c $(DIR_TESTS)/main.h | $(OBJ_DIR_TESTS)
$(CC) $(CC_FLAGS) -c $< -o $@ $(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 $@ $(CC) $(CC_FLAGS) -c $< -o $@

View File

@@ -46,15 +46,9 @@ bool tensor_is_equal(const tensor t1, const tensor t2)
assert(!tensor_is_empty(t1)); assert(!tensor_is_empty(t1));
assert(!tensor_is_empty(t2)); assert(!tensor_is_empty(t2));
uint32_t i;
if (t1->rank != t2->rank) return false; if (t1->rank != t2->rank) return false;
for (i = 0; i < t1->rank; i++) { if (!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
if (t1->size[i] != t2->size[i]) return false; return tarray_equals(t1->elements, t2->elements, t1->num_elem);
}
for (i = 0; i < t1->num_elem; i++) {
if (DTYPE_NE(t1->elements[i], t2->elements[i])) return false;
}
return true;
} }
bool _tensor_check_size(const uint32_t *size, uint8_t rank) 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; uint32_t i;
if(t1->rank != t2->rank) return false; if(t1->rank != t2->rank) return false;
for(i = 0; i < t1->rank; i++) { if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
if(t1->size[i] != t2->size[i]) return false;
}
for(i = 0; i < t1->num_elem; i++) { for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_ADD(t1->elements[i], t2->elements[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; uint32_t i;
if(t1->rank != t2->rank) return false; if(t1->rank != t2->rank) return false;
for(i = 0; i < t1->rank; i++) { if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
if(t1->size[i] != t2->size[i]) return false;
}
for(i = 0; i < t1->num_elem; i++) { for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_SUB(t1->elements[i], t2->elements[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; uint32_t i;
if(t1->rank != t2->rank) return false; if(t1->rank != t2->rank) return false;
for(i = 0; i < t1->rank; i++) { if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
if(t1->size[i] != t2->size[i]) return false;
}
for(i = 0; i < t1->num_elem; i++) { for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_MUL(t1->elements[i], t2->elements[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; uint32_t i;
if(t1->rank != t2->rank) return false; if(t1->rank != t2->rank) return false;
for(i = 0; i < t1->rank; i++) { if(!tarray_uint32_equals(t1->size, t2->size, t1->rank)) return false;
if(t1->size[i] != t2->size[i]) return false;
}
for(i = 0; i < t1->num_elem; i++) { for(i = 0; i < t1->num_elem; i++) {
t1->elements[i] = DTYPE_DIV(t1->elements[i], t2->elements[i]); t1->elements[i] = DTYPE_DIV(t1->elements[i], t2->elements[i]);
} }

View File

@@ -2,6 +2,7 @@
#define _TENSOR_H_INCLUDED_ #define _TENSOR_H_INCLUDED_
#include "dtype.h" #include "dtype.h"
#include "tensorarray.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>

37
tensorarray.c Normal file
View File

@@ -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;
}

12
tensorarray.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef _TENSORARRAY_H_INCLUDED_
#define _TENSORARRAY_H_INCLUDED_
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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_