Skip to content

Commit 34e0c77

Browse files
committed
Added some definitions to bcmath.h
Moved SWAR_ONES and SWAR_REPEAT to header, and defined bytes swap etc.
1 parent f442cec commit 34e0c77

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,66 @@ typedef struct bc_struct {
7979
#define LONG_MAX 0x7ffffff
8080
#endif
8181

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

83143
/* Function Prototypes */
84144

ext/bcmath/libbcmath/src/convert.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
# include <emmintrin.h>
2121
#endif
2222

23-
/* This will be 0x01010101 for 32-bit and 0x0101010101010101 */
24-
#define SWAR_ONES (~((size_t) 0) / 0xFF)
25-
/* This repeats a byte `x` into an entire 32/64-bit word.
26-
* Example: SWAR_REPEAT(0xAB) will be 0xABABABAB for 32-bit and 0xABABABABABABABAB for 64-bit. */
27-
#define SWAR_REPEAT(x) (SWAR_ONES * (x))
28-
2923
static char *bc_copy_and_shift_numbers(char *restrict dest, const char *source, const char *source_end, unsigned char shift, bool add)
3024
{
3125
size_t bulk_shift = SWAR_REPEAT(shift);

0 commit comments

Comments
 (0)