• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / ort / 16252192837

13 Jul 2025 06:29PM UTC coverage: 92.228% (-0.2%) from 92.44%
16252192837

push

github

krakjoe
cbrt schema

3 of 3 new or added lines in 2 files covered. (100.0%)

27 existing lines in 2 files now uncovered.

5150 of 5584 relevant lines covered (92.23%)

68924.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.79
/src/maths.c
1
/*
2
  +----------------------------------------------------------------------+
3
  | ort                                                                  |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2025                                       |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP license,      |
8
  | that is bundled with this package in the file LICENSE, and is        |
9
  | available through the world-wide-web at the following url:           |
10
  | http://www.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: krakjoe                                                      |
16
  +----------------------------------------------------------------------+
17
 */
18

19
#include "ort.h"
20
#include "status.h"
21

22
#include "maths.h"
23
#include "maths/api.h"
24
#include "maths/result.h"
25
#include "maths/promotion.h"
26

27
#include "maths/schema/acos.h"
28
#include "maths/schema/atan.h"
29
#include "maths/schema/add.h"
30
#include "maths/schema/asin.h"
31
#include "maths/schema/cbrt.h"
32
#include "maths/schema/cos.h"
33
#include "maths/schema/cosh.h"
34
#include "maths/schema/div.h"
35
#include "maths/schema/dot.h"
36
#include "maths/schema/exp.h"
37
#include "maths/schema/exp2.h"
38
#include "maths/schema/log.h"
39
#include "maths/schema/log2.h"
40
#include "maths/schema/log10.h"
41
#include "maths/schema/matmul.h"
42
#include "maths/schema/max.h"
43
#include "maths/schema/mean.h"
44
#include "maths/schema/min.h"
45
#include "maths/schema/mod.h"
46
#include "maths/schema/mul.h"
47
#include "maths/schema/neg.h"
48
#include "maths/schema/pow.h"
49
#include "maths/schema/recip.h"
50
#include "maths/schema/sign.h"
51
#include "maths/schema/sin.h"
52
#include "maths/schema/sinh.h"
53
#include "maths/schema/softmax.h"
54
#include "maths/schema/sqrt.h"
55
#include "maths/schema/sub.h"
56
#include "maths/schema/sum.h"
57
#include "maths/schema/tan.h"
58
#include "maths/schema/tanh.h"
59

60
typedef struct _php_ort_math_schema_t {
61
    const ort_math_type_promotion_schema_t* schema;
62
    zend_string*                            symbol;
63
    zend_object std;
64
} php_ort_math_schema_t;
65

66
zend_class_entry *php_ort_math_schema_ce;
67
zend_object_handlers php_ort_math_schema_handlers;
68

69
static zend_always_inline php_ort_math_schema_t* php_ort_math_schema_fetch(zend_object *o) {
70
    return (php_ort_math_schema_t*) (((char*) o) - XtOffsetOf(php_ort_math_schema_t, std));
71
}
72

73
static zend_object* php_ort_math_schema_create(zend_class_entry *ce) {
560✔
74
    php_ort_math_schema_t *ort = 
560✔
75
        zend_object_alloc(sizeof(php_ort_math_schema_t), ce);
560✔
76
        
77
    zend_object_std_init(&ort->std, ce);
560✔
78
    object_properties_init(&ort->std, ce);
560✔
79

80
    ort->std.handlers = &php_ort_math_schema_handlers;
560✔
81
    return &ort->std;
560✔
82
}
83

84
static HashTable* php_ort_math_schema_debug(zend_object *zo, int *temp) {
×
UNCOV
85
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(zo);
×
86
    HashTable *debug;
×
87

UNCOV
88
    ALLOC_HASHTABLE(debug);
×
89
    zend_hash_init(debug, 3, NULL, ZVAL_PTR_DTOR, 0);
×
90

UNCOV
91
    if (ort->symbol) {
×
92
        zval symbol;
×
93

94
        ZVAL_STR_COPY(&symbol, ort->symbol);
×
95

UNCOV
96
        zend_hash_str_update(debug,
×
97
            "symbol", sizeof("symbol") - 1,
98
            &symbol);
99
    }
100

UNCOV
101
__php_ort_tensor_debug_return:
×
102
    *temp = 1;
×
103

UNCOV
104
    return debug;
×
105
}
106

