Skip to content

Commit 364a698

Browse files
committed
use SIMD
1 parent 90c5b2a commit 364a698

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

ext/bcmath/libbcmath/src/str2num.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ static const char *bc_count_digits(const char *str, const char *end)
7676
return str;
7777
}
7878

79+
static inline const char *bc_skip_zero_reverse(const char *str, const char *end)
80+
{
81+
82+
#ifdef __SSE2__
83+
const __m128i c_zero_repeat = _mm_set1_epi8((signed char) '0');
84+
while (str - sizeof(__m128i) >= end) {
85+
str -= sizeof(__m128i);
86+
__m128i bytes = _mm_loadu_si128((const __m128i *) str);
87+
bytes = _mm_cmpeq_epi8(bytes, c_zero_repeat);
88+
89+
int mask = _mm_movemask_epi8(bytes);
90+
if (EXPECTED(mask != 0xffff)) {
91+
str += sizeof(__m128i);
92+
break;
93+
}
94+
}
95+
#endif
96+
97+
/* Exclude trailing zeros. */
98+
while (str - 1 >= end && str[-1] == '0') {
99+
str--;
100+
}
101+
102+
return str;
103+
}
104+
79105
/* Assumes `num` points to NULL, i.e. does yet not hold a number. */
80106
bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, bool auto_scale)
81107
{
@@ -124,9 +150,7 @@ bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, boo
124150
}
125151

126152
/* Exclude trailing zeros. */
127-
while (fractional_end - 1 > decimal_point && fractional_end[-1] == '0') {
128-
fractional_end--;
129-
}
153+
fractional_end = bc_skip_zero_reverse(fractional_end, fractional_ptr);
130154

131155
/* Move the pointer to the beginning of the fraction. */
132156
fractional_ptr = decimal_point + 1;

0 commit comments

Comments
 (0)