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
38typedef union {
39 double d;
40 uint8_t b[8];
41} double_p;
42
52class DiwaConv final {
53public:
62 static inline uint8_t* intToU8a(int value) {
63 uint8_t* bytes = new uint8_t[4];
64
65 bytes[0] = (value >> 0) & 0xFF;
66 bytes[1] = (value >> 8) & 0xFF;
67 bytes[2] = (value >> 16) & 0xFF;
68 bytes[3] = (value >> 24) & 0xFF;
69
70 return bytes;
71 }
72
81 static inline int u8aToInt(uint8_t bytes[4]) {
82 int result = 0;
83
84 result |= bytes[0];
85 result |= bytes[1] << 8;
86 result |= bytes[2] << 16;
87 result |= bytes[3] << 24;
88
89 return result;
90 }
91
100 static inline uint8_t* doubleToU8a(double value) {
101 double_p db;
102 db.d = value;
103
104 uint8_t* bytes = new uint8_t[8];
105 for(uint8_t i = 0; i < 8; ++i)
106 bytes[i] = db.b[i];
107
108 return bytes;
109 }
110
119 static inline double u8aToDouble(uint8_t bytes[8]) {
120 double_p db;
121 for(uint8_t i = 0; i < 8; ++i)
122 db.b[i] = bytes[i];
123
124 return db.d;
125 }
126};
127
128#endif // DIWA_CONV_H