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

krakjoe / ort / 16252292337

13 Jul 2025 06:41PM UTC coverage: 92.236% (+0.008%) from 92.228%
16252292337

push

github

krakjoe
floor schema

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

15 existing lines in 1 file now uncovered.

5156 of 5590 relevant lines covered (92.24%)

68868.22 hits per line

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

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

63
typedef struct _php_ort_math_schema_t {
64
    const ort_math_type_promotion_schema_t* schema;
65
    zend_string*                            symbol;
66
    zend_object std;
67
} php_ort_math_schema_t;
68

69
zend_class_entry *php_ort_math_schema_ce;
70
zend_object_handlers php_ort_math_schema_handlers;
71

72
static zend_always_inline php_ort_math_schema_t* php_ort_math_schema_fetch(zend_object *o) {
73
    return (php_ort_math_schema_t*) (((char*) o) - XtOffsetOf(php_ort_math_schema_t, std));
74
}
75

76
static zend_object* php_ort_math_schema_create(zend_class_entry *ce) {
608✔
77
    php_ort_math_schema_t *ort = 
608✔
78
        zend_object_alloc(sizeof(php_ort_math_schema_t), ce);
608✔
79
        
80
    zend_object_std_init(&ort->std, ce);
608✔
81
    object_properties_init(&ort->std, ce);
608✔
82

83
    ort->std.handlers = &php_ort_math_schema_handlers;
608✔
84
    return &ort->std;
608✔
85
}
86

87
static HashTable* php_ort_math_schema_debug(zend_object *zo, int *temp) {
×
UNCOV
88
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(zo);
×
89
    HashTable *debug;
×
90

UNCOV
91
    ALLOC_HASHTABLE(debug);
×
92
    zend_hash_init(debug, 3, NULL, ZVAL_PTR_DTOR, 0);
×
93

UNCOV
94
    if (ort->symbol) {
×
95
        zval symbol;
×
96

97
        ZVAL_STR_COPY(&symbol, ort->symbol);
×
98

UNCOV
99
        zend_hash_str_update(debug,
×
100
            "symbol", sizeof("symbol") - 1,
101
            &symbol);
102
    }
103

UNCOV
104
__php_ort_tensor_debug_return:
×
105
    *temp = 1;
×
106

UNCOV
107
    return debug;
×
108
}
109

110
void php_ort_math_schema_free(zend_object *zo) {
608✔
111
    php_ort_math_schema_t* ort = php_ort_math_schema_fetch(zo);
608✔
112

113
    if (ort->symbol) {
608✔
114
        zend_string_release(ort->symbol);
608✔
115
    }
116

117
    zend_object_std_dtor(zo);
608✔
118
}
608✔
119

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

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

186

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

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

244
ORT_MATH_BINARY_FUNCTION_IMPL(add)
16,896✔
245
ORT_MATH_BINARY_FUNCTION_IMPL(multiply)
4,112✔
246
ORT_MATH_BINARY_FUNCTION_IMPL(subtract)
1,936✔
247
ORT_MATH_BINARY_FUNCTION_IMPL(divide)
3,456✔
248

249
ORT_MATH_BINARY_FUNCTION_IMPL(pow)
2,448✔
250
ORT_MATH_BINARY_FUNCTION_IMPL(mod)
2,496✔
251

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

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

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

283
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(mean)
320✔
284
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(mean)
2,176✔
285

286
ORT_MATH_REDUCTION_TENSOR_FUNCTION_IMPL(sum)
224✔
287
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(sum)
2,304✔
288

289
ORT_MATH_REDUCTION_AXIS_FUNCTION_IMPL(softmax)
832✔
290

291
/* Dot reduction function */
292
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_dot_arginfo, 0, 1, ONNX\\Tensor, 0)
293
    ZEND_ARG_OBJ_INFO(0, tensor_a, ONNX\\Tensor, 0)
294
    ZEND_ARG_OBJ_INFO(0, tensor_b, ONNX\\Tensor, 0)
295
ZEND_END_ARG_INFO()
296

297
PHP_FUNCTION(dot)
288✔
298
{
299
    zval *tensor_a, *tensor_b;
288✔
300

301
    ZEND_PARSE_PARAMETERS_START(2, 2)
288✔
302
        Z_PARAM_OBJECT_OF_CLASS(tensor_a, php_ort_tensor_interface_ce)
576✔
303
        Z_PARAM_OBJECT_OF_CLASS(tensor_b, php_ort_tensor_interface_ce)
576✔
304
    ZEND_PARSE_PARAMETERS_END();
288✔
305

306
    php_ort_tensor_t* tensor_a_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_a));
288✔
307
    php_ort_tensor_t* tensor_b_ort = php_ort_tensor_fetch(Z_OBJ_P(tensor_b));
288✔
308
    ort_tensor_t* result = ort_math_result_dot(
288✔
309
        tensor_a_ort->object, tensor_b_ort->object);
310

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

319
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(php_ort_math_matmul_arginfo, 0, 2, ONNX\\Tensor, 0)
320
    ZEND_ARG_OBJ_INFO(0, matrix_a, ONNX\\Tensor, 0)
321
    ZEND_ARG_OBJ_INFO(0, matrix_b, ONNX\\Tensor, 0)
322
ZEND_END_ARG_INFO()
323

324
PHP_FUNCTION(matmul)
672✔
325
{
326
    zval *matrix_a_zv, *matrix_b_zv;
672✔
327

328
    ZEND_PARSE_PARAMETERS_START(2, 2)
672✔
329
        Z_PARAM_OBJECT_OF_CLASS(matrix_a_zv, php_ort_tensor_interface_ce)
1,344✔
330
        Z_PARAM_OBJECT_OF_CLASS(matrix_b_zv, php_ort_tensor_interface_ce)
1,344✔
331
    ZEND_PARSE_PARAMETERS_END();
672✔
332

333
    php_ort_tensor_t* matrix_a_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_a_zv));
672✔
334
    php_ort_tensor_t* matrix_b_ort = php_ort_tensor_fetch(Z_OBJ_P(matrix_b_zv));
672✔
335
    
336
    ort_tensor_t* result = ort_math_result_matmul(matrix_a_ort->object, matrix_b_ort->object);
672✔
337

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

345
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_backend_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
346
ZEND_END_ARG_INFO()
347

348
PHP_FUNCTION(backend)
16✔
349
{
350
    ZEND_PARSE_PARAMETERS_NONE();
16✔
351

352
#ifdef ORT_BACKEND_ENABLED
353
    RETURN_STRING(ORT_BACKEND_NAME);
12✔
354
#else
355
    RETURN_FALSE;
4✔
356
#endif
357
}
358

359
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_cores_arginfo, 0, 0, IS_LONG, 0)
360
ZEND_END_ARG_INFO()
361

362
PHP_FUNCTION(cores)
16✔
363
{
364
    ZEND_PARSE_PARAMETERS_NONE();
16✔
365

366
    RETURN_LONG(
16✔
367
        ort_pool_cores());
368
}
369

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

401
    ZEND_NS_FE("ONNX\\Math", neg,     php_ort_math_neg_arginfo)
402
    ZEND_NS_FE("ONNX\\Math", recip,   php_ort_math_recip_arginfo)
403
    ZEND_NS_FE("ONNX\\Math", trunc,   php_ort_math_trunc_arginfo)
404

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

415
    ZEND_NS_FE("ONNX\\Math", dot, php_ort_math_dot_arginfo)
416

417
    ZEND_NS_FE("ONNX\\Math", backend, php_ort_math_backend_arginfo)
418
    ZEND_NS_FE("ONNX\\Math", cores,   php_ort_math_cores_arginfo)
419
    ZEND_FE_END
420
};
421

422
/* {{{ */
423
ZEND_BEGIN_ARG_INFO_EX(php_ort_math_schema___construct_arginfo, 0, 0, 1)
424
    ZEND_ARG_TYPE_INFO(0, symbol, IS_STRING, 0)
425
ZEND_END_ARG_INFO()
426

427
PHP_METHOD(ONNX_Math_Schema, __construct)
608✔
428
{
429
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
608✔
430
    zend_string *symbol;
608✔
431

432
    ZEND_PARSE_PARAMETERS_START(1, 1)
608✔
433
        Z_PARAM_STR(symbol)
1,216✔
434
    ZEND_PARSE_PARAMETERS_END();
608✔
435

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

511
    ort->symbol = zend_string_copy(symbol);
1,168✔
512
}
513

514
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(php_ort_math_schema_getSymbol_arginfo, 0, 0, MAY_BE_STRING|MAY_BE_NULL)
515
ZEND_END_ARG_INFO()
516

517
PHP_METHOD(ONNX_Math_Schema, getSymbol)
16✔
518
{
519
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
520

521
    if (!ort->symbol) {
16✔
522
        return;
523
    }
524

525
    RETURN_STR_COPY(ort->symbol);
16✔
526
}
527

528
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_getKind_arginfo, 0, 0, IS_LONG, 0)
529
ZEND_END_ARG_INFO()
530

531
PHP_METHOD(ONNX_Math_Schema, getKind)
16✔
532
{
533
    php_ort_math_schema_t *ort =
16✔
534
        php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
535

536
    ZEND_PARSE_PARAMETERS_NONE();
16✔
537

538
    RETURN_LONG(ort->schema->kind);
16✔
539
}
540

541
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(php_ort_math_schema_resolve_arginfo, 0, 1, IS_LONG, 0)
542
    ZEND_ARG_VARIADIC_INFO(0, types)
543
ZEND_END_ARG_INFO()
544

