浏览代码

Inlined math functions.

woollybah 5 年之前
父节点
当前提交
4ddfc87a5e
共有 5 个文件被更改,包括 311 次插入70 次删除
  1. 173 0
      math.mod/bbMath.h
  2. 18 0
      math.mod/doc/round.bmx
  3. 11 0
      math.mod/doc/trunc.bmx
  4. 42 39
      math.mod/math.bmx
  5. 67 31
      math.mod/math.c

+ 173 - 0
math.mod/bbMath.h

@@ -0,0 +1,173 @@
+#include <math.h>
+
+#define RAD_TO_DEG 57.2957795130823208767981548141052
+#define DEG_TO_RAD 0.0174532925199432957692369076848861
+
+#ifndef __STDC_VERSION__
+	#define __STDC_VERSION__ 0
+#endif
+
+
+#if __STDC_VERSION__ >= 199901L
+
+inline int bbIsNan( double x ){
+	return isnan(x) ? 1 : 0;
+}
+inline int bbIsInf( double x ){
+	return isinf(x) ? 1 : 0;
+}
+inline double bbSqr( double x ){
+	return sqrt( x );
+}
+inline double bbSin( double x ){
+	return sin( x*DEG_TO_RAD );
+}
+inline double bbCos( double x ){
+	return cos( x*DEG_TO_RAD );
+}
+inline double bbTan( double x ){
+	return tan( x*DEG_TO_RAD );
+}
+inline double bbASin( double x ){
+	return asin( x ) * RAD_TO_DEG;
+}
+inline double bbACos( double x ){
+	return acos( x ) * RAD_TO_DEG;
+}
+inline double bbATan( double x ){
+	return atan( x ) * RAD_TO_DEG;
+}
+inline double bbATan2( double y,double x ){
+	return atan2( y,x ) * RAD_TO_DEG;
+}
+inline double bbSinh( double x ){
+	return sinh( x );
+}
+inline double bbCosh( double x ){
+	return cosh( x );
+}
+inline double bbTanh( double x ){
+	return tanh( x );
+}
+inline double bbExp( double x ){
+	return exp( x );
+}
+inline double bbFloor( double x ){
+	return floor( x );
+}
+inline double bbLog( double x ){
+	return log(x);
+}
+inline double bbLog10( double x ){
+	return log10(x);
+}
+inline double bbCeil( double x ){
+	return ceil( x );
+}
+inline double bbRound( double x ){
+	return round( x );
+}
+inline double bbTrunc( double x ){
+	return trunc( x );
+}
+
+#define RAD_TO_DEGF 57.2957795
+#define DEG_TO_RADF 0.0174532
+
+inline float bbSqrf( float x ){
+	return sqrtf( x );
+}
+inline float bbSinf( float x ){
+	return sinf( x*DEG_TO_RADF );
+}
+inline float bbCosf( float x ){
+	return cosf( x*DEG_TO_RADF );
+}
+inline float bbTanf( float x ){
+	return tanf( x*DEG_TO_RADF );
+}
+inline float bbASinf( float x ){
+	return asinf( x ) * RAD_TO_DEGF;
+}
+inline float bbACosf( float x ){
+	return acosf( x ) * RAD_TO_DEGF;
+}
+inline float bbATanf( float x ){
+	return atanf( x ) * RAD_TO_DEGF;
+}
+inline float bbATan2f( float y,float x ){
+	return atan2f( y,x ) * RAD_TO_DEGF;
+}
+inline float bbSinhf( float x ){
+	return sinhf( x );
+}
+inline float bbCoshf( float x ){
+	return coshf( x );
+}
+inline float bbTanhf( float x ){
+	return tanhf( x );
+}
+inline float bbExpf( float x ){
+	return expf( x );
+}
+inline float bbFloorf( float x ){
+	return floorf( x );
+}
+inline float bbLogf( float x ){
+	return logf(x);
+}
+inline float bbLog10f( float x ){
+	return log10f(x);
+}
+inline float bbCeilf( float x ){
+	return ceilf( x );
+}
+inline float bbRoundf( float x ){
+	return roundf( x );
+}
+inline float bbTruncf( float x ){
+	return truncf( x );
+}
+#else
+int bbIsNan( double x );
+int bbIsInf( double x );
+double bbSqr( double x );
+double bbSin( double x );
+double bbCos( double x );
+double bbTan( double x );
+double bbASin( double x );
+double bbACos( double x );
+double bbATan( double x );
+double bbATan2( double y,double x );
+double bbSinh( double x );
+double bbCosh( double x );
+double bbTanh( double x );
+double bbExp( double x );
+double bbFloor( double x );
+double bbLog( double x );
+double bbLog10( double x );
+double bbCeil( double x );
+double bbRound( double x );
+double bbTrunc( double x );
+
+int bbIsNanf( float x );
+int bbIsInff( float x );
+float bbSqrf( float x );
+float bbSinf( float x );
+float bbCosf( float x );
+float bbTanf( float x );
+float bbASinf( float x );
+float bbACosf( float x );
+float bbATanf( float x );
+float bbATan2f( float y,float x );
+float bbSinhf( float x );
+float bbCoshf( float x );
+float bbTanhf( float x );
+float bbExpf( float x );
+float bbFloorf( float x );
+float bbLogf( float x );
+float bbLog10f( float x );
+float bbCeilf( float x );
+float bbRoundf( float x );
+float bbTruncf( float x );
+#endif

