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#if defined(__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#elif defined(ARDUINO)
32 #define INCBIN_PREFIX
33 #define INCBIN_STYLE INCBIN_STYLE_SNAKE
34 #include "incbin.h"
35 #define INCLUDE_FILE(section, filename, symbol) INCBIN(symbol, filename)
36#else
37 #define INCLUDE_FILE(section, filename, symbol) asm (\
38 ".section "#section"\n" /* Change section */\
39 ".balign 4\n" /* Word alignment */\
40 ".global "#symbol"_start\n" /* Export the object start address */\
41 ".global "#symbol"_data\n" /* Export the object address */\
42 #symbol"_start:\n" /* Define the object start address label */\
43 #symbol"_data:\n" /* Define the object label */\
44 ".incbin \""filename"\"\n" /* Import the file */\
45 ".global "#symbol"_end\n" /* Export the object end address */\
46 #symbol"_end:\n" /* Define the object end address label */\
47 ".balign 4\n" /* Word alignment */\
48 ".section \".text\"\n") /* Restore section */
49#endif
50
59static inline void nn_assert(int condition, char *message) {
60 if (!condition) {
61 printf("Assertion failed: ");
62 printf("%s\n", message);
63 exit(1);
64 }
65}
66
67
76void nn_print_shape(size_t ndim, const size_t *shape) {
77 printf("(");
78 for (size_t i = 0; i < ndim; i += 1) {
79 printf("%d", (int)shape[i]);
80 if (i < ndim-1) {
81 printf(", ");
82 }
83 }
84 printf(")");
85}
86
87
96void nn_print_f32(float v, int16_t num_digits) {
97 if (isinf(v)) {
98 if (signbit(v)) {
99 printf("-inf");
100 } else {
101 printf("inf");
102 }
103 return;
104 }
105
106 if (v < 0) {
107 printf("-"); // Print the minus sign for negative numbers
108 v = -v; // Make the number positive for processing
109 }
110 else {
111 printf(" ");
112 }
113
114 // Calculate the integer part of the number
115 long int_part = (long)v;
116 float fractional_part = v - int_part;
117
118 // Print the integer part
119 printf("%ld", int_part);
120
121 if (num_digits > 0) {
122 printf("."); // Print the decimal point
123 }
124
125 // Handle the fractional part
126 while (num_digits > 0) {
127 num_digits -= 1;
128 fractional_part *= 10;
129 int digit = (int)(fractional_part);
130 printf("%d", digit);
131 fractional_part -= digit;
132 }
133}
134
135#include "nn_f32.h"
136
137#ifdef CONFIG_DTYPE_ENABLE_F16
138 #include "nn_f16.h"
139#endif
140
141#ifdef CONFIG_DTYPE_ENABLE_I32
142 #include "nn_i32.h"
143#endif
144
145
146#endif // __NN_H
void nn_print_f32(float v, int16_t num_digits)
Definition: nn.h:96
void nn_print_shape(size_t ndim, const size_t *shape)
Definition: nn.h:76
static void nn_assert(int condition, char *message)
Definition: nn.h:59
Baremetal-NN Library functions for half-precision floating-point (fp16) numbers.
Baremetal-NN Library functions for single-precision floating-point (fp32) numbers.
Baremetal-NN Library functions for signed long integer (i32) numbers.