107
void php_ort_math_schema_free(zend_object *zo) {
560✔
108
    php_ort_math_schema_t* ort = php_ort_math_schema_fetch(zo);
560✔
109

110
    if (ort->symbol) {
560✔
111
        zend_string_release(ort->symbol);
560✔
112
    }
113

114
    zend_object_std_dtor(zo);
560✔
115
}
560✔
116

117
#define ORT_MATH_UNARY_FUNCTION_IMPL(fname)                    \
118
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                    \
119
        php_ort_math_##fname##_arginfo, 0, 1, ONNX\\Tensor, 0) \
120
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)          \
121
    ZEND_END_ARG_INFO()                                        \
122
    PHP_FUNCTION(fname)                                        \
123
    {                                                          \
124
        zval *tensor_zv;                                       \
125
        ZEND_PARSE_PARAMETERS_START(1, 1)                      \
126
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                 \
127
                php_ort_tensor_interface_ce)                   \
128
        ZEND_PARSE_PARAMETERS_END();                           \
129
        php_ort_tensor_t* tensor_ort =                         \
130
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));          \
131
        ort_tensor_t* result =                                 \
132
            ort_math_result_##fname(tensor_ort->object);       \
133
        object_init_ex(return_value,                           \
134
            php_ort_tensor_transient_ce);                      \
135
        php_ort_tensor_t* rv =                                 \
136
            php_ort_tensor_fetch(Z_OBJ_P(return_value));       \
137
        rv->object = result;                                   \
138
    }
139

140
#define ORT_MATH_BINARY_FUNCTION_IMPL(fname)                    \
141
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                     \
142
        php_ort_math_##fname##_arginfo, 0, 2, ONNX\\Tensor, 0)  \
143
        ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)         \
144
        ZEND_ARG_INFO(0, tensor_b_or_scalar)                    \
145
    ZEND_END_ARG_INFO()                                         \
146
    PHP_FUNCTION(fname)                                         \
147
    {                                                           \
148
        zval *tensor_a_zv, *tensor_b_or_scalar;                 \
149
        ZEND_PARSE_PARAMETERS_START(2, 2)                       \
150
            Z_PARAM_OBJECT_OF_CLASS(tensor_a_zv,                \
151
                php_ort_tensor_interface_ce)                    \
152
            Z_PARAM_ZVAL(tensor_b_or_scalar)                    \
153
        ZEND_PARSE_PARAMETERS_END();                            \
154
        php_ort_tensor_t* tensor_a_ort =                        \
155
            php_ort_tensor_fetch(Z_OBJ_P(tensor_a_zv));         \
156
        ort_tensor_t* result = NULL;                            \
157
        if (Z_TYPE_P(tensor_b_or_scalar) == IS_OBJECT &&        \
158
            instanceof_function(Z_OBJCE_P(tensor_b_or_scalar),  \
159
                php_ort_tensor_interface_ce)) {                 \
160
            php_ort_tensor_t* tensor_b_ort =                    \
161
                php_ort_tensor_fetch(Z_OBJ_P(tensor_b_or_scalar)); \
162
            result = ort_math_result_##fname(                   \
163
                tensor_a_ort->object, tensor_b_ort->object);    \
164
        } else if (Z_TYPE_P(tensor_b_or_scalar) == IS_LONG ||   \
165
                   Z_TYPE_P(tensor_b_or_scalar) == IS_DOUBLE || \
166
                   (Z_TYPE_P(tensor_b_or_scalar) == IS_TRUE ||  \
167
                    Z_TYPE_P(tensor_b_or_scalar) == IS_FALSE)) { \
168
            result = ort_math_result_##fname##_scalar(          \
169
                tensor_a_ort->object, tensor_b_or_scalar);      \
170
        } else {                                                \
171
            php_ort_status_throw(                               \
172
                php_ort_status_math_invalidtype_ce,             \
173
                #fname ": second argument must be a Tensor or numeric value"); \
174
            return;                                             \
175
        }                                                       \
176
        object_init_ex(return_value,                            \
177
            php_ort_tensor_transient_ce);                       \
178
        php_ort_tensor_t* rv =                                  \
179
            php_ort_tensor_fetch(Z_OBJ_P(return_value));        \
180
        rv->object = result;                                    \
181
    }
182

183

184
#define ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(fname)             \
185
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
186
            php_ort_math_reduce_tensor_##fname##_arginfo,          \
187
                0, 1, ONNX\\Tensor, 0)                             \
188
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
189
    ZEND_END_ARG_INFO()                                            \