+ 18 - 0
math.mod/doc/round.bmx

@@ -0,0 +1,18 @@
+Rem
+Nearest integral value to x
+End Rem
+SuperStrict
+
+Framework brl.standardio
+Import brl.math
+
+Print "Round(+2.3) = " + Round(2.3)
+Print "Round(+2.5) = " + Round(2.5)
+Print "Round(+2.7) = " + round(2.7)
+Print ""
+
+Print "Round(-2.3) = " + Round(-2.3)
+Print "Round(-2.5) = " + Round(-2.5)
+Print "Round(-2.7) = " + Round(-2.7)
+
+Print "Round(-0.0) = " + Round(-0.0)

+ 11 - 0
math.mod/doc/trunc.bmx

@@ -0,0 +1,11 @@
+Rem
+Nearest integral not greater in magnitude than x
+End Rem
+SuperStrict
+
+Framework brl.standardio
+Import brl.math
+
+Print "Trunc(+2.7) = " + Trunc(2.7)
+Print "Trunc(-2.7) = " + Trunc(-2.7)
+Print "Trunc(-0.0) = " + Trunc(-0.0)

+ 42 - 39
math.mod/math.bmx

@@ -6,12 +6,14 @@ bbdoc: Math/Math
 End Rem
 Module BRL.Math
 
-ModuleInfo "Version: 1.07"
+ModuleInfo "Version: 1.08"
 ModuleInfo "Author: Mark Sibly"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Modserver: BRL"
 
+ModuleInfo "History: 1.08"
+ModuleInfo "History: Inlined math functions."
 ModuleInfo "History: 1.07"
 ModuleInfo "History: Added Round and Trunc."
 ModuleInfo "History: 1.06"
@@ -19,6 +21,7 @@ ModuleInfo "History: Added Float versions."
 ModuleInfo "History: 1.05 Release"
 ModuleInfo "History: Added IsNan and IsInf"
 
+Import "bbMath.h"
 Import "math.c"
 
 Extern
@@ -27,193 +30,193 @@ Rem
 bbdoc: Check if a value is NAN
 returns: True if @x is 'not a number' (eg: Sqr(-1))
 End Rem
-Function IsNan( x:Double )="bbIsNan"
+Function IsNan( x:Double )="int bbIsNan(double)!"
 
 Rem
 bbdoc: Check if a value is infinite (eg: 1.0/0.0)
 returns: True if @x is infinite
 End Rem
