-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.h
143 lines (107 loc) · 4.09 KB
/
utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2021 Bjørn Brodtkorb
#ifndef UTILITIES_H
#define UTILITIES_H
#include "stdarg.h"
#include "stdbool.h"
#include "stdint.h"
//--------------------------------------------------------------------------------------------------
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef float f32;
typedef double f64;
//--------------------------------------------------------------------------------------------------
#define _rw volatile
#define __w volatile
#define __r volatile const
#define limit(value, max) ((value) < (max)) ? (value) : (max)
#define SECTION(x) __attribute__((section(x)))
#define PACKED __attribute__((packed))
#define RAM_FUNCTION __attribute__((noinline, long_call, section(".ramfunc")))
//--------------------------------------------------------------------------------------------------
static inline void memory_copy(const void* source, void* dest, int size) {
for (int i = 0; i < size; i++) {
((u8 *)dest)[i] = ((const u8 *)source)[i];
}
}
//--------------------------------------------------------------------------------------------------
static inline void memory_fill(void* dest, u8 fill, int size) {
for (int i = 0; i < size; i++) {
((u8 *)dest)[i] = fill;
}
}
//--------------------------------------------------------------------------------------------------
static inline bool memory_compare(const void* source1, const void* source2, int size) {
for (int i = 0; i < size; i++) {
if (((u8 *)source1)[i] != ((u8 *)source2)[i]) {
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
static inline void memory_move(const void* source, void* dest, int size) {
const u8* from = source;
u8* to = dest;
if (to == from) {
return;
}
if (to > from) {
for (int i = size; i >= 0; i--) {
to[i] = from[i];
}
}
else {
for (int i = 0; i < size; i++) {
to[i] = from[i];
}
}
}
//--------------------------------------------------------------------------------------------------
static inline u32 read_be32(const void* pointer) {
const u8* source = pointer;
return source[0] << 24 | source[1] << 16 | source[2] << 8 | source[3];
}
//--------------------------------------------------------------------------------------------------
static inline u16 read_be16(const void* pointer) {
const u8* source = pointer;
return source[0] << 8 | source[1];
}
//--------------------------------------------------------------------------------------------------
static inline void write_be32(u32 value, void* pointer) {
u8* dest = pointer;
dest[0] = (value >> 24) & 0xFF;
dest[1] = (value >> 16) & 0xFF;
dest[2] = (value >> 8 ) & 0xFF;
dest[3] = (value >> 0 ) & 0xFF;
}
//--------------------------------------------------------------------------------------------------
static inline void write_be64(u64 value, void* pointer) {
u8* dest = pointer;
dest[0] = (value >> 56) & 0xFF;
dest[1] = (value >> 48) & 0xFF;
dest[2] = (value >> 40) & 0xFF;
dest[3] = (value >> 32) & 0xFF;
dest[4] = (value >> 24) & 0xFF;
dest[5] = (value >> 16) & 0xFF;
dest[6] = (value >> 8 ) & 0xFF;
dest[7] = (value >> 0 ) & 0xFF;
}
//--------------------------------------------------------------------------------------------------
static inline void write_be16(u16 value, void* pointer) {
u8* dest = pointer;
dest[0] = (value >> 8) & 0xFF;
dest[1] = (value >> 0) & 0xFF;
}
//--------------------------------------------------------------------------------------------------
static inline int align_up(int value, int alignment) {
return (value + alignment - 1) / alignment * alignment;
}
//--------------------------------------------------------------------------------------------------
int format_string(const char* string, char* buffer, int buffer_size, va_list arguments);
#endif