190
    PHP_NAMED_FUNCTION(php_ort_math_reduce_tensor_##fname)         \
191
    {                                                              \
192
        zval *tensor_zv;                                           \
193
        ZEND_PARSE_PARAMETERS_START(1, 1)                          \
194
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
195
                php_ort_tensor_interface_ce)                       \
196
        ZEND_PARSE_PARAMETERS_END();                               \
197
        php_ort_tensor_t* tensor_ort =                             \
198
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
199
        ort_tensor_t* result =                                     \
200
            ort_math_result_reduce_tensor_##fname(                 \
201
                tensor_ort->object);                               \
202
        object_init_ex(return_value,                               \
203
            php_ort_tensor_transient_ce);                          \
204
        php_ort_tensor_t* rv =                                     \
205
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
206
        rv->object = result;                                       \
207
    }
208

209
// Macro for reduction functions (Tensor, axis, keepdims)
210
#define ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(fname)               \
211
    ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(                        \
212
            php_ort_math_reduce_axis_##fname##_arginfo, 0, 2, ONNX\\Tensor, 0) \
213
        ZEND_ARG_OBJ_INFO(0, tensor, ONNX\\Tensor, 0)              \
214
        ZEND_ARG_TYPE_INFO(0, axis, IS_LONG, 0)                    \
215
        ZEND_ARG_TYPE_INFO(0, keepdims, _IS_BOOL, 0)               \
216
    ZEND_END_ARG_INFO()                                            \
217
    PHP_NAMED_FUNCTION(php_ort_math_reduce_axis_##fname)           \
218
    {                                                              \
219
        zval *tensor_zv;                                           \
220
        zend_long axis = 0;                                        \
221
        zend_bool keepdims = 0;                                    \
222
        ZEND_PARSE_PARAMETERS_START(2, 3)                          \
223
            Z_PARAM_OBJECT_OF_CLASS(tensor_zv,                     \
224
                php_ort_tensor_interface_ce)                       \
225
            Z_PARAM_LONG(axis)                                     \
226
            Z_PARAM_OPTIONAL                                       \
227
            Z_PARAM_BOOL(keepdims)                                 \
228
        ZEND_PARSE_PARAMETERS_END();                               \
229
        php_ort_tensor_t* tensor_ort =                             \
230
            php_ort_tensor_fetch(Z_OBJ_P(tensor_zv));              \
231
        ort_tensor_t* result = ort_math_result_reduce_axis_##fname(\
232
            tensor_ort->object, axis, keepdims);                   \
233
        object_init_ex(return_value,                               \
234
            php_ort_tensor_transient_ce);                          \
235
        php_ort_tensor_t* rv =                                     \
236
            php_ort_tensor_fetch(Z_OBJ_P(return_value));           \
237
        rv->object = result;                                       \
238
    }
239
/* Mathematical functions in ONNX\Math namespace */
240

241
ORT_MATH_BINARY_FUNCTION_IMPL(add)
16,896✔
242
ORT_MATH_BINARY_FUNCTION_IMPL(multiply)
4,112✔
243
ORT_MATH_BINARY_FUNCTION_IMPL(subtract)
1,936✔
244
ORT_MATH_BINARY_FUNCTION_IMPL(divide)
3,456✔
245

246
ORT_MATH_BINARY_FUNCTION_IMPL(pow)
2,448✔
247
ORT_MATH_BINARY_FUNCTION_IMPL(mod)
2,496✔
248

249
ORT_MATH_UNARY_FUNCTION_IMPL(sin)
736✔
250
ORT_MATH_UNARY_FUNCTION_IMPL(cos)
736✔
251
ORT_MATH_UNARY_FUNCTION_IMPL(tan)
736✔
252
ORT_MATH_UNARY_FUNCTION_IMPL(asin)
704✔
253
ORT_MATH_UNARY_FUNCTION_IMPL(acos)
704✔
254
ORT_MATH_UNARY_FUNCTION_IMPL(atan)
704✔
255
ORT_MATH_UNARY_FUNCTION_IMPL(sinh)
704✔
256
ORT_MATH_UNARY_FUNCTION_IMPL(cosh)
704✔
257
ORT_MATH_UNARY_FUNCTION_IMPL(tanh)
704✔
258
ORT_MATH_UNARY_FUNCTION_IMPL(exp)
736✔
259
ORT_MATH_UNARY_FUNCTION_IMPL(exp2)
736✔
260
ORT_MATH_UNARY_FUNCTION_IMPL(log)
736✔
261
ORT_MATH_UNARY_FUNCTION_IMPL(log2)
736✔
262
ORT_MATH_UNARY_FUNCTION_IMPL(log10)
736✔
263
ORT_MATH_UNARY_FUNCTION_IMPL(abs)
320✔
264
ORT_MATH_UNARY_FUNCTION_IMPL(recip)
704✔
265
ORT_MATH_UNARY_FUNCTION_IMPL(neg)
608✔
266
ORT_MATH_UNARY_FUNCTION_IMPL(cbrt)
736✔
267
ORT_MATH_UNARY_FUNCTION_IMPL(ceil)
320✔
268
ORT_MATH_UNARY_FUNCTION_IMPL(floor)
320✔
269
ORT_MATH_UNARY_FUNCTION_IMPL(round)
320✔
270
ORT_MATH_UNARY_FUNCTION_IMPL(trunc)
320✔
271
ORT_MATH_UNARY_FUNCTION_IMPL(sign)
416✔
272
ORT_MATH_UNARY_FUNCTION_IMPL(sqrt)
1,504✔
273

274
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(min)
320✔
275
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(min)
2,176✔
276

277
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(max)
320✔
278
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(max)
2,176✔
279

280
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(mean)
320✔
281
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(mean)
2,176✔
282

283
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(sum)
224✔
284
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(sum)
2,304✔
285

286
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(softmax)
832✔
287

288
/* Dot reduction function */
289
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_dot_arginfo, 0, 1, ONNX\\Tensor, 0)
290
    ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)
291
    ZEND_ARG_OBJ_INFO(0, tensor_b, ONNX\\Tensor, 0)
292
ZEND_END_ARG_INFO()
293

294
PHP_FUNCTION(dot)
288✔
295
{
296
    zval *tensor_a, *tensor_b;
288✔
297

298
    ZEND_PARSE_PARAMETERS_START(2, 2)
288✔
299
        Z_PARAM_OBJECT_OF_CLASS(tensor_a, php_ort_tensor_interface_ce)
576✔
300
        Z_PARAM_OBJECT_OF_CLASS(tensor_b, php_ort_tensor_interface_ce)
576✔
301
    ZEND_PARSE_PARAMETERS_END();
288✔
302

303
    php_ort_tensor_t* tensor_a_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_a));
288✔
304
    php_ort_tensor_t* tensor_b_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_b));
288✔
305
    ort_tensor_t* result = ort_math_result_dot(
288✔
306
        tensor_a_ort->object, tensor_b_ort->object);
307

308
    /* Create PHP tensor object for result */
309
    object_init_ex(return_value,
288✔
310
        php_ort_tensor_transient_ce);
311
    php_ort_tensor_t* rv =
288✔
312
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
288✔
313
    rv->object = result;
288✔
314
}
315

316
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_matmul_arginfo, 0, 2, ONNX\\Tensor, 0)
317
    ZEND_ARG_OBJ_INFO(0, matrix_a, ONNX\\Tensor, 0)
318
    ZEND_ARG_OBJ_INFO(0, matrix_b, ONNX\\Tensor, 0)
319
ZEND_END_ARG_INFO()
320

321
PHP_FUNCTION(matmul)
672✔
322
{
323
    zval *matrix_a_zv, *matrix_b_zv;
672✔
324

325
    ZEND_PARSE_PARAMETERS_START(2, 2)
672✔
326
        Z_PARAM_OBJECT_OF_CLASS(matrix_a_zv, php_ort_tensor_interface_ce)
1,344✔
327
        Z_PARAM_OBJECT_OF_CLASS(matrix_b_zv, php_ort_tensor_interface_ce)
1,344✔
328
    ZEND_PARSE_PARAMETERS_END();
672✔
329

330
    php_ort_tensor_t* matrix_a_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_a_zv));
672✔
331
    php_ort_tensor_t* matrix_b_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_b_zv));
672✔
332
    
333
    ort_tensor_t* result = ort_math_result_matmul(matrix_a_ort->object, matrix_b_ort->object);
672✔
334

335
    object_init_ex(return_value,
672✔
336
        php_ort_tensor_transient_ce);
337
    php_ort_tensor_t* rv =
672✔
338
        php_ort_tensor_fetch(Z_OBJ_P(return_value));
672✔
339
    rv->object = result;
672✔
340
}
341

342
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_backend_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
343
ZEND_END_ARG_INFO()
344

345
PHP_FUNCTION(backend)
16✔
346
{
347
    ZEND_PARSE_PARAMETERS_NONE();
16✔
348

349
#ifdef ORT_BACKEND_ENABLED
350
    RETURN_STRING(ORT_BACKEND_NAME);
12✔
351
#else
352
    RETURN_FALSE;
4✔
353
#endif
354
}
355

356
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_cores_arginfo, 0, 0, IS_LONG, 0)
357
ZEND_END_ARG_INFO()
358

359
PHP_FUNCTION(cores)
16✔
360
{
361
    ZEND_PARSE_PARAMETERS_NONE();
16✔
362

363
    RETURN_LONG(
16✔
364
        ort_pool_cores());
365
}
366

367
/* Function table for ONNX\Math namespace */
368
static const zend_function_entry php_ort_math_functions[] = {
369
    ZEND_NS_FE("ONNX\\Math", add, php_ort_math_add_arginfo)
370
    ZEND_NS_FE("ONNX\\Math", multiply, php_ort_math_multiply_arginfo) 
371
    ZEND_NS_FE("ONNX\\Math", subtract, php_ort_math_subtract_arginfo)
372
    ZEND_NS_FE("ONNX\\Math", divide, php_ort_math_divide_arginfo)
373
    ZEND_NS_FE("ONNX\\Math", sqrt, php_ort_math_sqrt_arginfo)
374
    ZEND_NS_FE("ONNX\\Math", sin, php_ort_math_sin_arginfo)
375
    ZEND_NS_FE("ONNX\\Math", cos, php_ort_math_cos_arginfo)
376
    ZEND_NS_FE("ONNX\\Math", exp, php_ort_math_exp_arginfo)
377
    ZEND_NS_FE("ONNX\\Math", log, php_ort_math_log_arginfo)
378
    ZEND_NS_FE("ONNX\\Math", matmul, php_ort_math_matmul_arginfo)
379
    ZEND_NS_FE("ONNX\\Math", asin, php_ort_math_asin_arginfo)
380
    ZEND_NS_FE("ONNX\\Math", acos, php_ort_math_acos_arginfo)
381
    ZEND_NS_FE("ONNX\\Math", atan, php_ort_math_atan_arginfo)
382
    ZEND_NS_FE("ONNX\\Math", sinh, php_ort_math_sinh_arginfo)
383
    ZEND_NS_FE("ONNX\\Math", cosh, php_ort_math_cosh_arginfo)
384
    ZEND_NS_FE("ONNX\\Math", tanh, php_ort_math_tanh_arginfo)
385
    ZEND_NS_FE("ONNX\\Math", exp2, php_ort_math_exp2_arginfo)
386
    ZEND_NS_FE("ONNX\\Math", log2, php_ort_math_log2_arginfo)
387
    ZEND_NS_FE("ONNX\\Math", log10, php_ort_math_log10_arginfo)
388
    ZEND_NS_FE("ONNX\\Math", cbrt, php_ort_math_cbrt_arginfo)
389
    ZEND_NS_FE("ONNX\\Math", ceil, php_ort_math_ceil_arginfo)
390
    ZEND_NS_FE("ONNX\\Math", floor, php_ort_math_floor_arginfo)
391
    ZEND_NS_FE("ONNX\\Math", round, php_ort_math_round_arginfo)
392
    ZEND_NS_FE("ONNX\\Math", sign, php_ort_math_sign_arginfo)
393
    ZEND_NS_FE("ONNX\\Math", tan, php_ort_math_tan_arginfo)
394
    ZEND_NS_FE("ONNX\\Math", abs, php_ort_math_abs_arginfo)
395
    ZEND_NS_FE("ONNX\\Math", pow, php_ort_math_pow_arginfo)
396
    ZEND_NS_FE("ONNX\\Math", mod, php_ort_math_mod_arginfo)
397

398
    ZEND_NS_FE("ONNX\\Math", neg,     php_ort_math_neg_arginfo)
399
    ZEND_NS_FE("ONNX\\Math", recip,   php_ort_math_recip_arginfo)
400
    ZEND_NS_FE("ONNX\\Math", trunc,   php_ort_math_trunc_arginfo)
401

402
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", min, php_ort_math_reduce_tensor_min, php_ort_math_reduce_tensor_min_arginfo)
403
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   min, php_ort_math_reduce_axis_min,   php_ort_math_reduce_axis_min_arginfo)
404
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", max, php_ort_math_reduce_tensor_max, php_ort_math_reduce_tensor_max_arginfo)
405
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   max, php_ort_math_reduce_axis_max,   php_ort_math_reduce_axis_max_arginfo)
406
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", mean, php_ort_math_reduce_tensor_mean, php_ort_math_reduce_tensor_mean_arginfo)
407
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   mean, php_ort_math_reduce_axis_mean,   php_ort_math_reduce_axis_mean_arginfo)
408
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\tensor", sum, php_ort_math_reduce_tensor_sum, php_ort_math_reduce_tensor_sum_arginfo)
409
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   sum, php_ort_math_reduce_axis_sum,   php_ort_math_reduce_axis_sum_arginfo)
410
    ZEND_NS_NAMED_FE("ONNX\\Math\\reduce\\axis",   softmax, php_ort_math_reduce_axis_softmax,   php_ort_math_reduce_axis_softmax_arginfo)
411

412
    ZEND_NS_FE("ONNX\\Math", dot, php_ort_math_dot_arginfo)
413

414
    ZEND_NS_FE("ONNX\\Math", backend, php_ort_math_backend_arginfo)
415
    ZEND_NS_FE("ONNX\\Math", cores,   php_ort_math_cores_arginfo)
416
    ZEND_FE_END
417
};
418

419
/* {{{ */
420
ZEND_BEGIN_ARG_INFO_EX(php_ort_math_schema___construct_arginfo, 0, 0, 1)
421
    ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
422
ZEND_END_ARG_INFO()
423

424
PHP_METHOD(ONNX_Math_Schema, __construct)
560✔
425
{
426
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
560✔
427
    zend_string *symbol;
560✔
428

429
    ZEND_PARSE_PARAMETERS_START(1, 1)
560✔
430
        Z_PARAM_STR(symbol)
1,120✔
431
    ZEND_PARSE_PARAMETERS_END();
560✔
432

433
    if (zend_string_equals_literal_ci(symbol, "acos")) {
560✔
434
        ort->schema = &ort_math_promotion_schema_acos;
16✔
435
    } else if (zend_string_equals_literal_ci(symbol, "add")) {
544✔
436
        ort->schema = &ort_math_promotion_schema_add;
64✔
437
    } else if (zend_string_equals_literal_ci(symbol, "asin")) {
480✔
438
        ort->schema = &ort_math_promotion_schema_asin;
16✔
439
    } else if (zend_string_equals_literal_ci(symbol, "atan")) {
464✔
440
        ort->schema = &ort_math_promotion_schema_atan;
16✔
441
    } else if (zend_string_equals_literal_ci(symbol, "cos")) {
448✔
442
        ort->schema = &ort_math_promotion_schema_cos;
16✔
443
    } else if (zend_string_equals_literal_ci(symbol, "cbrt")) {
432✔
444
        ort->schema = &ort_math_promotion_schema_cbrt;
16✔
445
    } else if (zend_string_equals_literal_ci(symbol, "cosh")) {
416✔
446
        ort->schema = &ort_math_promotion_schema_cosh;
16✔
447
    } else if (zend_string_equals_literal_ci(symbol, "div")) {
400✔
448
        ort->schema = &ort_math_promotion_schema_div;
16✔
449
    } else if (zend_string_equals_literal_ci(symbol, "dot")) {
384✔
450
        ort->schema = &ort_math_promotion_schema_dot;
16✔
451
    } else if (zend_string_equals_literal_ci(symbol, "exp")) {
368✔
452
        ort->schema = &ort_math_promotion_schema_exp;
16✔
453
    } else if (zend_string_equals_literal_ci(symbol, "exp2")) {
352✔
454
        ort->schema = &ort_math_promotion_schema_exp2;
16✔
455
    } else if (zend_string_equals_literal_ci(symbol, "log")) {
336✔
456
        ort->schema = &ort_math_promotion_schema_log;
16✔
457
    } else if (zend_string_equals_literal_ci(symbol, "log2")) {
320✔
458
        ort->schema = &ort_math_promotion_schema_log2;
16✔
459
    } else if (zend_string_equals_literal_ci(symbol, "log10")) {
304✔
460
        ort->schema = &ort_math_promotion_schema_log10;
16✔
461
    } else if (zend_string_equals_literal_ci(symbol, "matmul")) {
288✔
462
        ort->schema = &ort_math_promotion_schema_matmul;
16✔
463
    } else if (zend_string_equals_literal_ci(symbol, "max")) {
272✔
464
        ort->schema = &ort_math_promotion_schema_max;
16✔
465
    } else if (zend_string_equals_literal_ci(symbol, "mean")) {
256✔
466
        ort->schema = &ort_math_promotion_schema_mean;
16✔
467
    } else if (zend_string_equals_literal_ci(symbol, "min")) {
240✔
468
        ort->schema = &ort_math_promotion_schema_min;
16✔
469
    } else if (zend_string_equals_literal_ci(symbol, "mod")) {
224✔
470
        ort->schema = &ort_math_promotion_schema_mod;
16✔
471
    } else if (zend_string_equals_literal_ci(symbol, "mul")) {
208✔
472
        ort->schema = &ort_math_promotion_schema_mul;
16✔
473
    } else if (zend_string_equals_literal_ci(symbol, "neg")) {
192✔
474
        ort->schema = &ort_math_promotion_schema_neg;
16✔
475
    } else if (zend_string_equals_literal_ci(symbol, "pow")) {
176✔
476
        ort->schema = &ort_math_promotion_schema_pow;
16✔
477
    } else if (zend_string_equals_literal_ci(symbol, "recip")) {
160✔
478
        ort->schema = &ort_math_promotion_schema_recip;
16✔
479
    } else if (zend_string_equals_literal_ci(symbol, "sign")) {
144✔
480
        ort->schema = &ort_math_promotion_schema_sign;
16✔
481
    } else if (zend_string_equals_literal_ci(symbol, "sin")) {
128✔
482
        ort->schema = &ort_math_promotion_schema_sin;
16✔
483
    } else if (zend_string_equals_literal_ci(symbol, "sinh")) {
112✔
484
        ort->schema = &ort_math_promotion_schema_sinh;
16✔
485
    } else if (zend_string_equals_literal_ci(symbol, "softmax")) {
96✔
486
        ort->schema = &ort_math_promotion_schema_softmax;
16✔
487
    } else if (zend_string_equals_literal_ci(symbol, "sqrt")) {
80✔
488
        ort->schema = &ort_math_promotion_schema_sqrt;
16✔
489
    } else if (zend_string_equals_literal_ci(symbol, "sub")) {
64✔
490
        ort->schema = &ort_math_promotion_schema_sub;
16✔
491
    } else if (zend_string_equals_literal_ci(symbol, "sum")) {
48✔
492
        ort->schema = &ort_math_promotion_schema_sum;
16✔
493
    } else if (zend_string_equals_literal_ci(symbol, "tan")) {
32✔
494
        ort->schema = &ort_math_promotion_schema_tan;
16✔
495
    } else if (zend_string_equals_literal_ci(symbol, "tanh")) {
16✔
496
        ort->schema = &ort_math_promotion_schema_tanh;
16✔
497
    } else {
498
        /* throw */
UNCOV
499
        return;
×
500
    }
501

502
    ort->symbol = zend_string_copy(symbol);
1,072✔
503
}
504