-Function IsInf( x:Double )="bbIsInf"
+Function IsInf( x:Double )="int bbIsInf(double)!"
 
 Rem
 bbdoc: Square root of @x
 End Rem
-Function Sqr:Double( x:Double )="bbSqr"
+Function Sqr:Double( x:Double )="double bbSqr(double)!"
 
 Rem
 bbdoc: Sine of @x degrees
 End Rem
-Function Sin:Double( x:Double )="bbSin"
+Function Sin:Double( x:Double )="double bbSin(double)!"
 
 Rem
 bbdoc: Cosine of @x degrees
 End Rem
-Function Cos:Double( x:Double )="bbCos"
+Function Cos:Double( x:Double )="double bbCos(double)!"
 
 Rem
 bbdoc: Tangent of @x degrees
 End Rem
-Function Tan:Double( x:Double )="bbTan"
+Function Tan:Double( x:Double )="double bbTan(double)!"
 
 Rem
 bbdoc: Inverse Sine of @x 
 End Rem
-Function ASin:Double( x:Double )="bbASin"
+Function ASin:Double( x:Double )="double bbASin(double)!"
 
 Rem
 bbdoc: Inverse Cosine of @x
 End Rem
-Function ACos:Double( x:Double )="bbACos"
+Function ACos:Double( x:Double )="double bbACos(double)!"
 
 Rem
 bbdoc: Inverse Tangent of @x
 End Rem
-Function ATan:Double( x:Double )="bbATan"
+Function ATan:Double( x:Double )="double bbATan(double)!"
 
 Rem
 bbdoc: Inverse Tangent of two variables @x , @y
 End Rem
-Function ATan2:Double( y:Double,x:Double )="bbATan2"
+Function ATan2:Double( y:Double,x:Double )="double bbATan2(double,double)!"
 
 Rem
 bbdoc: Hyperbolic sine of @x
 End Rem
-Function Sinh:Double( x:Double )="bbSinh"
+Function Sinh:Double( x:Double )="double bbSinh(double)!"
 
 Rem
 bbdoc: Hyperbolic cosine of @x
 End Rem
-Function Cosh:Double( x:Double )="bbCosh"
+Function Cosh:Double( x:Double )="double bbCosh(double)!"
 
 Rem
 bbdoc: Hyperbolic tangent of @x
 End Rem
-Function Tanh:Double( x:Double )="bbTanh"
+Function Tanh:Double( x:Double )="double bbTanh(double)!"
 
 Rem
 bbdoc: Exponential function
 end rem
-Function Exp:Double( x:Double )="bbExp"
+Function Exp:Double( x:Double )="double bbExp(double)!"
 
 Rem
 bbdoc: Natural logarithm
 End Rem
-Function Log:Double( x:Double )="bbLog"
+Function Log:Double( x:Double )="double bbLog(double)!"
 
 Rem
 bbdoc: Base 10 logarithm
 End Rem
-Function Log10:Double( x:Double )="bbLog10"
+Function Log10:Double( x:Double )="double bbLog10(double)!"
 
 Rem
 bbdoc: Smallest integral value not less than @x
 End Rem
-Function Ceil:Double( x:Double )="bbCeil"
+Function Ceil:Double( x:Double )="double bbCeil(double)!"
 
 Rem
 bbdoc: Largest integral value not greater than @x
 End Rem
-Function Floor:Double( x:Double )="bbFloor"
+Function Floor:Double( x:Double )="double bbFloor(double)!"
 
 Rem
 bbdoc: Nearest integral value to @x.
 End Rem
-Function Round:Double( x:Double )="bbRound"
+Function Round:Double( x:Double )="double bbRound(double)!"
 
 Rem
 bbdoc: Nearest integral not greater in magnitude than @x.
 End Rem
