-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorton.cpp
151 lines (125 loc) · 3.64 KB
/
morton.cpp
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <math.h> //libmorton bug
#include <morton.h>
extern "C" {
#include "php.h"
#include "ext/standard/info.h"
#include "php_morton.h"
}
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(morton)
{
#if defined(ZTS) && defined(COMPILE_DL_MORTON)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(morton)
{
php_info_print_table_start();
php_info_print_table_header(2, "morton support", "enabled");
php_info_print_table_row(2, "morton intrinsics",
#if defined(__AVX512BITALG__)
"AVX512"
#elif defined(__BMI2__) || defined(__AVX2__)
"BMI2"
#else
"not used"
#endif
);
php_info_print_table_end();
}
/* }}} */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_morton2d_encode, 0, 2, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, x, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, y, IS_LONG, 0)
ZEND_END_ARG_INFO()
PHP_FUNCTION(morton2d_encode) {
zend_long x, y;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 2, 2)
Z_PARAM_LONG(x)
Z_PARAM_LONG(y)
ZEND_PARSE_PARAMETERS_END();
RETURN_LONG(static_cast<zend_long>(libmorton::morton2D_64_encode(x, y)));
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_morton3d_encode, 0, 3, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, x, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, y, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, z, IS_LONG, 0)
ZEND_END_ARG_INFO()
PHP_FUNCTION(morton3d_encode) {
zend_long x, y, z;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 3, 3)
Z_PARAM_LONG(x)
Z_PARAM_LONG(y)
Z_PARAM_LONG(z)
ZEND_PARSE_PARAMETERS_END();
RETURN_LONG(static_cast<zend_long>(libmorton::morton3D_64_encode(x, y, z)));
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_morton2d_decode, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, morton, IS_LONG, 0)
ZEND_END_ARG_INFO()
PHP_FUNCTION(morton2d_decode) {
const size_t SHIFT = (sizeof(zend_long) * 8) - 32;
zend_long morton;
uint_fast32_t x, y;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 1)
Z_PARAM_LONG(morton)
ZEND_PARSE_PARAMETERS_END();
libmorton::morton2D_64_decode(morton, x, y);
array_init_size(return_value, 2);
add_next_index_long(return_value, static_cast<zend_long>(x) << SHIFT >> SHIFT);
add_next_index_long(return_value, static_cast<zend_long>(y) << SHIFT >> SHIFT);
}
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_morton3d_decode, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, morton, IS_LONG, 0)
ZEND_END_ARG_INFO()
PHP_FUNCTION(morton3d_decode) {
const size_t SHIFT = (sizeof(zend_long) * 8) - 21;
zend_long morton;
uint_fast32_t x, y, z;
ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 1)
Z_PARAM_LONG(morton)
ZEND_PARSE_PARAMETERS_END();
libmorton::morton3D_64_decode(morton, x, y, z);
array_init_size(return_value, 3);
add_next_index_long(return_value, static_cast<zend_long>(x) << SHIFT >> SHIFT);
add_next_index_long(return_value, static_cast<zend_long>(y) << SHIFT >> SHIFT);
add_next_index_long(return_value, static_cast<zend_long>(z) << SHIFT >> SHIFT);
}
static const zend_function_entry morton_functions[] = {
PHP_FE(morton2d_encode, arginfo_morton2d_encode)
PHP_FE(morton3d_encode, arginfo_morton3d_encode)
PHP_FE(morton2d_decode, arginfo_morton2d_decode)
PHP_FE(morton3d_decode, arginfo_morton3d_decode)
PHP_FE_END
};
/* {{{ morton_module_entry
*/
zend_module_entry morton_module_entry = {
STANDARD_MODULE_HEADER,
"morton",
morton_functions,
NULL, /* MINIT */
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* PHP_RSHUTDOWN */
PHP_MINFO(morton),
PHP_MORTON_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_MORTON
extern "C" {
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(morton)
}
#endif