505
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_schema_getSymbol_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_NULL)
506
ZEND_END_ARG_INFO()
507

508
PHP_METHOD(ONNX_Math_Schema, getSymbol)
16✔
509
{
510
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
511

512
    if (!ort->symbol) {
16✔
513
        return;
514
    }
515

516
    RETURN_STR_COPY(ort->symbol);
16✔
517
}
518

519
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_getKind_arginfo, 0, 0, IS_LONG, 0)
520
ZEND_END_ARG_INFO()
521

522
PHP_METHOD(ONNX_Math_Schema, getKind)
16✔
523
{
524
    php_ort_math_schema_t *ort =
16✔
525
        php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
526

527
    ZEND_PARSE_PARAMETERS_NONE();
16✔
528

529
    RETURN_LONG(ort->schema->kind);
16✔
530
}
531

532
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_resolve_arginfo, 0, 1, IS_LONG, 0)
533
    ZEND_ARG_VARIADIC_INFO(0, types)
534
ZEND_END_ARG_INFO()
535

536
PHP_METHOD(ONNX_Math_Schema, resolve)
16✔
537
{
538
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
539
    zval *types;
16✔
540
    size_t count;
16✔
541

542
    ZEND_PARSE_PARAMETERS_START(1, -1)
16✔
543
        Z_PARAM_VARIADIC('+', types, count)
16✔
544
    ZEND_PARSE_PARAMETERS_END();
16✔
545

546
    if (count == 0) {
16✔
547
        /* throw */
548
        RETURN_LONG(-1);
×
549
    }
550

551
    if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY) {
16✔
552
        if (count != 2) {
16✔
553
            /* throw */
UNCOV
554
            RETURN_LONG(-1);
×
555
        }
556

557
        if (Z_TYPE(types[0]) != IS_LONG || Z_TYPE(types[1]) != IS_LONG) {
16✔
558
            /* throw */
UNCOV
559
            RETURN_LONG(-1);
×
560
        }
561

562
        ONNXTensorElementDataType result =
16✔
563
            ort_math_type_promotion_schema_resolve_binary(
32✔
564
                ort->schema,
565
                (ONNXTensorElementDataType) Z_LVAL(types[0]),
16✔
566
                (ONNXTensorElementDataType) Z_LVAL(types[1]));
16✔
567
        RETURN_LONG(result);
16✔
UNCOV
568
    } else if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY) {
×
569
        if (count != 1) {
×
UNCOV
570
            RETURN_LONG(-1);
×
571
        }
572

573
        if (Z_TYPE(types[0]) != IS_LONG) {
×
574
            /* throw */
575
            RETURN_LONG(-1);
×
576
        }
577

UNCOV
578
        ONNXTensorElementDataType result =
×
579
            ort_math_type_promotion_schema_resolve_unary(
×
580
                ort->schema,
UNCOV
581
                (ONNXTensorElementDataType) Z_LVAL(types[0]));
×
UNCOV
582
        RETURN_LONG(result);
×
583
    } else {
584
        /* throw */
UNCOV
585
        RETURN_LONG(-1);
×
586
    }