-Function Trunc:Double( x:Double )="bbTrunc"
+Function Trunc:Double( x:Double )="double bbTrunc(double)!"
 
 
 Rem
 bbdoc: Square root of @x
 End Rem
-Function SqrF:Float( x:Float )="bbSqrf"
+Function SqrF:Float( x:Float )="float bbSqrf(float)!"
 
 Rem
 bbdoc: Sine of @x degrees
 End Rem
-Function SinF:Float( x:Float )="bbSinf"
+Function SinF:Float( x:Float )="float bbSinf(float)!"
 
 Rem
 bbdoc: Cosine of @x degrees
 End Rem
-Function CosF:Float( x:Float )="bbCosf"
+Function CosF:Float( x:Float )="float bbCosf(float)!"
 
 Rem
 bbdoc: Tangent of @x degrees
 End Rem
-Function TanF:Float( x:Float )="bbTanf"
+Function TanF:Float( x:Float )="float bbTanf(float)!"
 
 Rem
 bbdoc: Inverse Sine of @x 
 End Rem
-Function ASinF:Float( x:Float )="bbASinf"
+Function ASinF:Float( x:Float )="float bbASinf(float)!"
 
 Rem
 bbdoc: Inverse Cosine of @x
 End Rem
-Function ACosF:Float( x:Float )="bbACosf"
+Function ACosF:Float( x:Float )="float bbACosf(float)!"
 
 Rem
 bbdoc: Inverse Tangent of @x
 End Rem
-Function ATanF:Float( x:Float )="bbATanf"
+Function ATanF:Float( x:Float )="float bbATanf(float)!"
 
 Rem
 bbdoc: Inverse Tangent of two variables @x , @y
 End Rem
-Function ATan2F:Float( y:Float,x:Float )="bbATan2f"
+Function ATan2F:Float( y:Float,x:Float )="float bbATan2f(float,float)!"
 
 Rem
 bbdoc: Hyperbolic sine of @x
 End Rem
-Function SinhF:Float( x:Float )="bbSinhf"
+Function SinhF:Float( x:Float )="float bbSinhf(float)!"
 
 Rem
 bbdoc: Hyperbolic cosine of @x
 End Rem
-Function CoshF:Float( x:Float )="bbCoshf"
+Function CoshF:Float( x:Float )="float bbCoshf(float)!"
 
 Rem
 bbdoc: Hyperbolic tangent of @x
 End Rem
-Function TanhF:Float( x:Float )="bbTanhf"
+Function TanhF:Float( x:Float )="float bbTanhf(float)!"
 
 Rem
 bbdoc: Exponential function
 end rem
-Function ExpF:Float( x:Float )="bbExpf"
+Function ExpF:Float( x:Float )="float bbExpf(float)!"
 
 Rem
 bbdoc: Natural logarithm
 End Rem
-Function LogF:Float( x:Float )="bbLogf"
+Function LogF:Float( x:Float )="float bbLogf(float)!"
 
 Rem
 bbdoc: Base 10 logarithm
 End Rem
-Function Log10F:Float( x:Float )="bbLog10f"
+Function Log10F:Float( x:Float )="float bbLog10f(float)!"
 
 Rem
 bbdoc: Smallest integral value not less than @x
 End Rem
-Function CeilF:Float( x:Float )="bbCeilf"
+Function CeilF:Float( x:Float )="float bbCeilf(float)!"
 
 Rem
 bbdoc: Largest integral value not greater than @x
 End Rem
-Function FloorF:Float( x:Float )="bbFloorf"
+Function FloorF:Float( x:Float )="float bbFloorf(float)!"
 
 Rem
 bbdoc: Nearest integral value to @x.
 End Rem
-Function RoundF:Float( x:Float )="bbRoundf"
+Function RoundF:Float( x:Float )="float bbRoundf(float)!"
 
 Rem
 bbdoc: Nearest integral not greater in magnitude than @x.
 End Rem
