Added function for array comparison. Fixed mistake in Makefile.
This commit is contained in:
16
Makefile
16
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 $@
|
||||
|
||||
|
||||
|
||||
26
tensor.c
26
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]);
|
||||
}
|
||||
|
||||
1
tensor.h
1
tensor.h
@@ -2,6 +2,7 @@
|
||||
#define _TENSOR_H_INCLUDED_
|
||||
|
||||
#include "dtype.h"
|
||||
#include "tensorarray.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
37
tensorarray.c
Normal file
37
tensorarray.c
Normal 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
12
tensorarray.h
Normal 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_
|
||||
Reference in New Issue
Block a user