587
}
588

589
static const zend_function_entry php_ort_math_schema_methods[] = {
590
    PHP_ME(ONNX_Math_Schema, __construct, php_ort_math_schema___construct_arginfo, ZEND_ACC_PUBLIC)
591
    PHP_ME(ONNX_Math_Schema, getSymbol,   php_ort_math_schema_getSymbol_arginfo,   ZEND_ACC_PUBLIC)
592
    PHP_ME(ONNX_Math_Schema, getKind,     php_ort_math_schema_getKind_arginfo,     ZEND_ACC_PUBLIC)
593
    PHP_ME(ONNX_Math_Schema, resolve,     php_ort_math_schema_resolve_arginfo,     ZEND_ACC_PUBLIC)
594
    PHP_FE_END
595
}; /* }}} */
596

597
PHP_MINIT_FUNCTION(ORT_MATH)
3,136✔
598
{
599
    ort_math_startup();
3,136✔
600

601
    zend_class_entry ce;
3,136✔
602

603
    INIT_NS_CLASS_ENTRY(ce, "ONNX\\Math", "Schema", php_ort_math_schema_methods);
3,136✔
604
    php_ort_math_schema_ce = zend_register_internal_class(&ce);
3,136✔
605
    php_ort_math_schema_ce->create_object = php_ort_math_schema_create;
3,136✔
606
    php_ort_math_schema_ce->ce_flags |= ZEND_ACC_FINAL;
3,136✔
607

608
    zend_declare_class_constant_long(
3,136✔
609
        php_ort_math_schema_ce,
610
        ZEND_STRL("BINARY"),
611
        ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY);
612
    zend_declare_class_constant_long(
3,136✔
613
        php_ort_math_schema_ce,
614
        ZEND_STRL("UNARY"),
615
        ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY);
616

617
    memcpy(&php_ort_math_schema_handlers,
3,136✔
618
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
619
    php_ort_math_schema_handlers.offset = XtOffsetOf(php_ort_math_schema_t, std);
3,136✔
620
    php_ort_math_schema_handlers.free_obj = php_ort_math_schema_free;
3,136✔
621
    php_ort_math_schema_handlers.get_debug_info = php_ort_math_schema_debug;
3,136✔
622
    php_ort_math_schema_handlers.clone_obj = NULL; // No cloning support
3,136✔
623

624
    zend_register_functions(NULL,
3,136✔
625
        php_ort_math_functions, NULL, MODULE_PERSISTENT);
626

627
    return SUCCESS;
3,136✔
628
}
629

630
PHP_RINIT_FUNCTION(ORT_MATH)
3,136✔
631
{
632
    ort_math_activate();
3,136✔
633

634
    return SUCCESS;
3,136✔
635
}
636

637
PHP_RSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
638
{
639
    ort_math_deactivate();
3,136✔
640

641
    return SUCCESS;
3,136✔
642
}
643

644
PHP_MSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
645
{
646
    ort_math_shutdown();
3,136✔
647

648
    return SUCCESS;
3,136✔
649
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc