From 00c9cf068c17f41078eb81a61219c0072bb2a287 Mon Sep 17 00:00:00 2001 From: Fatih Cetinkaya <965295+devfacet@users.noreply.github.com> Date: Sun, 7 Apr 2024 15:21:11 -0400 Subject: [PATCH] Use fixed-size arrays in Neuron functions (#9) --- README.md | 2 -- include/nn_neuron.h | 6 +++--- src/nn_neuron.c | 14 +++----------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3379b86..f420e44 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,9 @@ git submodule update --init ### Build and run examples ```shell -# Generic examples make build-examples ARCH=generic make run-examples ARCH=generic -# Arm examples make build-examples ARCH=arm TECH=neon,cmsis-dsp make run-examples ARCH=arm TECH=neon,cmsis-dsp ``` diff --git a/include/nn_neuron.h b/include/nn_neuron.h index 007fab7..a29bcd7 100644 --- a/include/nn_neuron.h +++ b/include/nn_neuron.h @@ -24,15 +24,15 @@ typedef struct { } NNNeuron; // nn_neuron_init initializes a neuron with the given arguments. -bool nn_neuron_init(NNNeuron *neuron, const float *weights, size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error); +bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error); // nn_neuron_set_weights sets the weights of the given neuron. -bool nn_neuron_set_weights(NNNeuron *neuron, const float *weights, NNError *error); +bool nn_neuron_set_weights(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], NNError *error); // nn_neuron_set_bias sets the bias of the given neuron. bool nn_neuron_set_bias(NNNeuron *neuron, float bias, NNError *error); // nn_neuron_compute computes the given neuron and returns the output. -float nn_neuron_compute(const NNNeuron *neuron, const float *inputs, NNError *error); +float nn_neuron_compute(const NNNeuron *neuron, const float inputs[NEURON_MAX_WEIGHTS], NNError *error); #endif // NN_NEURON_H diff --git a/src/nn_neuron.c b/src/nn_neuron.c index 7771f65..d05b4d8 100644 --- a/src/nn_neuron.c +++ b/src/nn_neuron.c @@ -7,7 +7,7 @@ #include // nn_neuron_init initializes a neuron with the given arguments. -bool nn_neuron_init(NNNeuron *neuron, const float *weights, size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error) { +bool nn_neuron_init(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], size_t n_weights, float bias, NNActivationFunction act_func, NNDotProductFunction dot_product_func, NNError *error) { nn_error_set(error, NN_ERROR_NONE, NULL); if (neuron == NULL) { nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "neuron is NULL"); @@ -32,16 +32,12 @@ bool nn_neuron_init(NNNeuron *neuron, const float *weights, size_t n_weights, fl } // nn_neuron_set_weights sets the weights of the given neuron. -bool nn_neuron_set_weights(NNNeuron *neuron, const float *weights, NNError *error) { +bool nn_neuron_set_weights(NNNeuron *neuron, const float weights[NEURON_MAX_WEIGHTS], NNError *error) { nn_error_set(error, NN_ERROR_NONE, NULL); if (neuron == NULL) { nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "neuron is NULL"); return false; } - if (weights == NULL) { - nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "weights is NULL"); - return false; - } for (size_t i = 0; i < neuron->n_weights; ++i) { neuron->weights[i] = weights[i]; } @@ -60,16 +56,12 @@ bool nn_neuron_set_bias(NNNeuron *neuron, float bias, NNError *error) { } // nn_neuron_compute computes the given neuron and returns the output. -float nn_neuron_compute(const NNNeuron *neuron, const float *inputs, NNError *error) { +float nn_neuron_compute(const NNNeuron *neuron, const float inputs[NEURON_MAX_WEIGHTS], NNError *error) { nn_error_set(error, NN_ERROR_NONE, NULL); if (neuron == NULL) { nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "neuron is NULL"); return NAN; } - if (inputs == NULL) { - nn_error_set(error, NN_ERROR_INVALID_INSTANCE, "inputs is NULL"); - return NAN; - } // Initialize the result float result = 0.0f;