From e070bf22eebc050b5757b4d063347214fa35f3c5 Mon Sep 17 00:00:00 2001 From: Dmitry Atamanov Date: Mon, 31 Oct 2022 17:00:39 +0500 Subject: [PATCH] Added `pow` macros to math module. --- lib/std/core/types.c3 | 2 ++ lib/std/math.c3 | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/std/core/types.c3 b/lib/std/core/types.c3 index 80c3aaf60..dee790fff 100644 --- a/lib/std/core/types.c3 +++ b/lib/std/core/types.c3 @@ -123,6 +123,8 @@ macro bool is_subarray_convertable($Type) $endswitch; } +macro bool is_int($Type) = $Type.kindof == TypeKind.SIGNED_INT || $Type.kindof == TypeKind.UNSIGNED_INT; + macro bool is_intlike($Type) { $switch ($Type.kindof): diff --git a/lib/std/math.c3 b/lib/std/math.c3 index 07a3f184c..2c12b45cd 100644 --- a/lib/std/math.c3 +++ b/lib/std/math.c3 @@ -163,6 +163,19 @@ macro min(x, y) = $$min(x, y); **/ macro muladd(a, b, c) = $$fmuladd(a, b, c); +/** + * @require types::is_floatlike($typeof(x)) `The input must be a floating point value or float vector` + * @require types::is_floatlike($typeof(exp)) || types::is_int($typeof(exp)) `The input must be an integer, a floating point value or float vector` + **/ +macro pow(x, exp) +{ + $if (types::is_floatlike($typeof(exp))): + return $$pow(x, exp); + $else: + return $$pow_int(x, exp); + $endif; +} + macro sec(x) = 1 / cos(x); macro sech(x) = 2 / (exp(x) + exp(-x)); @@ -201,7 +214,7 @@ macro float float.floor(float x) = $$floor(x); macro float float.fma(float a, float b, float c) = $$fma(a, b, c); macro float float.muladd(float a, float b, float c) = $$fmuladd(a, b, c); macro float float.nearbyint(float x) = $$nearbyint(x); -macro float float.pow(float x, float exp) = $$pow(x, exp); +macro float float.pow(float x, exp) = pow(x, exp); macro float float.rint(float x) = $$rint(x); macro float float.round(float x) = $$round(x); macro float float.roundeven(float x) = $$roundeven(x); @@ -213,7 +226,7 @@ macro float[<*>] float[<*>].copysign(float[<*>] mag, float[<*>] sgn) = $$copysig macro float[<*>] float[<*>].fma(float[<*>] a, float[<*>] b, float[<*>] c) = $$fma(a, b, c); macro float[<*>] float[<*>].floor(float[<*>] x) = $$floor(x); macro float[<*>] float[<*>].nearbyint(float[<*>] x) = $$nearbyint(x); -macro float[<*>] float[<*>].pow(float[<*>] x, float[<*>] exp) = $$pow(x, exp); +macro float[<*>] float[<*>].pow(float[<*>] x, exp) = pow(x, exp); macro float[<*>] float[<*>].rint(float[<*>] x) = $$rint(x); macro float[<*>] float[<*>].round(float[<*>] x) = $$round(x); macro float[<*>] float[<*>].roundeven(float[<*>] x) = $$roundeven(x); @@ -226,7 +239,7 @@ macro double double.floor(double x) = $$floor(x); macro double double.fma(double a, double b, double c) = $$fma(a, b, c); macro double double.muladd(double a, double b, double c) = $$fmuladd(a, b, c); macro double double.nearbyint(double x) = $$nearbyint(x); -macro double double.pow(double x, double exp) = $$pow(x, exp); +macro double double.pow(double x, exp) = pow(x, exp); macro double double.rint(double x) = $$rint(x); macro double double.round(double x) = $$round(x); macro double double.roundeven(double x) = $$roundeven(x); @@ -238,7 +251,7 @@ macro double[<*>] double[<*>].copysign(double[<*>] mag, double[<*>] sgn) = $$cop macro double[<*>] double[<*>].floor(double[<*>] x) = $$floor(x); macro double[<*>] double[<*>].fma(double[<*>] a, double[<*>] b, double[<*>] c) = $$fma(a, b, c); macro double[<*>] double[<*>].nearbyint(double[<*>] x) = $$nearbyint(x); -macro double[<*>] double[<*>].pow(double[<*>] x, double[<*>] exp) = $$pow(x, exp); +macro double[<*>] double[<*>].pow(double[<*>] x, exp) = pow(x, exp); macro double[<*>] double[<*>].rint(double[<*>] x) = $$rint(x); macro double[<*>] double[<*>].round(double[<*>] x) = $$round(x); macro double[<*>] double[<*>].roundeven(double[<*>] x) = $$roundeven(x);