summaryrefslogtreecommitdiffstats
path: root/src/parser/parser_base.cpp
blob: e49af71c1418f213f8bb7ee9a3b37908ff44ed73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "orcus/parser_base.hpp"
#include "orcus/parser_global.hpp"
#include "cpu_features.hpp"

#include <sstream>
#include <cstring>
#include <limits>
#include <cassert>
#include <algorithm>

#ifdef __ORCUS_CPU_FEATURES
#include <immintrin.h>
#endif

namespace orcus {

parser_base::parser_base(const char* p, size_t n) :
    mp_begin(p), mp_char(p), mp_end(p+n),
    m_func_parse_numeric(parse_numeric)
{
}

void parser_base::prev(size_t dec)
{
    mp_char -= dec;
}

char parser_base::peek_char(std::size_t offset) const
{
    return *(mp_char + offset);
}

std::string_view parser_base::peek_chars(std::size_t length) const
{
    return {mp_char, length};
}

void parser_base::skip_bom()
{
    // Skip one or more UTF-8 BOM's.
    constexpr std::string_view BOM = "\xEF\xBB\xBF";

    while (true)
    {
        if (available_size() < 3)
            return;

        if (peek_chars(3) != BOM)
            return;

        next(3);
    }
}

void parser_base::skip(std::string_view chars_to_skip)
{
#if defined(__ORCUS_CPU_FEATURES) && defined(__SSE4_2__)
    __m128i match = _mm_loadu_si128((const __m128i*)chars_to_skip.data());
    const int mode = _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_EQUAL_ANY | _SIDD_UBYTE_OPS | _SIDD_NEGATIVE_POLARITY;

    int n_total = available_size();

    while (n_total)
    {
        __m128i char_block = _mm_loadu_si128((const __m128i*)mp_char);

        // Find position of the first character that is NOT any of the
        // characters to skip.
        int n = std::min<int>(16, n_total);
        int r = _mm_cmpestri(match, chars_to_skip.size(), char_block, n, mode);

        if (!r)
            // No characters to skip. Bail out.
            break;

        mp_char += r; // Move the current char position.

        if (r < 16)
            // No need to move to the next segment. Stop here.
            break;

        // Skip 16 chars to the next segment.
        n_total -= 16;
    }
#else
    for (; has_char(); next())
    {
        if (!is_in(*mp_char, chars_to_skip))
            break;
    }
#endif
}

void parser_base::skip_space_and_control()
{
#if defined(__ORCUS_CPU_FEATURES) && defined(__AVX2__)
    size_t n_total = available_size();
    const __m256i ws = _mm256_set1_epi8(' '); // whitespaces
    const __m256i sb = _mm256_set1_epi8(0x80); // signed bit on.

    while (n_total)
    {
        // The 'results' stores (for each 8-bit int) 0x00 if the char is less
        // than or equal to whitespace, or the char is "negative" i.e. the
        // signed bit is on IOW greater than 127.
        __m256i char_block = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(mp_char));
        __m256i results = _mm256_cmpgt_epi8(char_block, ws); // NB: this is a signed comparison.
        results = _mm256_or_si256(results, _mm256_and_si256(char_block, sb));
        int r = _mm256_movemask_epi8(results);
        r = _tzcnt_u32(r);
        r = std::min<size_t>(r, n_total);

        if (!r)
            // No characters to skip. Bail out.
            break;

        mp_char += r; // Move the current char position.

        if (r < 32)
            // No need to move to the next segment. Stop here.
            break;

        n_total -= 32;
    }

#elif defined(__ORCUS_CPU_FEATURES) && defined(__SSE4_2__)
    __m128i match = _mm_loadu_si128((const __m128i*)"\0 ");
    const int mode = _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS | _SIDD_NEGATIVE_POLARITY;

    size_t n_total = available_size();

    while (n_total)
    {
        __m128i char_block = _mm_loadu_si128((const __m128i*)mp_char);

        // Find position of the first character that is NOT any of the
        // characters to skip.
        int n = std::min<size_t>(16u, n_total);
        int r = _mm_cmpestri(match, 2, char_block, n, mode);

        if (!r)
            // No characters to skip. Bail out.
            break;

        mp_char += r; // Move the current char position.

        if (r < 16)
            // No need to move to the next segment. Stop here.
            break;

        // Skip 16 chars to the next segment.
        n_total -= 16;
    }
#else
    for (; mp_char != mp_end && ((unsigned char)*mp_char) <= (unsigned char)' '; ++mp_char)
        ;
#endif
}

bool parser_base::parse_expected(std::string_view expected)
{
    if (expected.size() > available_size())
        return false;

#if defined(__ORCUS_CPU_FEATURES) && defined(__SSE4_2__)
    __m128i v_expected = _mm_loadu_si128((const __m128i*)expected.data());
    __m128i v_char_block = _mm_loadu_si128((const __m128i*)mp_char);

    const int mode = _SIDD_CMP_EQUAL_ORDERED | _SIDD_UBYTE_OPS | _SIDD_BIT_MASK;
    __m128i res = _mm_cmpestrm(v_expected, expected.size(), v_char_block, expected.size(), mode);
    int mask = _mm_cvtsi128_si32(res);

    if (mask)
        mp_char += expected.size();

    return mask;
#else
    const char* p = expected.data();
    for (size_t i = 0; i < expected.size(); ++i, ++p, next())
    {
        if (cur_char() != *p)
            return false;
    }

    return true;
#endif
}

double parser_base::parse_double()
{
    size_t max_length = available_size();
    const char* p = mp_char;
    double val;
    p = m_func_parse_numeric(p, p + max_length, val);
    if (p == mp_char)
        return std::numeric_limits<double>::quiet_NaN();

    mp_char = p;
    return val;
}

size_t parser_base::remaining_size() const
{
    size_t n = available_size();
    return n ? (n - 1) : 0;
}

std::ptrdiff_t parser_base::offset() const
{
    return std::distance(mp_begin, mp_char);
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */