Skip to content

Commit eceb18c

Browse files
committed
use zend_simd.h in bcmath
1 parent ee9eb32 commit eceb18c

File tree

3 files changed

+28
-87
lines changed

3 files changed

+28
-87
lines changed

ext/bcmath/libbcmath/src/convert.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
#include "bcmath.h"
1818
#include "convert.h"
1919
#include "private.h"
20-
#include "simd.h"
20+
#include "zend_simd.h"
2121

2222
char *bc_copy_and_toggle_bcd(char *restrict dest, const char *source, const char *source_end)
2323
{
2424
const size_t bulk_shift = SWAR_REPEAT('0');
2525

26-
#ifdef HAVE_BC_SIMD_128
26+
#ifdef ZEND_HAVE_VECTOR_128
2727
/* 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);
33-
34-
source += sizeof(bc_simd_128_t);
35-
dest += sizeof(bc_simd_128_t);
28+
__m128i shift_vector = _mm_set1_epi8('0');
29+
while (source + sizeof(__m128i) <= source_end) {
30+
__m128i bytes = _mm_loadu_si128((const __m128i *) source);
31+
bytes = _mm_xor_si128(bytes, shift_vector);
32+
_mm_storeu_si128((__m128i *) dest, bytes);
33+
34+
source += sizeof(__m128i);
35+
dest += sizeof(__m128i);
3636
}
3737
#endif
3838

ext/bcmath/libbcmath/src/simd.h

-59
This file was deleted.

ext/bcmath/libbcmath/src/str2num.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@
3232
#include "bcmath.h"
3333
#include "convert.h"
3434
#include "private.h"
35-
#include "simd.h"
35+
#include "zend_simd.h"
3636
#include <stdbool.h>
3737
#include <stddef.h>
3838

3939
/* Convert strings to bc numbers. Base 10 only.*/
4040
static inline const char *bc_count_digits(const char *str, const char *end)
4141
{
4242
/* Process in bulk */
43-
#ifdef HAVE_BC_SIMD_128
44-
const bc_simd_128_t offset = bc_simd_set_8x16((signed char) (SCHAR_MIN - '0'));
43+
#ifdef ZEND_HAVE_VECTOR_128
44+
const __m128i offset = _mm_set1_epi8((signed char) (SCHAR_MIN - '0'));
4545
/* we use the less than comparator, so add 1 */
46-
const bc_simd_128_t threshold = bc_simd_set_8x16(SCHAR_MIN + ('9' + 1 - '0'));
46+
const __m128i threshold = _mm_set1_epi8(SCHAR_MIN + ('9' + 1 - '0'));
4747

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);
48+
while (str + sizeof(__m128i) <= end) {
49+
__m128i bytes = _mm_loadu_si128((const __m128i *) str);
5050
/* Wrapping-add the offset to the bytes, such that all bytes below '0' are positive and others are negative.
5151
* More specifically, '0' will be -128 and '9' will be -119. */
52-
bytes = bc_simd_add_8x16(bytes, offset);
52+
bytes = _mm_add_epi8(bytes, offset);
5353
/* Now mark all bytes that are <= '9', i.e. <= -119, i.e. < -118, i.e. the threshold. */
54-
bytes = bc_simd_cmplt_8x16(bytes, threshold);
54+
bytes = _mm_cmplt_epi8(bytes, threshold);
5555

56-
int mask = bc_simd_movemask_8x16(bytes);
56+
int mask = _mm_movemask_epi8(bytes);
5757
if (mask != 0xffff) {
5858
/* At least one of the bytes is not within range. Move to the first offending byte. */
5959
#ifdef PHP_HAVE_BUILTIN_CTZL
@@ -63,7 +63,7 @@ static inline const char *bc_count_digits(const char *str, const char *end)
6363
#endif
6464
}
6565

66-
str += sizeof(bc_simd_128_t);
66+
str += sizeof(__m128i);
6767
}
6868
#endif
6969

@@ -77,19 +77,19 @@ static inline const char *bc_count_digits(const char *str, const char *end)
7777
static inline const char *bc_skip_zero_reverse(const char *scanner, const char *stop)
7878
{
7979
/* Check in bulk */
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);
80+
#ifdef ZEND_HAVE_VECTOR_128
81+
const __m128i c_zero_repeat = _mm_set1_epi8('0');
82+
while (scanner - sizeof(__m128i) >= stop) {
83+
scanner -= sizeof(__m128i);
84+
__m128i bytes = _mm_loadu_si128((const __m128i *) scanner);
8585
/* Checks if all numeric strings are equal to '0'. */
86-
bytes = bc_simd_cmpeq_8x16(bytes, c_zero_repeat);
86+
bytes = _mm_cmpeq_epi8(bytes, c_zero_repeat);
8787

88-
int mask = bc_simd_movemask_8x16(bytes);
88+
int mask = _mm_movemask_epi8(bytes);
8989
/* The probability of having 16 trailing 0s in a row is very low, so we use EXPECTED. */
9090
if (EXPECTED(mask != 0xffff)) {
9191
/* Move the pointer back and check each character in loop. */
92-
scanner += sizeof(bc_simd_128_t);
92+
scanner += sizeof(__m128i);
9393
break;
9494
}
9595
}

0 commit comments

Comments
 (0)