Skip to content

Commit

Permalink
Optimize scalar comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Feb 6, 2021
1 parent b7f5e0a commit 54e3190
Show file tree
Hide file tree
Showing 12 changed files with 863 additions and 1,779 deletions.
138 changes: 138 additions & 0 deletions ext/include/tensor_comparison.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,141 @@ void tensor_less_equal(zval * return_value, zval * a, zval * b)

RETVAL_ARR(Z_ARR(c));
}

void tensor_equal_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_IS_EQUAL(valueA, b)) {
ZVAL_LONG(&comparison, 1);
} else {
ZVAL_LONG(&comparison, 0);
}

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

RETVAL_ARR(Z_ARR(c));
}

void tensor_not_equal_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_IS_EQUAL(valueA, b)) {
ZVAL_LONG(&comparison, 0);
} else {
ZVAL_LONG(&comparison, 1);
}

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

RETVAL_ARR(Z_ARR(c));
}

void tensor_greater_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_GT(valueA, b)) {
ZVAL_LONG(&comparison, 1);
} else {
ZVAL_LONG(&comparison, 0);
}

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

RETVAL_ARR(Z_ARR(c));
}

void tensor_greater_equal_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_GE(valueA, b)) {
ZVAL_LONG(&comparison, 1);
} else {
ZVAL_LONG(&comparison, 0);
}

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

RETVAL_ARR(Z_ARR(c));
}

void tensor_less_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_LT(valueA, b)) {
ZVAL_LONG(&comparison, 1);
} else {
ZVAL_LONG(&comparison, 0);
}

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

RETVAL_ARR(Z_ARR(c));
}

void tensor_less_equal_scalar(zval * return_value, zval * a, zval * b)
{
zval * valueA;
zval comparison;
zval c;

zend_array * aHat = Z_ARR_P(a);

array_init_size(&c, zend_array_count(aHat));

ZEND_HASH_FOREACH_VAL(aHat, valueA) {
if (ZEPHIR_LE(valueA, b)) {
ZVAL_LONG(&comparison, 1);
} else {
ZVAL_LONG(&comparison, 0);
}

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

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

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

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

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

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

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

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

void tensor_equal_scalar(zval * return_value, zval * a, zval * b);
void tensor_not_equal_scalar(zval * return_value, zval * a, zval * b);
void tensor_greater_scalar(zval * return_value, zval * a, zval * b);
void tensor_greater_equal_scalar(zval * return_value, zval * a, zval * b);
void tensor_less_scalar(zval * return_value, zval * a, zval * b);
void tensor_less_equal_scalar(zval * return_value, zval * a, zval * b);

#endif
Loading

0 comments on commit 54e3190

Please sign in to comment.