chiark / gitweb /
ec-field-test.c: Make the field-element type use internal format.
[secnet.git] / fake-mLib-bits.h
1 /*
2  * This file is part of secnet.
3  * See README for full list of copyright holders.
4  *
5  * secnet is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version d of the License, or
8  * (at your option) any later version.
9  *
10  * secnet is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 3 along with secnet; if not, see
17  * https://www.gnu.org/licenses/gpl.html.
18  */
19
20 #ifndef fake_mLib_bits_h
21 #define fake_mLib_bits_h
22
23 /* The <mLib/bits.h> header defines a large number of types and macros for
24  * various kinds of bithacking.  Notably, it has machinery for autodetecting
25  * suitable types holding (at least) various numbers of bits -- a service
26  * which C99 provides through other means.
27  *
28  * This file provides a small portion of the <mLib/bits.h> interface, just
29  * enough to make the pieces of code lifted from Catacomb feel at home.
30  */
31
32 #include <stdint.h>
33 #include "u64.h"
34 #include "unaligned.h"
35
36 typedef uint8_t octet;
37 typedef uint32_t uint32;
38 typedef uint64_t uint64;
39
40 typedef int32_t int32;
41 typedef int64_t int64;
42
43 #define LOAD32_L(p) (get_uint32_le(p))
44 #define LOAD32_B(p) (get_uint32(p))
45 #define STORE32_L(p, x) put_uint32_le((p), (x))
46 #define STORE32_B(p, x) put_uint32((p), (x))
47 #define ROL32(x, n) ((n) ? ((x) << (n) | (x) >> (32 - (n))) : (x))
48
49 #define MASK32 0xffffffffu
50 #define U32(x) ((uint32)(x) & MASK32)
51
52 typedef u64 kludge64;
53 #define X64(hi, lo) u64init(0x##hi, 0x##lo)
54 #define HI64(x) u64gethi(x)
55 #define LO64(x) u64getlo(x)
56 #define LOAD64_L_(x, p) do {                                            \
57     const uint8_t *p_ = (const uint8_t *)(p);                           \
58     uint32_t lo_ = LOAD32_L(p_ + 0), hi_ = LOAD32_L(p_ + 4);            \
59     (x) = u64hilo(hi_, lo_);                                            \
60 } while (0)
61 #define LOAD64_B_(x, p) do {                                            \
62     const uint8_t *p_ = (const uint8_t *)(p);                           \
63     uint32_t hi_ = LOAD32_B(p_ + 0), lo_ = LOAD32_B(p_ + 4);            \
64     (x) = u64hilo(hi_, lo_);                                            \
65 } while (0)
66 #define STORE64_L_(p, x) do {                                           \
67     uint8_t *p_ = (uint8_t *)(p);                                       \
68     uint32_t lo_ = LO64(x), hi_ = HI64(x);                              \
69     STORE32_L(p_ + 0, lo_); STORE32_L(p_ + 4, hi_);                     \
70 } while (0)
71 #define STORE64_B_(p, x) do {                                           \
72     uint8_t *p_ = (uint8_t *)(p);                                       \
73     uint32_t lo_ = LO64(x), hi_ = HI64(x);                              \
74     STORE32_B(p_ + 0, hi_); STORE32_B(p_ + 4, lo_);                     \
75 } while (0)
76 #define SET64(z, hi, lo) ((z) = u64hilo((hi), (lo)))
77 #define AND64(z, x, y) ((z) = u64and((x), (y)))
78 #define OR64(z, x, y) ((z) = u64or((x), (y)))
79 #define XOR64(z, x, y) ((z) = u64xor((x), (y)))
80 #define CPL64(z, x) ((z) = u64not((x)))
81 #define ROL64_(z, x, n) ((n) ? (z) = u64rol((x), (n)) : (x))
82
83 #endif /* fake_mLib_bits_h */