Skip to content

Commit 55a20e9

Browse files
author
Eddie
committed
Fixes imprecisions about conditional compilation
1 parent 04ef330 commit 55a20e9

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

benchmark/atoi-corpus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "atoi.h"
22
#include "zoo/pp/platform.h"
3+
34
#include <vector>
45
#include <string>
56
#include <cstring>

benchmark/atoi.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
#include "zoo/swar/SWAR.h"
1+
#include "atoi.h"
2+
23
#include "zoo/swar/associative_iteration.h"
34

4-
#if ZOO_CONFIGURED_TO_USE_AVX
5+
#if ZOO_CONFIGURED_TO_USE_AVX()
56
#include <immintrin.h>
67
#endif
78

89
#include <stdint.h>
910
#include <string.h>
1011
#include <stdlib.h>
12+
#include <ctype.h>
1113

1214
// Copied from Daniel Lemire's GitHub at
1315
// https://lemire.me/blog/2018/10/03/quickly-parsing-eight-digits/
@@ -25,7 +27,7 @@ uint32_t parse_eight_digits_swar(const char *chars) {
2527

2628
// Note: eight digits can represent from 0 to (10^9) - 1, the logarithm base 2
2729
// of 10^9 is slightly less than 30, thus, only 30 bits are needed.
28-
auto lemire_as_zoo_swar(const char *chars) {
30+
uint32_t lemire_as_zoo_swar(const char *chars) {
2931
uint64_t bytes;
3032
memcpy(&bytes, chars, 8);
3133
auto allCharacterZero = zoo::meta::BitmaskMaker<uint64_t, '0', 8>::value;
@@ -54,8 +56,44 @@ auto lemire_as_zoo_swar(const char *chars) {
5456
return uint32_t(by10001base2to32.value() >> 32);
5557
}
5658

59+
std::size_t spaces_glibc(const char *ptr) {
60+
auto rv = 0;
61+
while(isspace(ptr[rv])) { ++rv; }
62+
return rv;
63+
}
64+
5765
namespace zoo {
5866

67+
//constexpr
68+
std::size_t leadingSpacesCount(swar::SWAR<8, uint64_t> bytes) noexcept {
69+
/*
70+
space (0x20, ' ')
71+
form feed (0x0c, '\f')
72+
line feed (0x0a, '\n')
73+
carriage return (0x0d, '\r')
74+
horizontal tab (0x09, '\t')
75+
vertical tab (0x0b, '\v')*
76+
constexpr std::array<char, 6> SpaceCharacters = {
77+
0b10'0000, //0x20 space
78+
0b00'1101, // 0xD \r
79+
0b00'1100, // 0xC \f
80+
0b00'1011, // 0xB \v
81+
0b00'1010, // 0xA \n
82+
0b00'1001 // 9 \t
83+
},
84+
ExpressedAsEscapeCodes = { ' ', '\r', '\f', '\v', '\n', '\t' };
85+
static_assert(SpaceCharacters == ExpressedAsEscapeCodes); */
86+
using S = swar::SWAR<8, uint64_t>;
87+
constexpr S Space{meta::BitmaskMaker<uint64_t, ' ', 8>::value};
88+
auto space = swar::equals(bytes, Space);
89+
auto otherWhiteSpace =
90+
swar::constantIsGreaterEqual<'\r'>(bytes) &
91+
~swar::constantIsGreaterEqual<'\t' - 1>(bytes);
92+
auto whiteSpace = space | otherWhiteSpace;
93+
auto notWhiteSpace = S{S::MostSignificantBit} ^ whiteSpace;
94+
return notWhiteSpace.lsbIndex();
95+
}
96+
5997
std::size_t c_strLength(const char *s) {
6098
using S = swar::SWAR<8, std::size_t>;
6199
constexpr auto
@@ -119,7 +157,7 @@ std::size_t c_strLength_manualComparison(const char *s) {
119157
}
120158
}
121159

122-
#if ZOO_CONFIGURED_TO_USE_AVX
160+
#if ZOO_CONFIGURED_TO_USE_AVX()
123161
size_t avx2_strlen(const char* str) {
124162
const __m256i zero = _mm256_setzero_si256(); // Vector of 32 zero bytes
125163
size_t offset = 0;

benchmark/atoi.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
#include "stdint.h"
1+
#include "zoo/swar/SWAR.h"
2+
#include "zoo/pp/platform.h"
3+
24
#include <cstdlib>
35

46
uint32_t parse_eight_digits_swar(const char *chars);
57
uint32_t lemire_as_zoo_swar(const char *chars);
8+
std::size_t spaces_glibc(const char *ptr);
69

710
namespace zoo {
811

12+
std::size_t leadingSpacesCount(swar::SWAR<8, uint64_t> bytes) noexcept;
913
std::size_t c_strLength(const char *s);
1014
std::size_t c_strLength_natural(const char *s);
1115
std::size_t c_strLength_manualComparison(const char *s);
1216

13-
#if ZOO_CONFIGURED_TO_USE_AVX
17+
#if ZOO_CONFIGURED_TO_USE_AVX()
1418
std::size_t avx2_strlen(const char* str);
1519
#endif
1620

0 commit comments

Comments
 (0)