Diwa
Lightweight implementation of Artificial Neural Network for resource-constrained environments
Loading...
Searching...
No Matches
diwa_conv.h
Go to the documentation of this file.
1/*
2 * This file is part of the Diwa library.
3 * Copyright (c) 2024 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
34#ifndef DIWA_UTIL_H
35#define DIWA_UTIL_H
36
37#include <stdint.h>
38
40typedef union {
41 double d;
42 uint8_t b[8];
43} double_p;
44
54class DiwaConv final {
55public:
64 static inline uint8_t* intToU8a(int value) {
65 uint8_t* bytes = new uint8_t[4];
66
67 bytes[0] = (value >> 0) & 0xFF;
68 bytes[1] = (value >> 8) & 0xFF;
69 bytes[2] = (value >> 16) & 0xFF;
70 bytes[3] = (value >> 24) & 0xFF;
71
72 return bytes;
73 }
74
83 static inline int u8aToInt(uint8_t bytes[4]) {
84 int result = 0;
85
86 result |= bytes[0];
87 result |= bytes[1] << 8;
88 result |= bytes[2] << 16;
89 result |= bytes[3] << 24;
90
91 return result;
92 }
93
102 static inline uint8_t* doubleToU8a(double value) {
103 double_p db;
104 db.d = value;
105
106 uint8_t* bytes = new uint8_t[8];
107 for(uint8_t i = 0; i < 8; ++i)
108 bytes[i] = db.b[i];
109
110 return bytes;
111 }
112
121 static inline double u8aToDouble(uint8_t bytes[8]) {
122 double_p db;
123 for(uint8_t i = 0; i < 8; ++i)
124 db.b[i] = bytes[i];
125
126 return db.d;
127 }
128};
129
130#endif // DIWA_CONV_H