Monero
int-util.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <string.h>
37 
38 #ifndef _MSC_VER
39 #include <sys/param.h>
40 #endif
41 
42 #if defined(__ANDROID__)
43 #include <byteswap.h>
44 #endif
45 
46 #if defined(__sun) && defined(__SVR4)
47 #include <endian.h>
48 #endif
49 
50 #if defined(_MSC_VER)
51 #include <stdlib.h>
52 
53 static inline uint32_t rol32(uint32_t x, int r) {
54  static_assert(sizeof(uint32_t) == sizeof(unsigned int), "this code assumes 32-bit integers");
55  return _rotl(x, r);
56 }
57 
58 static inline uint64_t rol64(uint64_t x, int r) {
59  return _rotl64(x, r);
60 }
61 
62 #else
63 
64 static inline uint32_t rol32(uint32_t x, int r) {
65  return (x << (r & 31)) | (x >> (-r & 31));
66 }
67 
68 static inline uint64_t rol64(uint64_t x, int r) {
69  return (x << (r & 63)) | (x >> (-r & 63));
70 }
71 
72 #endif
73 
74 static inline uint64_t hi_dword(uint64_t val) {
75  return val >> 32;
76 }
77 
78 static inline uint64_t lo_dword(uint64_t val) {
79  return val & 0xFFFFFFFF;
80 }
81 
82 static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
83  // multiplier = ab = a * 2^32 + b
84  // multiplicand = cd = c * 2^32 + d
85  // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
86  uint64_t a = hi_dword(multiplier);
87  uint64_t b = lo_dword(multiplier);
88  uint64_t c = hi_dword(multiplicand);
89  uint64_t d = lo_dword(multiplicand);
90 
91  uint64_t ac = a * c;
92  uint64_t ad = a * d;
93  uint64_t bc = b * c;
94  uint64_t bd = b * d;
95 
96  uint64_t adbc = ad + bc;
97  uint64_t adbc_carry = adbc < ad ? 1 : 0;
98 
99  // multiplier * multiplicand = product_hi * 2^64 + product_lo
100  uint64_t product_lo = bd + (adbc << 32);
101  uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
102  *product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
103  assert(ac <= *product_hi);
104 
105  return product_lo;
106 }
107 
108 static inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
109  dividend |= ((uint64_t)*remainder) << 32;
110  *remainder = dividend % divisor;
111  return dividend / divisor;
112 }
113 
114 // Long division with 2^32 base
115 static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
116  uint64_t dividend_dwords[4];
117  uint32_t remainder = 0;
118 
119  dividend_dwords[3] = hi_dword(dividend_hi);
120  dividend_dwords[2] = lo_dword(dividend_hi);
121  dividend_dwords[1] = hi_dword(dividend_lo);
122  dividend_dwords[0] = lo_dword(dividend_lo);
123 
124  *quotient_hi = div_with_reminder(dividend_dwords[3], divisor, &remainder) << 32;
125  *quotient_hi |= div_with_reminder(dividend_dwords[2], divisor, &remainder);
126  *quotient_lo = div_with_reminder(dividend_dwords[1], divisor, &remainder) << 32;
127  *quotient_lo |= div_with_reminder(dividend_dwords[0], divisor, &remainder);
128 
129  return remainder;
130 }
131 
132 #define IDENT32(x) ((uint32_t) (x))
133 #define IDENT64(x) ((uint64_t) (x))
134 
135 #define SWAP32(x) ((((uint32_t) (x) & 0x000000ff) << 24) | \
136  (((uint32_t) (x) & 0x0000ff00) << 8) | \
137  (((uint32_t) (x) & 0x00ff0000) >> 8) | \
138  (((uint32_t) (x) & 0xff000000) >> 24))
139 #define SWAP64(x) ((((uint64_t) (x) & 0x00000000000000ff) << 56) | \
140  (((uint64_t) (x) & 0x000000000000ff00) << 40) | \
141  (((uint64_t) (x) & 0x0000000000ff0000) << 24) | \
142  (((uint64_t) (x) & 0x00000000ff000000) << 8) | \
143  (((uint64_t) (x) & 0x000000ff00000000) >> 8) | \
144  (((uint64_t) (x) & 0x0000ff0000000000) >> 24) | \
145  (((uint64_t) (x) & 0x00ff000000000000) >> 40) | \
146  (((uint64_t) (x) & 0xff00000000000000) >> 56))
147 
148 static inline uint32_t ident32(uint32_t x) { return x; }
149 static inline uint64_t ident64(uint64_t x) { return x; }
150 
151 #ifndef __OpenBSD__
152 # if defined(__ANDROID__) && defined(__swap32) && !defined(swap32)
153 # define swap32 __swap32
154 # elif !defined(swap32)
155 static inline uint32_t swap32(uint32_t x) {
156  x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8);
157  return (x << 16) | (x >> 16);
158 }
159 # endif
160 # if defined(__ANDROID__) && defined(__swap64) && !defined(swap64)
161 # define swap64 __swap64
162 # elif !defined(swap64)
163 static inline uint64_t swap64(uint64_t x) {
164  x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8);
165  x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16);
166  return (x << 32) | (x >> 32);
167 }
168 # endif
169 #endif /* __OpenBSD__ */
170 
171 #if defined(__GNUC__)
172 #define UNUSED __attribute__((unused))
173 #else
174 #define UNUSED
175 #endif
176 static inline void mem_inplace_ident(void *mem UNUSED, size_t n UNUSED) { }
177 #undef UNUSED
178 
179 static inline void mem_inplace_swap32(void *mem, size_t n) {
180  size_t i;
181  for (i = 0; i < n; i++) {
182  ((uint32_t *) mem)[i] = swap32(((const uint32_t *) mem)[i]);
183  }
184 }
185 static inline void mem_inplace_swap64(void *mem, size_t n) {
186  size_t i;
187  for (i = 0; i < n; i++) {
188  ((uint64_t *) mem)[i] = swap64(((const uint64_t *) mem)[i]);
189  }
190 }
191 
192 static inline void memcpy_ident32(void *dst, const void *src, size_t n) {
193  memcpy(dst, src, 4 * n);
194 }
195 static inline void memcpy_ident64(void *dst, const void *src, size_t n) {
196  memcpy(dst, src, 8 * n);
197 }
198 
199 static inline void memcpy_swap32(void *dst, const void *src, size_t n) {
200  size_t i;
201  for (i = 0; i < n; i++) {
202  ((uint32_t *) dst)[i] = swap32(((const uint32_t *) src)[i]);
203  }
204 }
205 static inline void memcpy_swap64(void *dst, const void *src, size_t n) {
206  size_t i;
207  for (i = 0; i < n; i++) {
208  ((uint64_t *) dst)[i] = swap64(((const uint64_t *) src)[i]);
209  }
210 }
211 
212 #ifdef _MSC_VER
213 # define LITTLE_ENDIAN 1234
214 # define BIG_ENDIAN 4321
215 # define BYTE_ORDER LITTLE_ENDIAN
216 #endif
217 
218 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN)
219 static_assert(false, "BYTE_ORDER is undefined. Perhaps, GNU extensions are not enabled");
220 #endif
221 
222 #if BYTE_ORDER == LITTLE_ENDIAN
223 #define SWAP32LE IDENT32
224 #define SWAP32BE SWAP32
225 #define swap32le ident32
226 #define swap32be swap32
227 #define mem_inplace_swap32le mem_inplace_ident
228 #define mem_inplace_swap32be mem_inplace_swap32
229 #define memcpy_swap32le memcpy_ident32
230 #define memcpy_swap32be memcpy_swap32
231 #define SWAP64LE IDENT64
232 #define SWAP64BE SWAP64
233 #define swap64le ident64
234 #define swap64be swap64
235 #define mem_inplace_swap64le mem_inplace_ident
236 #define mem_inplace_swap64be mem_inplace_swap64
237 #define memcpy_swap64le memcpy_ident64
238 #define memcpy_swap64be memcpy_swap64
239 #endif
240 
241 #if BYTE_ORDER == BIG_ENDIAN
242 #define SWAP32BE IDENT32
243 #define SWAP32LE SWAP32
244 #define swap32be ident32
245 #define swap32le swap32
246 #define mem_inplace_swap32be mem_inplace_ident
247 #define mem_inplace_swap32le mem_inplace_swap32
248 #define memcpy_swap32be memcpy_ident32
249 #define memcpy_swap32le memcpy_swap32
250 #define SWAP64BE IDENT64
251 #define SWAP64LE SWAP64
252 #define swap64be ident64
253 #define swap64le swap64
254 #define mem_inplace_swap64be mem_inplace_ident
255 #define mem_inplace_swap64le mem_inplace_swap64
256 #define memcpy_swap64be memcpy_ident64
257 #define memcpy_swap64le memcpy_swap64
258 #endif
int d
Definition: base.py:14
static void mem_inplace_ident(void *mem UNUSED, size_t n UNUSED)
Definition: int-util.h:176
static void memcpy_ident32(void *dst, const void *src, size_t n)
Definition: int-util.h:192
static void memcpy_swap64(void *dst, const void *src, size_t n)
Definition: int-util.h:205
static uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t *quotient_hi, uint64_t *quotient_lo)
Definition: int-util.h:115
static void mem_inplace_swap64(void *mem, size_t n)
Definition: int-util.h:185
Definition: d.py:1
static void memcpy_ident64(void *dst, const void *src, size_t n)
Definition: int-util.h:195
static uint32_t rol32(uint32_t x, int r)
Definition: int-util.h:64
static uint64_t swap64(uint64_t x)
Definition: int-util.h:163
static uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
Definition: int-util.h:108
int b
Definition: base.py:1
static void mem_inplace_swap32(void *mem, size_t n)
Definition: int-util.h:179
static uint64_t lo_dword(uint64_t val)
Definition: int-util.h:78
static uint32_t swap32(uint32_t x)
Definition: int-util.h:155
static uint64_t hi_dword(uint64_t val)
Definition: int-util.h:74
static uint64_t ident64(uint64_t x)
Definition: int-util.h:149
static uint64_t rol64(uint64_t x, int r)
Definition: int-util.h:68
string a
Definition: MakeCryptoOps.py:15
#define UNUSED
Definition: int-util.h:174
static void memcpy_swap32(void *dst, const void *src, size_t n)
Definition: int-util.h:199
static uint32_t ident32(uint32_t x)
Definition: int-util.h:148
static uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi)
Definition: int-util.h:82