Skip to content

Commit

Permalink
Optimize scalar arithmetic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Feb 6, 2021
1 parent 4f9280d commit b7f5e0a
Show file tree
Hide file tree
Showing 13 changed files with 972 additions and 1,552 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- 2.1.2
- Blanket extension optimizations
- Optimize scalar arithmetic and comparisons

- 2.1.1
- Zephir redux
Expand Down
114 changes: 114 additions & 0 deletions ext/include/tensor_arithmetic.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,117 @@ void tensor_mod(zval * return_value, zval * a, zval * b)

RETVAL_ARR(Z_ARR(c));
}

void tensor_multiply_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval product;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
mul_function(&product, valueA, b);

add_next_index_zval(&c, &product);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}

void tensor_divide_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval quotient;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
div_function(&quotient, valueA, b);

add_next_index_zval(&c, &quotient);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}

void tensor_add_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval sum;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
add_function(&sum, valueA, b);

add_next_index_zval(&c, &sum);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}

void tensor_subtract_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval difference;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
sub_function(&difference, valueA, b);

add_next_index_zval(&c, &difference);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}

void tensor_pow_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval power;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
pow_function(&power, valueA, b);

add_next_index_zval(&c, &power);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}

void tensor_mod_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval modulus;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
mod_function(&modulus, valueA, b);

add_next_index_zval(&c, &modulus);
} ZEND_HASH_FOREACH_END();

RETVAL_ARR(Z_ARR(c));
}
12 changes: 7 additions & 5 deletions ext/include/tensor_arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
#include <Zend/zend.h>

void tensor_multiply(zval * return_value, zval * a, zval * b);

void tensor_divide(zval * return_value, zval * a, zval * b);

void tensor_add(zval * return_value, zval * a, zval * b);

void tensor_subtract(zval * return_value, zval * a, zval * b);

void tensor_pow(zval * return_value, zval * a, zval * b);

void tensor_mod(zval * return_value, zval * a, zval * b);

void tensor_multiply_scalar(zval * return_value, zval * a, zval * b);
void tensor_divide_scalar(zval * return_value, zval * a, zval * b);
void tensor_add_scalar(zval * return_value, zval * a, zval * b);
void tensor_subtract_scalar(zval * return_value, zval * a, zval * b);
void tensor_pow_scalar(zval * return_value, zval * a, zval * b);
void tensor_mod_scalar(zval * return_value, zval * a, zval * b);

#endif
Loading

0 comments on commit b7f5e0a

Please sign in to comment.