-Function TruncF:Float( x:Float )="bbTruncf"
+Function TruncF:Float( x:Float )="float bbTruncf(float)!"
 
 End Extern

+ 67 - 31
math.mod/math.c

@@ -1,9 +1,54 @@
 
-#include <math.h>
+#include <bbMath.h>
 
 #define RAD_TO_DEG 57.2957795130823208767981548141052
 #define DEG_TO_RAD 0.0174532925199432957692369076848861
 
+#if __STDC_VERSION__ >= 199901L
+extern int bbIsNan( double x );
+extern int bbIsInf( double x );
+extern double bbSqr( double x );
+extern double bbSin( double x );
+extern double bbCos( double x );
+extern double bbTan( double x );
+extern double bbASin( double x );
+extern double bbACos( double x );
+extern double bbATan( double x );
+extern double bbATan2( double y,double x );
+extern double bbSinh( double x );
+extern double bbCosh( double x );
+extern double bbTanh( double x );
+extern double bbExp( double x );
+extern double bbFloor( double x );
+extern double bbLog( double x );
+extern double bbLog10( double x );
+extern double bbCeil( double x );
+extern double bbRound( double x );
+extern double bbTrunc( double x );
+
+extern int bbIsNanf( float x );
+extern int bbIsInff( float x );
+extern float bbSqrf( float x );
+extern float bbSinf( float x );
+extern float bbCosf( float x );
+extern float bbTanf( float x );
+extern float bbASinf( float x );
+extern float bbACosf( float x );
+extern float bbATanf( float x );
+extern float bbATan2f( float y,float x );
+extern float bbSinhf( float x );
+extern float bbCoshf( float x );
+extern float bbTanhf( float x );
+extern float bbExpf( float x );
+extern float bbFloorf( float x );
+extern float bbLogf( float x );
+extern float bbLog10f( float x );
+extern float bbCeilf( float x );
+extern float bbRoundf( float x );
+extern float bbTruncf( float x );
+
+#else
+
 int bbIsNan( double x ){
 	return isnan(x) ? 1 : 0;
 }
@@ -65,37 +110,27 @@ double bbTrunc( double x ){
 	return trunc( x );
 }
 
-// fallback for pre C99 versions
-#ifndef __STDC_VERSION__
-	#define __STDC_VERSION__ 0
-#endif
+#define RAD_TO_DEGF RAD_TO_DEG
+#define DEG_TO_RADF DEG_TO_RAD
 
-#if __STDC_VERSION__ < 199901L
-	#define RAD_TO_DEGF RAD_TO_DEG
-	#define DEG_TO_RADF DEG_TO_RAD
-
-	#define sqrtf sqrt
-	#define sinf sin
-	#define cosf cos
-	#define tanf tan
-	#define asinf asin
-	#define acosf acos
-	#define atanf atan
-	#define atan2f atan2
-	#define sinhf sinh
-	#define coshf cosh
-	#define tanhf tanh
-	#define expf exp
-	#define floorf floor
-	#define logf log
-	#define log10f log10
-	#define ceilf ceil
-	#define roundf round
-	#define truncf trunc
-#else
-	#define RAD_TO_DEGF 57.2957795
-	#define DEG_TO_RADF 0.0174532
-#endif
+#define sqrtf sqrt
+#define sinf sin
+#define cosf cos
+#define tanf tan
+#define asinf asin
+#define acosf acos
+#define atanf atan
+#define atan2f atan2
+#define sinhf sinh
+#define coshf cosh
+#define tanhf tanh
+#define expf exp
+#define floorf floor
+#define logf log
+#define log10f log10
+#define ceilf ceil
+#define roundf round
+#define truncf trunc
 
 float bbSqrf( float x ){
 	return sqrtf( x );
@@ -151,3 +186,4 @@ float bbRoundf( float x ){
 float bbTruncf( float x ){
 	return truncf( x );
 }
+#endif