545
PHP_METHOD(ONNX_Math_Schema, resolve)
16✔
546
{
547
    php_ort_math_schema_t *ort = php_ort_math_schema_fetch(Z_OBJ_P(getThis()));
16✔
548
    zval *types;
16✔
549
    size_t count;
16✔
550

551
    ZEND_PARSE_PARAMETERS_START(1, -1)
16✔
552
        Z_PARAM_VARIADIC('+', types, count)
16✔
553
    ZEND_PARSE_PARAMETERS_END();
16✔
554

555
    if (count == 0) {
16✔
556
        /* throw */
557
        RETURN_LONG(-1);
×
558
    }
559

560
    if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY) {
16✔
561
        if (count != 2) {
16✔
562
            /* throw */
UNCOV
563
            RETURN_LONG(-1);
×
564
        }
565

566
        if (Z_TYPE(types[0]) != IS_LONG || Z_TYPE(types[1]) != IS_LONG) {
16✔
567
            /* throw */
UNCOV
568
            RETURN_LONG(-1);
×
569
        }
570

571
        ONNXTensorElementDataType result =
16✔
572
            ort_math_type_promotion_schema_resolve_binary(
32✔
573
                ort->schema,
574
                (ONNXTensorElementDataType) Z_LVAL(types[0]),
16✔
575
                (ONNXTensorElementDataType) Z_LVAL(types[1]));
16✔
576
        RETURN_LONG(result);
16✔
UNCOV
577
    } else if (ort->schema->kind == ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY) {
×
578
        if (count != 1) {
×
UNCOV
579
            RETURN_LONG(-1);
×
580
        }
581

582
        if (Z_TYPE(types[0]) != IS_LONG) {
×
583
            /* throw */
584
            RETURN_LONG(-1);
×
585
        }
586

UNCOV
587
        ONNXTensorElementDataType result =
×
588
            ort_math_type_promotion_schema_resolve_unary(
×
589
                ort->schema,
UNCOV
590
                (ONNXTensorElementDataType) Z_LVAL(types[0]));
×
UNCOV
591
        RETURN_LONG(result);
×
592
    } else {
593
        /* throw */
UNCOV
594
        RETURN_LONG(-1);
×
595
    }
596
}
597

598
static const zend_function_entry php_ort_math_schema_methods[] = {
599
    PHP_ME(ONNX_Math_Schema, __construct, php_ort_math_schema___construct_arginfo, ZEND_ACC_PUBLIC)
600
    PHP_ME(ONNX_Math_Schema, getSymbol,   php_ort_math_schema_getSymbol_arginfo,   ZEND_ACC_PUBLIC)
601
    PHP_ME(ONNX_Math_Schema, getKind,     php_ort_math_schema_getKind_arginfo,     ZEND_ACC_PUBLIC)
602
    PHP_ME(ONNX_Math_Schema, resolve,     php_ort_math_schema_resolve_arginfo,     ZEND_ACC_PUBLIC)
603
    PHP_FE_END
604
}; /* }}} */
605

606
PHP_MINIT_FUNCTION(ORT_MATH)
3,136✔
607
{
608
    ort_math_startup();
3,136✔
609

610
    zend_class_entry ce;
3,136✔
611

612
    INIT_NS_CLASS_ENTRY(ce, "ONNX\\Math", "Schema", php_ort_math_schema_methods);
3,136✔
613
    php_ort_math_schema_ce = zend_register_internal_class(&ce);
3,136✔
614
    php_ort_math_schema_ce->create_object = php_ort_math_schema_create;
3,136✔
615
    php_ort_math_schema_ce->ce_flags |= ZEND_ACC_FINAL;
3,136✔
616

617
    zend_declare_class_constant_long(
3,136✔
618
        php_ort_math_schema_ce,
619
        ZEND_STRL("BINARY"),
620
        ORT_MATH_TYPE_PROMOTION_SCHEMA_BINARY);
621
    zend_declare_class_constant_long(
3,136✔
622
        php_ort_math_schema_ce,
623
        ZEND_STRL("UNARY"),
624
        ORT_MATH_TYPE_PROMOTION_SCHEMA_UNARY);
625

626
    memcpy(&php_ort_math_schema_handlers,
3,136✔
627
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
628
    php_ort_math_schema_handlers.offset = XtOffsetOf(php_ort_math_schema_t, std);
3,136✔
629
    php_ort_math_schema_handlers.free_obj = php_ort_math_schema_free;
3,136✔
630
    php_ort_math_schema_handlers.get_debug_info = php_ort_math_schema_debug;
3,136✔
631
    php_ort_math_schema_handlers.clone_obj = NULL; // No cloning support
3,136✔
632

633
    zend_register_functions(NULL,
3,136✔
634
        php_ort_math_functions, NULL, MODULE_PERSISTENT);
635

636
    return SUCCESS;
3,136✔
637
}
638

639
PHP_RINIT_FUNCTION(ORT_MATH)
3,136✔
640
{
641
    ort_math_activate();
3,136✔
642

643
    return SUCCESS;
3,136✔
644
}
645

646
PHP_RSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
647
{
648
    ort_math_deactivate();
3,136✔
649

650
    return SUCCESS;
3,136✔
651
}
652

653
PHP_MSHUTDOWN_FUNCTION(ORT_MATH)
3,136✔
654
{
655
    ort_math_shutdown();
3,136✔
656

657
    return SUCCESS;
3,136✔
658
}
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