Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hashx_endian.h
1#ifndef ENDIAN_H
2#define ENDIAN_H
3
4#include <stdint.h>
5#include <string.h>
6#include "force_inline.h"
7
8/* Argon2 Team - Begin Code */
9/*
10 Not an exhaustive list, but should cover the majority of modern platforms
11 Additionally, the code will always be correct---this is only a performance
12 tweak.
13*/
14#if (defined(__BYTE_ORDER__) && \
15 (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) || \
16 defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
17 defined(__AARCH64EL__) || defined(__amd64__) || defined(__i386__) || \
18 defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) || \
19 defined(_M_ARM)
20#define NATIVE_LITTLE_ENDIAN
21#endif
22/* Argon2 Team - End Code */
23
24static FORCE_INLINE uint32_t load32(const void* src) {
25#if defined(NATIVE_LITTLE_ENDIAN)
26 uint32_t w;
27 memcpy(&w, src, sizeof w);
28 return w;
29#else
30 const uint8_t* p = (const uint8_t*)src;
31 uint32_t w = *p++;
32 w |= (uint32_t)(*p++) << 8;
33 w |= (uint32_t)(*p++) << 16;
34 w |= (uint32_t)(*p++) << 24;
35 return w;
36#endif
37}
38
39static FORCE_INLINE uint64_t load64_native(const void* src) {
40 uint64_t w;
41 memcpy(&w, src, sizeof w);
42 return w;
43}
44
45static FORCE_INLINE uint64_t load64(const void* src) {
46#if defined(NATIVE_LITTLE_ENDIAN)
47 return load64_native(src);
48#else
49 const uint8_t* p = (const uint8_t*)src;
50 uint64_t w = *p++;
51 w |= (uint64_t)(*p++) << 8;
52 w |= (uint64_t)(*p++) << 16;
53 w |= (uint64_t)(*p++) << 24;
54 w |= (uint64_t)(*p++) << 32;
55 w |= (uint64_t)(*p++) << 40;
56 w |= (uint64_t)(*p++) << 48;
57 w |= (uint64_t)(*p++) << 56;
58 return w;
59#endif
60}
61
62static FORCE_INLINE void store32(void* dst, uint32_t w) {
63#if defined(NATIVE_LITTLE_ENDIAN)
64 memcpy(dst, &w, sizeof w);
65#else
66 uint8_t* p = (uint8_t*)dst;
67 *p++ = (uint8_t)w;
68 w >>= 8;
69 *p++ = (uint8_t)w;
70 w >>= 8;
71 *p++ = (uint8_t)w;
72 w >>= 8;
73 *p++ = (uint8_t)w;
74#endif
75}
76
77static FORCE_INLINE void store64_native(void* dst, uint64_t w) {
78 memcpy(dst, &w, sizeof w);
79}
80
81static FORCE_INLINE void store64(void* dst, uint64_t w) {
82#if defined(NATIVE_LITTLE_ENDIAN)
83 store64_native(dst, w);
84#else
85 uint8_t* p = (uint8_t*)dst;
86 *p++ = (uint8_t)w;
87 w >>= 8;
88 *p++ = (uint8_t)w;
89 w >>= 8;
90 *p++ = (uint8_t)w;
91 w >>= 8;
92 *p++ = (uint8_t)w;
93 w >>= 8;
94 *p++ = (uint8_t)w;
95 w >>= 8;
96 *p++ = (uint8_t)w;
97 w >>= 8;
98 *p++ = (uint8_t)w;
99 w >>= 8;
100 *p++ = (uint8_t)w;
101#endif
102}
103#endif