php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
swar.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Alexander Borisov
3 *
4 * Author: Niels Dossche <nielsdos@php.net>
5 */
6
7#ifndef LEXBOR_SWAR_H
8#define LEXBOR_SWAR_H
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14
15#include "lexbor/core/base.h"
16
17
18/*
19 * Based on techniques from https://graphics.stanford.edu/~seander/bithacks.html
20 */
21#define LEXBOR_SWAR_ONES (~((size_t) 0) / 0xFF)
22#define LEXBOR_SWAR_REPEAT(x) (LEXBOR_SWAR_ONES * (x))
23#define LEXBOR_SWAR_HAS_ZERO(v) (((v) - LEXBOR_SWAR_ONES) & ~(v) & LEXBOR_SWAR_REPEAT(0x80))
24#define LEXBOR_SWAR_IS_LITTLE_ENDIAN (*(unsigned char *) &(uint16_t){1})
25
26
27/*
28 * When handling hot loops that search for a set of characters,
29 * this function can be used to quickly move the data pointer much
30 * closer to the first occurrence of such a character.
31 */
35{
36 size_t bytes, matches, t1, t2, t3, t4;
37
39 while (data + sizeof(size_t) <= end) {
40 memcpy(&bytes, data, sizeof(size_t));
41
42 t1 = bytes ^ LEXBOR_SWAR_REPEAT(c1);
43 t2 = bytes ^ LEXBOR_SWAR_REPEAT(c2);
44 t3 = bytes ^ LEXBOR_SWAR_REPEAT(c3);
45 t4 = bytes ^ LEXBOR_SWAR_REPEAT(c4);
48
49 if (matches) {
50 data += ((((matches - 1) & LEXBOR_SWAR_ONES) * LEXBOR_SWAR_ONES)
51 >> (sizeof(size_t) * 8 - 8)) - 1;
52 break;
53 } else {
54 data += sizeof(size_t);
55 }
56 }
57 }
58
59 return data;
60}
61
65{
66 size_t bytes, matches, t1, t2, t3;
67
69 while (data + sizeof(size_t) <= end) {
70 memcpy(&bytes, data, sizeof(size_t));
71
72 t1 = bytes ^ LEXBOR_SWAR_REPEAT(c1);
73 t2 = bytes ^ LEXBOR_SWAR_REPEAT(c2);
74 t3 = bytes ^ LEXBOR_SWAR_REPEAT(c3);
77
78 if (matches) {
79 data += ((((matches - 1) & LEXBOR_SWAR_ONES) * LEXBOR_SWAR_ONES)
80 >> (sizeof(size_t) * 8 - 8)) - 1;
81 break;
82 } else {
83 data += sizeof(size_t);
84 }
85 }
86 }
87
88 return data;
89}
90
91
92#ifdef __cplusplus
93} /* extern "C" */
94#endif
95
96#endif /* LEXBOR_SWAR_H */
97
memcpy(ptr1, ptr2, size)
unsigned const char * end
Definition php_ffi.h:51
#define t4
#define t1
#define t3
#define t2
zend_constant * data
lxb_inline const lxb_char_t * lexbor_swar_seek3(const lxb_char_t *data, const lxb_char_t *end, lxb_char_t c1, lxb_char_t c2, lxb_char_t c3)
Definition swar.h:63
lxb_inline const lxb_char_t * lexbor_swar_seek4(const lxb_char_t *data, const lxb_char_t *end, lxb_char_t c1, lxb_char_t c2, lxb_char_t c3, lxb_char_t c4)
Definition swar.h:33
#define LEXBOR_SWAR_HAS_ZERO(v)
Definition swar.h:23
#define LEXBOR_SWAR_IS_LITTLE_ENDIAN
Definition swar.h:24
#define LEXBOR_SWAR_ONES
Definition swar.h:21
#define LEXBOR_SWAR_REPEAT(x)
Definition swar.h:22
#define lxb_inline
Definition types.h:21
unsigned char lxb_char_t
Definition types.h:27