Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
280 lines (234 sloc) 9.02 KB
//
// php-armadillo/complex.hh
//
// @Author CismonX
//
#ifndef PHP_ARMA_COMPLEX_HH
#define PHP_ARMA_COMPLEX_HH
#include "php_arma.hh"
#include <complex>
#ifdef PHP_ARMA_OPERATORS
#if PHP_VERSION_ID < 70300
// Before PHP 7.3, `-$a` is compile to `MUL -1, $a, ~` instead of `MUL $a, -1, ~`.
#define CONVERT_NEG_1_TO_COMPLEX(zv, type) \
else if (Z_TYPE_P(zv) == IS_LONG && Z_LVAL_P(zv) == -1) { \
zval_set_scalar(&tmp, std::complex<type>(-1)); \
zv1 = &tmp; \
}
#else
#define CONVERT_NEG_1_TO_COMPLEX(zv, type)
#endif
// For non-assignment operators, first operand can be of type double.
#define PHP_ARMA_COMPLEX_OPERATOR(type, func) \
if (instanceof_function(ce, complex<type>::ce)) { \
zval tmp; \
ZVAL_UNDEF(&tmp); \
if (zval_is_scalar<type>(zv1)) { \
auto real = zval_get_scalar<type>(zv1); \
zval_set_scalar(&tmp, std::complex<type>(real)); \
zv1 = &tmp; \
} \
CONVERT_NEG_1_TO_COMPLEX(zv1, type) \
else if (UNEXPECTED(Z_TYPE_P(zv1) != IS_OBJECT)) { \
return false; \
} \
auto v = complex<type>::operators::func(zv1, zv2, rv); \
if (Z_TYPE(tmp) != IS_UNDEF) { \
zval_ptr_dtor_nogc(&tmp); \
} \
return v; \
}
#define PHP_ARMA_COMPLEX_OPERATOR_ASSIGN(type, func) \
if (instanceof_function(ce, complex<type>::ce)) { \
auto v = complex<type>::operators::func(zv1, zv2, zv1); \
if (rv) { \
ZVAL_COPY(rv, zv1); \
} \
return v; \
}
#endif // PHP_ARMA_OPERATORS
using cx_double = std::complex<double>;
namespace php_arma
{
template <typename T>
struct complex
{
using native_t = std::complex<T>;
struct operators;
friend void complex_init();
PHP_ARMA_CE_HANDLRES_DECLARE();
private:
PHP_ARMA_COMMON_DECLARE();
static PHP_FUNCTION(__construct);
static PHP_FUNCTION(fromPolar);
static PHP_FUNCTION(add);
static PHP_FUNCTION(sub);
static PHP_FUNCTION(neg);
static PHP_FUNCTION(mul);
static PHP_FUNCTION(div);
static PHP_FUNCTION(abs);
static PHP_FUNCTION(arg);
static PHP_FUNCTION(norm);
static PHP_FUNCTION(conj);
static PHP_FUNCTION(exp);
static PHP_FUNCTION(log);
static PHP_FUNCTION(log10);
static PHP_FUNCTION(pow);
static PHP_FUNCTION(sqrt);
static PHP_FUNCTION(sin);
static PHP_FUNCTION(cos);
static PHP_FUNCTION(tan);
static PHP_FUNCTION(asin);
static PHP_FUNCTION(acos);
static PHP_FUNCTION(atan);
static PHP_FUNCTION(sinh);
static PHP_FUNCTION(cosh);
static PHP_FUNCTION(tanh);
static PHP_FUNCTION(asinh);
static PHP_FUNCTION(acosh);
static PHP_FUNCTION(atanh);
static zval *read_dimension(zval*, zval*, int, zval*);
static void write_property(zval*, zval*, zval*, void**);
static int compare_objects(zval*, zval*);
static void ce_init(zend_class_entry*);
};
void complex_init();
constexpr const char complex_php_name[] = "Complex";
constexpr const char real_property_name[] = "real";
constexpr const char imag_property_name[] = "imag";
inline zend_class_entry *complex_ce;
/// Helper functions for handling cx_double types.
template <>
zend_always_inline
bool zval_is_scalar<cx_double>(zval *zv)
{
return Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), PHP_ARMA_CE(complex, double));
}
template <>
zend_always_inline
const char *scalar_type_name<cx_double>()
{
return ZSTR_VAL(PHP_ARMA_CE(complex, double)->name);
}
template <>
zend_always_inline
cx_double zval_get_scalar<cx_double>(zval *zv)
{
auto zobj = Z_OBJ_P(zv);
return { Z_DVAL_P(OBJ_PROP_NUM(zobj, 0)), Z_DVAL_P(OBJ_PROP_NUM(zobj, 1)) };
}
zend_always_inline
void zval_set_scalar(zval *zv, cx_double val)
{
auto zobj = object_create(PHP_ARMA_CE(complex, double), PHP_ARMA_HANDLERS(complex, double));
ZVAL_DOUBLE(OBJ_PROP_NUM(zobj, 0), val.real());
ZVAL_DOUBLE(OBJ_PROP_NUM(zobj, 1), val.imag());
zval_ptr_dtor(zv);
ZVAL_OBJ(zv, zobj);
}
template <typename T>
zend_always_inline
void ex_bad_type(zval *zv) {
auto real_type_name = scalar_type_name<T>();
auto complex_type_name = scalar_type_name<typename complex<T>::native_t>();
auto expected = (char*)malloc((strlen(real_type_name) + strlen(complex_type_name) + 5) * sizeof(char));
sprintf(expected, "%s or %s", real_type_name, complex_type_name);
auto got = Z_TYPE_P(zv) == IS_OBJECT ? Z_OBJNAME_P(zv) : zend_get_type_by_const(Z_TYPE_P(zv));
ex_bad_type(expected, got);
free(expected);
}
template <typename T>
struct complex<T>::operators
{
zend_always_inline
static bool add(zval *zv1, zval *zv2, zval *retval)
{
auto v1 = zval_get_scalar<native_t>(zv1);
if (zval_is_scalar<native_t>(zv2)) {
zval_set_scalar(retval, v1 + zval_get_scalar<native_t>(zv2));
return true;
}
if (zval_is_scalar<T>(zv2)) {
zval_set_scalar(retval, v1 + zval_get_scalar<T>(zv2));
return true;
}
return false;
}
zend_always_inline
static bool sub(zval *zv1, zval *zv2, zval *retval)
{
auto v1 = zval_get_scalar<native_t>(zv1);
if (zval_is_scalar<native_t>(zv2)) {
zval_set_scalar(retval, v1 - zval_get_scalar<native_t>(zv2));
return true;
}
if (zval_is_scalar<T>(zv2)) {
zval_set_scalar(retval, v1 - zval_get_scalar<T>(zv2));
return true;
}
return false;
}
zend_always_inline
static void neg(zval *zv, zval *retval)
{
auto v = zval_get_scalar<native_t>(zv);
zval_set_scalar(retval, -v);
}
zend_always_inline
static bool mul(zval *zv1, zval *zv2, zval *retval)
{
auto v1 = zval_get_scalar<native_t>(zv1);
if (zval_is_scalar<native_t>(zv2)) {
zval_set_scalar(retval, v1 * zval_get_scalar<native_t>(zv2));
return true;
}
if (zval_is_scalar<T>(zv2)) {
zval_set_scalar(retval, v1 * zval_get_scalar<T>(zv2));
return true;
}
#if PHP_VERSION_ID >= 70300
if (Z_TYPE_P(zv2) == IS_LONG && Z_LVAL_P(zv2) == -1) {
// Negation operator is compiled as multiplication to -1.
neg(zv1, retval);
return true;
}
#endif
return false;
}
zend_always_inline
static bool div(zval *zv1, zval *zv2, zval *retval)
{
auto v1 = zval_get_scalar<native_t>(zv1);
if (zval_is_scalar<native_t>(zv2)) {
zval_set_scalar(retval, v1 / zval_get_scalar<native_t>(zv2));
return true;
}
if (zval_is_scalar<T>(zv2)) {
zval_set_scalar(retval, v1 / zval_get_scalar<T>(zv2));
return true;
}
return false;
}
zend_always_inline
static bool pow(zval *zv1, zval *zv2, zval *retval)
{
auto v1 = zval_get_scalar<native_t>(zv1);
if (zval_is_scalar<native_t>(zv2)) {
zval_set_scalar(retval, std::pow(v1, zval_get_scalar<native_t>(zv2)));
return true;
}
if (zval_is_scalar<T>(zv2)) {
zval_set_scalar(retval, std::pow(v1, zval_get_scalar<T>(zv2)));
return true;
}
return false;
}
zend_always_inline
static bool conj(zval *zv, zval *unused, zval *retval)
{
zval_set_scalar(retval, std::conj(zval_get_scalar<native_t>(zv)));
return true;
}
};
}
#endif // !PHP_ARMA_COMPLEX_HH
You can’t perform that action at this time.