Skip to content

Commit 1ce79eb

Browse files
ext/bcmath: In the arm processor environment, NEON is used to use SIMD. (#18130)
1 parent b19a0a5 commit 1ce79eb

File tree

3 files changed

+88
-33
lines changed

3 files changed

+88
-33
lines changed

ext/bcmath/libbcmath/src/convert.c

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

2422
char *bc_copy_and_toggle_bcd(char *restrict dest, const char *source, const char *source_end)
2523
{
2624
const size_t bulk_shift = SWAR_REPEAT('0');
2725

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);
26+
#ifdef HAVE_BC_SIMD_128
27+
/* SIMD SSE2 or NEON bulk shift + copy */
28+
bc_simd_128_t shift_vector = bc_simd_set_8x16('0');
29+
while (source + sizeof(bc_simd_128_t) <= source_end) {
30+
bc_simd_128_t bytes = bc_simd_load_8x16((const bc_simd_128_t *) source);
31+
bytes = bc_simd_xor_8x16(bytes, shift_vector);
32+
bc_simd_store_8x16((bc_simd_128_t *) dest, bytes);
3533

36-
source += sizeof(__m128i);
37-
dest += sizeof(__m128i);
34+
source += sizeof(bc_simd_128_t);
35+
dest += sizeof(bc_simd_128_t);
3836
}
3937
#endif
4038

ext/bcmath/libbcmath/src/simd.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Saki Takamachi <saki@php.net> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
18+
#ifndef _BCMATH_SIMD_H_
19+
#define _BCMATH_SIMD_H_
20+
21+
#ifdef __SSE2__
22+
# include <emmintrin.h>
23+
typedef __m128i bc_simd_128_t;
24+
# define HAVE_BC_SIMD_128
25+
# define bc_simd_set_8x16(x) _mm_set1_epi8(x)
26+
# define bc_simd_load_8x16(ptr) _mm_loadu_si128((const __m128i *) (ptr))
27+
# define bc_simd_xor_8x16(a, b) _mm_xor_si128(a, b)
28+
# define bc_simd_store_8x16(ptr, val) _mm_storeu_si128((__m128i *) (ptr), val)
29+
# define bc_simd_add_8x16(a, b) _mm_add_epi8(a, b)
30+
# define bc_simd_cmpeq_8x16(a, b) _mm_cmpeq_epi8(a, b)
31+
# define bc_simd_cmplt_8x16(a, b) _mm_cmplt_epi8(a, b)
32+
# define bc_simd_movemask_8x16(a) _mm_movemask_epi8(a)
33+
34+
#elif defined(__aarch64__) || defined(_M_ARM64)
35+
# include <arm_neon.h>
36+
typedef int8x16_t bc_simd_128_t;
37+
# define HAVE_BC_SIMD_128
38+
# define bc_simd_set_8x16(x) vdupq_n_s8(x)
39+
# define bc_simd_load_8x16(ptr) vld1q_s8((const int8_t *) (ptr))
40+
# define bc_simd_xor_8x16(a, b) veorq_s8(a, b)
41+
# define bc_simd_store_8x16(ptr, val) vst1q_s8((int8_t *) (ptr), val)
42+
# define bc_simd_add_8x16(a, b) vaddq_s8(a, b)
43+
# define bc_simd_cmpeq_8x16(a, b) (vreinterpretq_s8_u8(vceqq_s8(a, b)))
44+
# define bc_simd_cmplt_8x16(a, b) (vreinterpretq_s8_u8(vcltq_s8(a, b)))
45+
static inline int bc_simd_movemask_8x16(int8x16_t vec)
46+
{
47+
/**
48+
* based on code from
49+
* https://community.arm.com/arm-community-blogs/b/servers-and-cloud-computing-blog/posts/porting-x86-vector-bitmask-optimizations-to-arm-neon
50+
*/
51+
uint16x8_t high_bits = vreinterpretq_u16_u8(vshrq_n_u8(vreinterpretq_u8_s8(vec), 7));
52+
uint32x4_t paired16 = vreinterpretq_u32_u16(vsraq_n_u16(high_bits, high_bits, 7));
53+
uint64x2_t paired32 = vreinterpretq_u64_u32(vsraq_n_u32(paired16, paired16, 14));
54+
uint8x16_t paired64 = vreinterpretq_u8_u64(vsraq_n_u64(paired32, paired32, 28));
55+
return vgetq_lane_u8(paired64, 0) | ((int) vgetq_lane_u8(paired64, 8) << 8);
56+
}
57+
#endif
58+
59+
#endif

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,28 @@
3232
#include "bcmath.h"
3333
#include "convert.h"
3434
#include "private.h"
35+
#include "simd.h"
3536
#include <stdbool.h>
3637
#include <stddef.h>
37-
#ifdef __SSE2__
38-
# include <emmintrin.h>
39-
#endif
4038

4139
/* Convert strings to bc numbers. Base 10 only.*/
42-
static const char *bc_count_digits(const char *str, const char *end)
40+
static inline const char *bc_count_digits(const char *str, const char *end)
4341
{
4442
/* Process in bulk */
45-
#ifdef __SSE2__
46-
const __m128i offset = _mm_set1_epi8((signed char) (SCHAR_MIN - '0'));
43+
#ifdef HAVE_BC_SIMD_128
44+
const bc_simd_128_t offset = bc_simd_set_8x16((signed char) (SCHAR_MIN - '0'));
4745
/* we use the less than comparator, so add 1 */
48-
const __m128i threshold = _mm_set1_epi8(SCHAR_MIN + ('9' + 1 - '0'));
46+
const bc_simd_128_t threshold = bc_simd_set_8x16(SCHAR_MIN + ('9' + 1 - '0'));
4947

50-
while (str + sizeof(__m128i) <= end) {
51-
__m128i bytes = _mm_loadu_si128((const __m128i *) str);
48+
while (str + sizeof(bc_simd_128_t) <= end) {
49+
bc_simd_128_t bytes = bc_simd_load_8x16((const bc_simd_128_t *) str);
5250
/* Wrapping-add the offset to the bytes, such that all bytes below '0' are positive and others are negative.
5351
* More specifically, '0' will be -128 and '9' will be -119. */
54-
bytes = _mm_add_epi8(bytes, offset);
52+
bytes = bc_simd_add_8x16(bytes, offset);
5553
/* Now mark all bytes that are <= '9', i.e. <= -119, i.e. < -118, i.e. the threshold. */
56-
bytes = _mm_cmplt_epi8(bytes, threshold);
54+
bytes = bc_simd_cmplt_8x16(bytes, threshold);
5755

58-
int mask = _mm_movemask_epi8(bytes);
56+
int mask = bc_simd_movemask_8x16(bytes);
5957
if (mask != 0xffff) {
6058
/* At least one of the bytes is not within range. Move to the first offending byte. */
6159
#ifdef PHP_HAVE_BUILTIN_CTZL
@@ -65,7 +63,7 @@ static const char *bc_count_digits(const char *str, const char *end)
6563
#endif
6664
}
6765

68-
str += sizeof(__m128i);
66+
str += sizeof(bc_simd_128_t);
6967
}
7068
#endif
7169

@@ -79,19 +77,19 @@ static const char *bc_count_digits(const char *str, const char *end)
7977
static inline const char *bc_skip_zero_reverse(const char *scanner, const char *stop)
8078
{
8179
/* 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);
80+
#ifdef HAVE_BC_SIMD_128
81+
const bc_simd_128_t c_zero_repeat = bc_simd_set_8x16('0');
82+
while (scanner - sizeof(bc_simd_128_t) >= stop) {
83+
scanner -= sizeof(bc_simd_128_t);
84+
bc_simd_128_t bytes = bc_simd_load_8x16((const bc_simd_128_t *) scanner);
8785
/* Checks if all numeric strings are equal to '0'. */
88-
bytes = _mm_cmpeq_epi8(bytes, c_zero_repeat);
86+
bytes = bc_simd_cmpeq_8x16(bytes, c_zero_repeat);
8987

90-
int mask = _mm_movemask_epi8(bytes);
88+
int mask = bc_simd_movemask_8x16(bytes);
9189
/* The probability of having 16 trailing 0s in a row is very low, so we use EXPECTED. */
9290
if (EXPECTED(mask != 0xffff)) {
9391
/* Move the pointer back and check each character in loop. */
94-
scanner += sizeof(__m128i);
92+
scanner += sizeof(bc_simd_128_t);
9593
break;
9694
}
9795
}

0 commit comments

Comments
 (0)