Skip to content

Commit 536d2f7

Browse files
committed
Added some definitions to bcmath.h
Moved SWAR_ONES and SWAR_REPEAT to header, and defined bytes swap etc.
1 parent f97bd07 commit 536d2f7

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
@@ -78,6 +78,66 @@ typedef struct bc_struct {
7878
#define LONG_MAX 0x7ffffff
7979
#endif
8080

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
81141

82142
/* Function Prototypes */
83143

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)