Baremetal-NN
Baremetal-NN API documentation
Loading...
Searching...
No Matches
nn.h
Go to the documentation of this file.
1
8#ifndef __NN_H
9#define __NN_H
10
11#include <assert.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <stdint.h>
15#include <string.h>
16#include <math.h>
17
18
19// http://elm-chan.org/junk/32bit/binclude.html
20#ifdef __APPLE__
21#define INCLUDE_FILE(section, filename, symbol) asm (\
22 ".align 4\n" /* Word alignment */\
23 ".globl _"#symbol"_start\n" /* Export the object start address */\
24 ".globl _"#symbol"_data\n" /* Export the object address */\
25 "_"#symbol"_start:\n" /* Define the object start address label */\
26 "_"#symbol"_data:\n" /* Define the object label */\
27 ".incbin \""filename"\"\n" /* Import the file */\
28 ".globl _"#symbol"_end\n" /* Export the object end address */\
29 "_"#symbol"_end:\n" /* Define the object end address label */\
30 ".align 4\n") /* Word alignment */
31#else
32#define INCLUDE_FILE(section, filename, symbol) asm (\
33 ".section "#section"\n" /* Change section */\
34 ".balign 4\n" /* Word alignment */\
35 ".global "#symbol"_start\n" /* Export the object start address */\
36 ".global "#symbol"_data\n" /* Export the object address */\
37 #symbol"_start:\n" /* Define the object start address label */\
38 #symbol"_data:\n" /* Define the object label */\
39 ".incbin \""filename"\"\n" /* Import the file */\
40 ".global "#symbol"_end\n" /* Export the object end address */\
41 #symbol"_end:\n" /* Define the object end address label */\
42 ".balign 4\n" /* Word alignment */\
43 ".section \".text\"\n") /* Restore section */
44#endif
45
54static inline void nn_assert(int condition, char *message) {
55 if (!condition) {
56 printf("Assertion failed: ");
57 printf("%s\n", message);
58 exit(1);
59 }
60}
61
62
71void nn_print_shape(size_t ndim, const size_t *shape) {
72 printf("(");
73 for (size_t i = 0; i < ndim; i += 1) {
74 printf("%d", (int)shape[i]);
75 if (i < ndim-1) {
76 printf(", ");
77 }
78 }
79 printf(")");
80}
81
82
91void nn_print_f32(float v, int16_t num_digits) {
92 if (isinf(v)) {
93 if (signbit(v)) {
94 printf("-inf");
95 } else {
96 printf("inf");
97 }
98 return;
99 }
100
101 if (v < 0) {
102 printf("-"); // Print the minus sign for negative numbers
103 v = -v; // Make the number positive for processing
104 }
105 else {
106 printf(" ");
107 }
108
109 // Calculate the integer part of the number
110 long int_part = (long)v;
111 float fractional_part = v - int_part;
112
113 // Print the integer part
114 printf("%ld", int_part);
115
116 if (num_digits > 0) {
117 printf("."); // Print the decimal point
118 }
119
120 // Handle the fractional part
121 while (num_digits > 0) {
122 num_digits -= 1;
123 fractional_part *= 10;
124 int digit = (int)(fractional_part);
125 printf("%d", digit);
126 fractional_part -= digit;
127 }
128}
129
130#include "nn_f32.h"
131
132#ifdef CONFIG_DTYPE_ENABLE_F16
133 #include "nn_f16.h"
134#endif
135
136#ifdef CONFIG_DTYPE_ENABLE_I32
137 #include "nn_i32.h"
138#endif
139
140
141#endif // __NN_H
void nn_print_f32(float v, int16_t num_digits)
Definition: nn.h:91
void nn_print_shape(size_t ndim, const size_t *shape)
Definition: nn.h:71
static void nn_assert(int condition, char *message)
Definition: nn.h:54