Skip to content

Commit 1e45007

Browse files
committed
In the arm processor environment, NEON is used to use SIMD.
1 parent 8622362 commit 1e45007

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-32
lines changed

ext/bcmath/libbcmath/src/convert.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@
1717
#include "bcmath.h"
1818
#include "convert.h"
1919
#include "private.h"
20-
#ifdef __SSE2__
21-
# include <emmintrin.h>
22-
#endif
2320

2421
char *bc_copy_and_toggle_bcd(char *restrict dest, const char *source, const char *source_end)
2522
{
2623
const size_t bulk_shift = SWAR_REPEAT('0');
2724

28-
#ifdef __SSE2__
29-
/* SIMD SSE2 bulk shift + copy */
30-
__m128i shift_vector = _mm_set1_epi8('0');
31-
while (source + sizeof(__m128i) <= source_end) {
32-
__m128i bytes = _mm_loadu_si128((const __m128i *) source);
33-
bytes = _mm_xor_si128(bytes, shift_vector);
34-
_mm_storeu_si128((__m128i *) dest, bytes);
25+
#ifdef HAVE_BC_SIMD128_T
26+
/* SIMD SSE2 of NEON bulk shift + copy */
27+
bc_simd_128_t shift_vector = bc_set_8x16('0');
28+
while (source + sizeof(bc_simd_128_t) <= source_end) {
29+
bc_simd_128_t bytes = bc_load_u128((const bc_simd_128_t *) source);
30+
bytes = bc_xor_128(bytes, shift_vector);
31+
bc_store_u128((bc_simd_128_t *) dest, bytes);
3532

36-
source += sizeof(__m128i);
37-
dest += sizeof(__m128i);
33+
source += sizeof(bc_simd_128_t);
34+
dest += sizeof(bc_simd_128_t);
3835
}
3936
#endif
4037

ext/bcmath/libbcmath/src/private.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,53 @@
6969

7070
#define BC_ARR_SIZE_FROM_LEN(len) (((len) + BC_VECTOR_SIZE - 1) / BC_VECTOR_SIZE)
7171

72+
#ifdef __SSE2__
73+
# include <emmintrin.h>
74+
typedef __m128i bc_simd_128_t;
75+
# define HAVE_BC_SIMD128_T
76+
# define bc_set_8x16(x) _mm_set1_epi8(x)
77+
# define bc_load_u128(ptr) _mm_loadu_si128((const __m128i *) (ptr))
78+
# define bc_xor_128(a, b) _mm_xor_si128(a, b)
79+
# define bc_store_u128(ptr, val) _mm_storeu_si128((__m128i *) (ptr), val)
80+
# define bc_add_8x16(a, b) _mm_add_epi8(a, b)
81+
# define bc_cmpeq_8x16(a, b) _mm_cmpeq_epi8(a, b)
82+
# define bc_cmplt_8x16(a, b) _mm_cmplt_epi8(a, b)
83+
# define bc_movemask_8x16(a) _mm_movemask_epi8(a)
84+
#elif defined(__aarch64__) || defined(_M_ARM64)
85+
# include <arm_neon.h>
86+
typedef int8x16_t bc_simd_128_t;
87+
# define HAVE_BC_SIMD128_T
88+
# define bc_set_8x16(x) vdupq_n_s8(x)
89+
# define bc_load_u128(ptr) vld1q_s8((const int8_t *) (ptr))
90+
# define bc_xor_128(a, b) veorq_s8(a, b)
91+
# define bc_store_u128(ptr, val) vst1q_s8((int8_t *) (ptr), val)
92+
# define bc_add_8x16(a, b) vaddq_s8(a, b)
93+
# define bc_cmpeq_8x16(a, b) (vreinterpretq_s8_u8(vceqq_s8(a, b)))
94+
# define bc_cmplt_8x16(a, b) (vreinterpretq_s8_u8(vcltq_s8(a, b)))
95+
static inline int bc_movemask_8x16(int8x16_t vec)
96+
{
97+
// bulk shift right by 7
98+
uint8x16_t uvec = vshrq_n_u8(vreinterpretq_u8_s8(vec), 7);
99+
return
100+
(vgetq_lane_u8(uvec, 0) << 0) |
101+
(vgetq_lane_u8(uvec, 1) << 1) |
102+
(vgetq_lane_u8(uvec, 2) << 2) |
103+
(vgetq_lane_u8(uvec, 3) << 3) |
104+
(vgetq_lane_u8(uvec, 4) << 4) |
105+
(vgetq_lane_u8(uvec, 5) << 5) |
106+
(vgetq_lane_u8(uvec, 6) << 6) |
107+
(vgetq_lane_u8(uvec, 7) << 7) |
108+
(vgetq_lane_u8(uvec, 8) << 8) |
109+
(vgetq_lane_u8(uvec, 9) << 9) |
110+
(vgetq_lane_u8(uvec, 10) << 10) |
111+
(vgetq_lane_u8(uvec, 11) << 11) |
112+
(vgetq_lane_u8(uvec, 12) << 12) |
113+
(vgetq_lane_u8(uvec, 13) << 13) |
114+
(vgetq_lane_u8(uvec, 14) << 14) |
115+
(vgetq_lane_u8(uvec, 15) << 15);
116+
}
117+
#endif
118+
72119
/*
73120
* Adding more than this many times may cause uint32_t/uint64_t to overflow.
74121
* Typically this is 1844 for 64bit and 42 for 32bit.

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,25 @@
3434
#include "private.h"
3535
#include <stdbool.h>
3636
#include <stddef.h>
37-
#ifdef __SSE2__
38-
# include <emmintrin.h>
39-
#endif
4037

4138
/* Convert strings to bc numbers. Base 10 only.*/
4239
static const char *bc_count_digits(const char *str, const char *end)
4340
{
4441
/* Process in bulk */
45-
#ifdef __SSE2__
46-
const __m128i offset = _mm_set1_epi8((signed char) (SCHAR_MIN - '0'));
42+
#ifdef HAVE_BC_SIMD128_T
43+
const bc_simd_128_t offset = bc_set_8x16((signed char) (SCHAR_MIN - '0'));
4744
/* we use the less than comparator, so add 1 */
48-
const __m128i threshold = _mm_set1_epi8(SCHAR_MIN + ('9' + 1 - '0'));
45+
const bc_simd_128_t threshold = bc_set_8x16(SCHAR_MIN + ('9' + 1 - '0'));
4946

50-
while (str + sizeof(__m128i) <= end) {
51-
__m128i bytes = _mm_loadu_si128((const __m128i *) str);
47+
while (str + sizeof(bc_simd_128_t) <= end) {
48+
bc_simd_128_t bytes = bc_load_u128((const bc_simd_128_t *) str);
5249
/* Wrapping-add the offset to the bytes, such that all bytes below '0' are positive and others are negative.
5350
* More specifically, '0' will be -128 and '9' will be -119. */
54-
bytes = _mm_add_epi8(bytes, offset);
51+
bytes = bc_add_8x16(bytes, offset);
5552
/* Now mark all bytes that are <= '9', i.e. <= -119, i.e. < -118, i.e. the threshold. */
56-
bytes = _mm_cmplt_epi8(bytes, threshold);
53+
bytes = bc_cmplt_8x16(bytes, threshold);
5754

58-
int mask = _mm_movemask_epi8(bytes);
55+
int mask = bc_movemask_8x16(bytes);
5956
if (mask != 0xffff) {
6057
/* At least one of the bytes is not within range. Move to the first offending byte. */
6158
#ifdef PHP_HAVE_BUILTIN_CTZL
@@ -65,7 +62,7 @@ static const char *bc_count_digits(const char *str, const char *end)
6562
#endif
6663
}
6764

68-
str += sizeof(__m128i);
65+
str += sizeof(bc_simd_128_t);
6966
}
7067
#endif
7168

@@ -79,19 +76,19 @@ static const char *bc_count_digits(const char *str, const char *end)
7976
static inline const char *bc_skip_zero_reverse(const char *scanner, const char *stop)
8077
{
8178
/* Check in bulk */
82-
#ifdef __SSE2__
83-
const __m128i c_zero_repeat = _mm_set1_epi8('0');
84-
while (scanner - sizeof(__m128i) >= stop) {
85-
scanner -= sizeof(__m128i);
86-
__m128i bytes = _mm_loadu_si128((const __m128i *) scanner);
79+
#ifdef HAVE_BC_SIMD128_T
80+
const bc_simd_128_t c_zero_repeat = bc_set_8x16('0');
81+
while (scanner - sizeof(bc_simd_128_t) >= stop) {
82+
scanner -= sizeof(bc_simd_128_t);
83+
bc_simd_128_t bytes = bc_load_u128((const bc_simd_128_t *) scanner);
8784
/* Checks if all numeric strings are equal to '0'. */
88-
bytes = _mm_cmpeq_epi8(bytes, c_zero_repeat);
85+
bytes = bc_cmpeq_8x16(bytes, c_zero_repeat);
8986

90-
int mask = _mm_movemask_epi8(bytes);
87+
int mask = bc_movemask_8x16(bytes);
9188
/* The probability of having 16 trailing 0s in a row is very low, so we use EXPECTED. */
9289
if (EXPECTED(mask != 0xffff)) {
9390
/* Move the pointer back and check each character in loop. */
94-
scanner += sizeof(__m128i);
91+
scanner += sizeof(bc_simd_128_t);
9592
break;
9693
}
9794
}

0 commit comments

Comments
 (0)