Skip to content

Commit a627d51

Browse files
committed
refactor parse_unsigned
1 parent 66d9251 commit a627d51

File tree

1 file changed

+8
-44
lines changed

1 file changed

+8
-44
lines changed

include/boost/json/detail/sse2.hpp

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -315,53 +315,17 @@ inline int count_digits( char const* p ) noexcept
315315

316316
inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noexcept
317317
{
318-
while( n >= 4 )
318+
constexpr int N = 4;
319+
auto const e = p + n;
320+
for( ; (e - p) >= N; p += N )
319321
{
320-
// faster on on clang for x86,
321-
// slower on gcc
322-
#ifdef __clang__
323-
r = r * 10 + p[0] - '0';
324-
r = r * 10 + p[1] - '0';
325-
r = r * 10 + p[2] - '0';
326-
r = r * 10 + p[3] - '0';
327-
#else
328-
uint32_t v;
329-
std::memcpy( &v, p, 4 );
330-
331-
v -= 0x30303030;
332-
333-
unsigned w0 = v & 0xFF;
334-
unsigned w1 = (v >> 8) & 0xFF;
335-
unsigned w2 = (v >> 16) & 0xFF;
336-
unsigned w3 = (v >> 24);
337-
338-
#ifdef BOOST_JSON_BIG_ENDIAN
339-
r = (((r * 10 + w3) * 10 + w2) * 10 + w1) * 10 + w0;
340-
#else
341-
r = (((r * 10 + w0) * 10 + w1) * 10 + w2) * 10 + w3;
342-
#endif
343-
#endif
344-
p += 4;
345-
n -= 4;
322+
for( auto i = 0; i < N; ++i )
323+
r = r * 10 + p[i] - '0';
346324
}
347325

348-
switch( n )
349-
{
350-
case 0:
351-
break;
352-
case 1:
353-
r = r * 10 + p[0] - '0';
354-
break;
355-
case 2:
356-
r = r * 10 + p[0] - '0';
357-
r = r * 10 + p[1] - '0';
358-
break;
359-
case 3:
360-
r = r * 10 + p[0] - '0';
361-
r = r * 10 + p[1] - '0';
362-
r = r * 10 + p[2] - '0';
363-
break;
364-
}
326+
for( ; p != e; ++p )
327+
r = r * 10 + *p - '0';
328+
365329
return r;
366330
}
367331

0 commit comments

Comments
 (0)