forked from pq-code-package/slhdsa-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplat_local.h
More file actions
265 lines (215 loc) · 5.67 KB
/
Copy pathplat_local.h
File metadata and controls
265 lines (215 loc) · 5.67 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (c) The slhdsa-c project authors
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
*/
/* === Localization affecting all implementations can be inserted here. */
#ifndef _PLAT_LOCAL_H_
#define _PLAT_LOCAL_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
#include <stdint.h>
#ifndef PLAT_VERS_STR
#define PLAT_VERS_STR "dev version"
#define PLAT_VERS_MAJ 0
#define PLAT_VERS_MIN 0
#endif
/* === platform detection logic */
#ifndef PLAT_XLEN
#if defined(__riscv)
#define PLAT_XLEN __riscv_xlen
#if defined(__riscv_vector)
#define PLAT_ARCH_RVV
#define PLAT_ARCH_STR "PLAT_ARCH_RVV"
#elif (__riscv_xlen == 32)
#define PLAT_ARCH_RV32
#define PLAT_ARCH_STR "PLAT_ARCH_RV32"
#else
#define PLAT_ARCH_RV64
#define PLAT_ARCH_STR "PLAT_ARCH_RV64"
#endif
#elif defined(__arm__)
#define PLAT_XLEN 32
#define PLAT_ARCH_STR "PLAT_ARCH_ARM32"
#define PLAT_ARCH_ARM32
#elif defined(__aarch64__)
#define PLAT_XLEN 64
#define PLAT_ARCH_STR "PLAT_ARCH_ARM64"
#define PLAT_ARCH_ARM64
#elif defined(__i386__)
#define PLAT_XLEN 32
#define PLAT_ARCH_STR "PLAT_ARCH_IA32"
#define PLAT_ARCH_IA32
#elif defined(__x86_64__)
#define PLAT_XLEN 64
#define PLAT_ARCH_STR "PLAT_ARCH_X64"
#define PLAT_ARCH_X64
#elif (__WORDSIZE == 32)
#define PLAT_XLEN 32
#define PLAT_ARCH_STR "PLAT_ARCH_GEN32"
#define PLAT_ARCH_GEN32
#else
/* fallback for everything */
#define PLAT_XLEN 64
#define PLAT_ARCH_STR "PLAT_ARCH_GEN64"
#define PLAT_ARCH_GEN64
#endif
/* PLAT_XLEN signals that the rest of the macros are defined too */
#endif
/* === Assume-Assert checks */
/* No-op for production */
#ifndef __CPROVER__
#ifndef XDEBUG
#define XASSERT(x)
#define XASSUME(x)
#define XTMPVAR(x)
/* Runtime checks in debug build */
#else
#include <assert.h>
#define XASSERT(x) assert(x)
#define XASSUME(x) assert(x)
#define XTMPVAR(x) x
/* XDEBUG */
#endif
/* For CBMC (formal checks) */
#else
#include <assert.h>
#include <stdio.h>
#define XASSERT(x) assert(x)
#define XASSUME(x) __CPROVER_assume(x)
#define XTMPVAR(x) x
#define XPROOFS
#endif /* __CPROVER__ */
/* revert if not big endian */
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
/* nop */
#define rev8_be32(x) (x)
#else
/* RISC-V: grev(x, 0x18) or rev8 */
static inline uint32_t rev8_be32(uint32_t x)
{
return ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) |
((x & 0x0000FF00) << 8) | ((x & 0x000000FF) << 24);
}
#endif
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define rev8_be64(x) (x)
#else
/* RISC-V: grev(x, 0x38) or rev8(x) */
static inline uint64_t rev8_be64(uint64_t x)
{
return (x << 56) | ((x & 0x000000000000FF00LL) << 40) |
((x & 0x0000000000FF0000LL) << 24) |
((x & 0x00000000FF000000LL) << 8) | ((x & 0x000000FF00000000LL) >> 8) |
((x & 0x0000FF0000000000LL) >> 24) |
((x & 0x00FF000000000000LL) >> 40) | (x >> 56);
}
#endif
/* rotate left */
static inline uint32_t rol32(uint32_t x, uint32_t n)
{
return (x << n) | (x >> (32 - n));
}
static inline uint64_t rol64(uint64_t x, uint64_t n)
{
return (x << n) | (x >> (64 - n));
}
/* rotate right (RISC-V ROR or RORI) */
static inline uint32_t ror32(uint32_t x, uint32_t n)
{
return (x >> n) | (x << (32 - n));
}
static inline uint64_t ror64(uint64_t x, uint64_t n)
{
return (x >> n) | (x << (64 - n));
}
/* and with negate (RISC-V ANDN) */
static inline uint32_t andn32(uint32_t x, uint32_t y) { return x & ~y; }
static inline uint64_t andn64(uint64_t x, uint64_t y) { return x & ~y; }
/* little-endian loads and stores (unaligned) */
static inline uint16_t get16u_le(const uint8_t *v)
{
return (((uint16_t)v[1]) << 8) | ((uint16_t)v[0]);
}
static inline void put16u_le(uint8_t *v, uint16_t x)
{
v[0] = x;
v[1] = x >> 8;
}
static inline uint32_t get32u_le(const uint8_t *v)
{
return ((uint32_t)v[0]) | (((uint32_t)v[1]) << 8) |
(((uint32_t)v[2]) << 16) | (((uint32_t)v[3]) << 24);
}
static inline void put32u_le(uint8_t *v, uint32_t x)
{
v[0] = x;
v[1] = x >> 8;
v[2] = x >> 16;
v[3] = x >> 24;
}
static inline uint64_t get64u_le(const uint8_t *v)
{
return ((uint64_t)v[0]) | (((uint64_t)v[1]) << 8) |
(((uint64_t)v[2]) << 16) | (((uint64_t)v[3]) << 24) |
(((uint64_t)v[4]) << 32) | (((uint64_t)v[5]) << 40) |
(((uint64_t)v[6]) << 48) | (((uint64_t)v[7]) << 56);
}
static inline void put64u_le(uint8_t *v, uint64_t x)
{
v[0] = x;
v[1] = x >> 8;
v[2] = x >> 16;
v[3] = x >> 24;
v[4] = x >> 32;
v[5] = x >> 40;
v[6] = x >> 48;
v[7] = x >> 56;
}
/* big-endian loads and stores (unaligned) */
static inline uint16_t get16u_be(const uint8_t *v)
{
return (((uint16_t)v[0]) << 8) | ((uint16_t)v[1]);
}
static inline void put16u_be(uint8_t *v, uint16_t x)
{
v[0] = x >> 8;
v[1] = x;
}
static inline uint32_t get32u_be(const uint8_t *v)
{
return (((uint32_t)v[0]) << 24) | (((uint32_t)v[1]) << 16) |
(((uint32_t)v[2]) << 8) | ((uint32_t)v[3]);
}
static inline void put32u_be(uint8_t *v, uint32_t x)
{
v[0] = x >> 24;
v[1] = x >> 16;
v[2] = x >> 8;
v[3] = x;
}
static inline uint64_t get64u_be(const uint8_t *v)
{
return (((uint64_t)v[0]) << 56) | (((uint64_t)v[1]) << 48) |
(((uint64_t)v[2]) << 40) | (((uint64_t)v[3]) << 32) |
(((uint64_t)v[4]) << 24) | (((uint64_t)v[5]) << 16) |
(((uint64_t)v[6]) << 8) | ((uint64_t)v[7]);
}
static inline void put64u_be(uint8_t *v, uint64_t x)
{
v[0] = x >> 56;
v[1] = x >> 48;
v[2] = x >> 40;
v[3] = x >> 32;
v[4] = x >> 24;
v[5] = x >> 16;
v[6] = x >> 8;
v[7] = x;
}
#ifdef __cplusplus
}
#endif
/* _PLAT_LOCAL_H_ */
#endif