@@ -78,6 +78,66 @@ typedef struct bc_struct {
78
78
#define LONG_MAX 0x7ffffff
79
79
#endif
80
80
81
+ /* This will be 0x01010101 for 32-bit and 0x0101010101010101 for 64-bit */
82
+ #define SWAR_ONES (~((size_t) 0) / 0xFF)
83
+ /* This repeats a byte `x` into an entire 32/64-bit word.
84
+ * Example: SWAR_REPEAT(0xAB) will be 0xABABABAB for 32-bit and 0xABABABABABABABAB for 64-bit. */
85
+ #define SWAR_REPEAT (x ) (SWAR_ONES * (x))
86
+
87
+ /* Bytes swap */
88
+ #if defined(_MSC_VER )
89
+ # include <stdlib.h>
90
+ # define BSWAP32 (u ) _byteswap_ulong(u)
91
+ # define BSWAP64 (u ) _byteswap_uint64(u)
92
+ #else
93
+ # ifdef __has_builtin
94
+ # if __has_builtin (__builtin_bswap32 )
95
+ # define BSWAP32 (u ) __builtin_bswap32(u)
96
+ # endif // __has_builtin(__builtin_bswap32)
97
+ # if __has_builtin (__builtin_bswap64 )
98
+ # define BSWAP64 (u ) __builtin_bswap64(u)
99
+ # endif // __has_builtin(__builtin_bswap64)
100
+ # elif defined(__GNUC__ )
101
+ # define BSWAP32 (u ) __builtin_bswap32(u)
102
+ # define BSWAP64 (u ) __builtin_bswap64(u)
103
+ # endif // __has_builtin
104
+ #endif // defined(_MSC_VER)
105
+ #ifndef BSWAP32
106
+ inline uint32_t BSWAP32 (uint32_t u )
107
+ {
108
+ return (((u & 0xff000000 ) >> 24 )
109
+ | ((u & 0x00ff0000 ) >> 8 )
110
+ | ((u & 0x0000ff00 ) << 8 )
111
+ | ((u & 0x000000ff ) << 24 ));
112
+ }
113
+ #endif
114
+ #ifndef BSWAP64
115
+ inline uint64_t BSWAP64 (uint64_t u )
116
+ {
117
+ return (((u & 0xff00000000000000ULL ) >> 56 )
118
+ | ((u & 0x00ff000000000000ULL ) >> 40 )
119
+ | ((u & 0x0000ff0000000000ULL ) >> 24 )
120
+ | ((u & 0x000000ff00000000ULL ) >> 8 )
121
+ | ((u & 0x00000000ff000000ULL ) << 8 )
122
+ | ((u & 0x0000000000ff0000ULL ) << 24 )
123
+ | ((u & 0x000000000000ff00ULL ) << 40 )
124
+ | ((u & 0x00000000000000ffULL ) << 56 ));
125
+ }
126
+ #endif
127
+
128
+ #if SIZEOF_SIZE_T >= 8
129
+ #define BC_BSWAP (u ) BSWAP64(u)
130
+ #define BC_UINT_T uint64_t
131
+ #else
132
+ #define BC_BSWAP (u ) BSWAP32(u)
133
+ #define BC_UINT_T uint32_t
134
+ #endif
135
+
136
+ #ifdef WORDS_BIGENDIAN
137
+ #define BC_LITTLE_ENDIAN 0
138
+ #else
139
+ #define BC_LITTLE_ENDIAN 1
140
+ #endif
81
141
82
142
/* Function Prototypes */
83
143
0 commit comments