-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathqb_types.h
371 lines (316 loc) · 10.3 KB
/
qb_types.h
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Chung Leong <cleong@cal.berkeley.edu> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef QB_TYPES_H_
#define QB_TYPES_H_
typedef struct qb_block_allocator qb_block_allocator;
typedef struct qb_intrinsic_function qb_intrinsic_function;
typedef struct qb_data_pool qb_data_pool;
typedef struct qb_pointer_SCA qb_pointer_SCA;
typedef struct qb_pointer_ELE qb_pointer_ELE;
typedef struct qb_pointer_ARR qb_pointer_ARR;
typedef struct qb_pointer_adjustment qb_pointer_adjustment;
typedef struct qb_thread_parameters qb_thread_parameters;
typedef enum qb_primitive_type qb_primitive_type;
#define MAKE_STRING(...) #__VA_ARGS__
#define STRING(x) MAKE_STRING(x)
#define MAX_DIMENSION 8
#define MAX_THREAD_COUNT 288
#define CTYPE_I08 int8_t
#define CTYPE_I16 int16_t
#define CTYPE_I32 int32_t
#define CTYPE_I64 int64_t
#define CTYPE_F32 float32_t
#define CTYPE_F64 float64_t
#define CTYPE_S08 int8_t
#define CTYPE_S16 int16_t
#define CTYPE_S32 int32_t
#define CTYPE_S64 int64_t
#define CTYPE_U08 uint8_t
#define CTYPE_U16 uint16_t
#define CTYPE_U32 uint32_t
#define CTYPE_U64 uint64_t
#define CTYPE(type) CTYPE_##type
#define STORAGE_TYPE_MATCH(type1, type2) ((type1 == type2) || ((type1 & ~QB_TYPE_UNSIGNED) == (type2 & ~QB_TYPE_UNSIGNED) && (type1 < QB_TYPE_F32)))
#define BYTE_COUNT(element_count, type) ((uint32_t) ((element_count) << type_size_shifts[type]))
#define ELEMENT_COUNT(byte_count, type) ((byte_count) >> type_size_shifts[type])
#define SHIFT_POINTER(p, shift) p = (void *) (((char *) (p)) + shift)
#define INVALID_INDEX ((uint32_t) -1)
enum qb_primitive_type {
QB_TYPE_I08 = 0,
QB_TYPE_I16 = 2,
QB_TYPE_I32 = 4,
QB_TYPE_I64 = 6,
QB_TYPE_S08 = 0,
QB_TYPE_U08 = 1,
QB_TYPE_S16 = 2,
QB_TYPE_U16 = 3,
QB_TYPE_S32 = 4,
QB_TYPE_U32 = 5,
QB_TYPE_S64 = 6,
QB_TYPE_U64 = 7,
QB_TYPE_F32 = 8,
QB_TYPE_F64 = 9,
};
#define QB_TYPE_COUNT 10
#define QB_TYPE_VOID ((qb_primitive_type) 100)
#define QB_TYPE_UNKNOWN ((qb_primitive_type) 101)
#define QB_TYPE_ANY ((qb_primitive_type) 102)
#define QB_TYPE_UNSIGNED 0x0001
struct qb_intrinsic_function {
ulong hash_value;
const char *name;
uint32_t argument_count_min;
uint32_t argument_count_max;
void *extra;
};
struct qb_pointer_adjustment {
uint32_t index;
uint32_t count;
};
struct qb_thread_parameters {
void *pointer1;
void *pointer2;
};
#ifdef _MSC_VER
#define SWAP_BE_I16(v) _byteswap_ushort(v)
#define SWAP_BE_I32(v) _byteswap_ulong(v)
#define SWAP_BE_I64(v) _byteswap_uint64(v)
#define SWAP_LE_I16(v) v
#define SWAP_LE_I32(v) v
#define SWAP_LE_I64(v) v
#endif
#ifdef __GNUC__
#ifdef QB_LITTLE_ENDIAN
#define SWAP_BE_I16(v) __builtin_bswap16(v)
#define SWAP_BE_I32(v) __builtin_bswap32(v)
#define SWAP_BE_I64(v) __builtin_bswap64(v)
#define SWAP_LE_I16(v) v
#define SWAP_LE_I32(v) v
#define SWAP_LE_I64(v) v
#else
#define SWAP_BE_I16(v) v
#define SWAP_BE_I32(v) v
#define SWAP_BE_I64(v) v
#define SWAP_LE_I16(v) __builtin_bswap16(v)
#define SWAP_LE_I32(v) __builtin_bswap32(v)
#define SWAP_LE_I64(v) __builtin_bswap64(v)
#endif
#endif
#define ALIGN_TO(v, count) (((v) + (count) - 1) & ~((count) - 1))
#define ZERO_IF_FALSE(v, cond) (v & ~(-(cond)))
extern uint32_t type_sizes[];
extern uint32_t type_size_shifts[];
extern const char *type_names[];
extern const char *type_cnames[];
extern int64_t integer_lower_bounds[];
extern uint64_t integer_upper_bounds[];
int64_t qb_zval_to_long(zval *zvalue);
double qb_zval_to_double(zval *zvalue);
int32_t qb_zval_array_to_int64(zval *zvalue, int64_t *p_integer);
zval * qb_cstring_to_zval(const char *s TSRMLS_DC);
zval * qb_string_to_zval(const char *s, uint32_t len TSRMLS_DC);
zval * qb_cstring_to_zend_literal(const char *s TSRMLS_DC);
zval * qb_string_to_zend_literal(const char *s, uint32_t len TSRMLS_DC);
uint32_t qb_element_to_string(char *buffer, uint32_t buffer_len, int8_t *bytes, uint32_t type);
#define __qb_clamp_float32(f, max) qb_clamp_float32_0_##max(f)
#define qb_clamp_float32(f, max) __qb_clamp_float32(f, max)
#define __qb_clamp_float64(f, max) qb_clamp_float64_0_##max(f)
#define qb_clamp_float64(f, max) __qb_clamp_float64(f, max)
#if defined(__i386__)
#ifdef __SSE2__
#define FAST_FLOAT_TO_INT
#endif
#else
#define FAST_FLOAT_TO_INT
#endif
static zend_always_inline int32_t qb_clamp_float32_0_255(float32_t f) {
#ifdef FAST_FLOAT_TO_INT
int32_t n = (int32_t) (f * 255);
if(UNEXPECTED((uint32_t) n > 255)) {
if(n < 0) {
n = 0;
} else {
n = 255;
}
}
return n;
#else
int32_t n, sig, mask1, mask2;
f++;
n = ((int32_t *) &f)[0];
sig = n >> 15;
mask1 = -(n >= 0x40000000);
mask2 = -(n >= 0x3F800000) & 0xFF;
return (sig | mask1) & mask2;
#endif
}
static zend_always_inline int32_t qb_clamp_float64_0_255(float64_t f) {
#ifdef FAST_FLOAT_TO_INT
int32_t n = (int32_t) (f * 255);
if(UNEXPECTED((uint32_t) n > 255)) {
if(n < 0) {
n = 0;
} else {
n = 255;
}
}
return n;
#else
int32_t n, sig, mask1, mask2;
f++;
n = ((int32_t *) &f)[1];
sig = n >> 12;
mask1 = -(n >= 0x40000000);
mask2 = -(n >= 0x3FF00000) & 0xFF;
return (sig | mask1) & mask2;
#endif
}
static zend_always_inline int32_t qb_clamp_float32_0_127(float32_t f) {
#ifdef FAST_FLOAT_TO_INT
int32_t n = (int32_t) (f * 127);
if(UNEXPECTED((uint32_t) n > 127)) {
if(n < 0) {
n = 0;
} else {
n = 127;
}
}
return n;
#else
int32_t n, sig, mask1, mask2;
f++;
n = ((int32_t *) &f)[0];
sig = n >> 16;
mask1 = -(n >= 0x40000000);
mask2 = -(n >= 0x3F800000) & 0x7F;
return (sig | mask1) & mask2;
#endif
}
static zend_always_inline int32_t qb_clamp_float64_0_127(float64_t f) {
#ifdef FAST_FLOAT_TO_INT
int32_t n = (int32_t) (f * 127);
if(UNEXPECTED((uint32_t) n > 127)) {
if(n < 0) {
n = 0;
} else {
n = 127;
}
}
return n;
#else
int32_t n, sig, mask1, mask2;
f++;
n = ((int32_t *) &f)[1];
sig = n >> 13;
mask1 = -(n >= 0x40000000);
mask2 = -(n >= 0x3FF00000) & 0x7F;
return (sig | mask1) & mask2;
#endif
}
int32_t qb_uncompress_table(const char *data, void ***p_table, uint32_t *p_item_count, int32_t persistent);
struct qb_block_allocator {
uint32_t count;
uint32_t capacity;
uint32_t item_size;
qb_block_allocator *previous_block;
char *top;
char data[4];
};
struct qb_data_pool {
void ***arrays;
uint32_t array_count;
qb_block_allocator *op_allocator;
qb_block_allocator *address_allocator;
qb_block_allocator *expression_allocator;
qb_block_allocator *pointer_allocator;
qb_block_allocator *operand_allocator;
qb_block_allocator *index_alias_scheme_allocator;
qb_block_allocator *string_allocator;
qb_block_allocator *uint32_allocator;
qb_block_allocator *type_declaration_allocator;
qb_block_allocator *variable_allocator;
qb_block_allocator *function_declaration_allocator;
qb_block_allocator *class_declaration_allocator;
qb_block_allocator *array_initializer_allocator;
qb_block_allocator *result_destination_allocator;
char * const *op_names;
uint32_t op_name_count;
char * const *op_actions;
uint32_t op_action_count;
int32_t * const *op_function_usages;
uint32_t op_function_usage_count;
char * const *function_prototypes;
uint32_t function_prototype_count;
char * const *zend_op_names;
uint32_t zend_op_name_count;
char * const *pbj_op_names;
uint32_t pbj_op_name_count;
};
void qb_create_block_allocator(qb_block_allocator **p_allocator, uint32_t item_size, uint32_t capacity);
void * qb_allocate_items(qb_block_allocator **p_allocator, uint32_t count);
void * qb_reallocate_items(qb_block_allocator **p_allocator, void *current, uint32_t current_count, uint32_t new_count);
void qb_destroy_block_allocator(qb_block_allocator **p_allocator);
void qb_create_array(void **p_array, uint32_t *p_count, uint32_t item_size, uint32_t increment);
void * qb_enlarge_array(void **p_array, uint32_t count);
void qb_destroy_array(void **p_array);
float qb_fast_rsqrtf(float a);
double qb_fast_rsqrt(double a);
float qb_fast_sqrtf(float a);
double qb_fast_sqrt(double a);
uint64_t qb_calculate_crc64(const uint8_t *buf, size_t size, uint64_t crc);
double qb_get_high_res_timestamp(void);
void qb_initialize_data_pool(qb_data_pool *pool);
void qb_free_data_pool(qb_data_pool *pool);
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
#define UTF8_ACCEPT 0
#define UTF8_REJECT 12
extern const uint8_t utf8d[];
static uint32_t inline
decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
uint32_t type = utf8d[byte];
*codep = (*state != UTF8_ACCEPT) ?
(byte & 0x3fu) | (*codep << 6) :
(0xff >> type) & (byte);
*state = utf8d[256 + *state + type];
return *state;
}
static uint32_t inline
encode(uint32_t c, uint8_t *bytes) {
if (c < 0x80) {
bytes[0] = c;
return 1;
} else if(c < 0x800) {
bytes[0] = (-1 << 6) | (c >> 6);
bytes[1] = (((c >> 0) & 0x3f) | 0x80);
return 2;
} else if(c < 0x10000) {
bytes[0] = (-1 << 5) | (c >> 12);
bytes[1] = (((c >> 6) & 0x3f) | 0x80);
bytes[2] = (((c >> 0) & 0x3f) | 0x80);
return 3;
} else {
bytes[0] = (-1 << 4) | (c >> 18);
bytes[1] = (((c >> 12) & 0x3f) | 0x80);
bytes[2] = (((c >> 6) & 0x3f) | 0x80);
bytes[3] = (((c >> 0) & 0x3f) | 0x80);
return 4;
}
}
#endif