ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
math.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
11# include <cmath>
12#endif // !DOXYGEN
13
14#if !defined(M_PI)
15# define M_PI 3.14159265358979323846
16#endif
17#if !defined(M_E)
18# define M_E 2.7182818284590452354
19#endif
20#if !defined(M_LN10)
21# define M_LN10 2.30258509299404568402
22#endif
23
24
25//! @cond NO_DOX
26
27#define INT(box) (box).Unbox<integer>()
28#define FLT(box) (box).Unbox<double >()
29#define FUNC( name,...) Box name( Scope& scope, \
30 ArgIterator args, \
31 ArgIterator end ) \
32 { (void) scope; (void) args; (void) end; __VA_ARGS__ }
33
34namespace alib { namespace expressions { namespace plugins {
35
36namespace {
37
38// #################################################################################################
39// ### Math - constants
40// #################################################################################################
41
42Box constPI;
43Box constE;
44
45// #################################################################################################
46// ### Math - functions
47// #################################################################################################
48FUNC( max_I , return (std::max) (INT(*args),INT(*(args+1))); )
49FUNC( max_F , return (::fmax) (FLT(*args),FLT(*(args+1))); )
50FUNC( min_I , return (std::min) (INT(*args),INT(*(args+1))); )
51FUNC( min_F , return (::fmin) (FLT(*args),FLT(*(args+1))); )
52FUNC( abs_I , return std::abs (INT(*args)); )
53FUNC( abs , return ::fabs (FLT(*args)); )
54FUNC( ceil , return ::ceil (FLT(*args)); )
55FUNC( floor , return ::floor (FLT(*args)); )
56FUNC( trunc , return ::round (FLT(*args)); )
57FUNC( round , return ::round (FLT(*args)); )
58FUNC( rint , return static_cast<integer>(::llrint(FLT(*args))); )
59FUNC( remainder, return ::remainder(FLT(*args),FLT(*(args+1))); )
60
61FUNC( rand , return static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX); )
62
63FUNC( sin , return ::sin (FLT(*args)); )
64FUNC( asin , return ::asin (FLT(*args)); )
65FUNC( cos , return ::cos (FLT(*args)); )
66FUNC( acos , return ::acos (FLT(*args)); )
67FUNC( tan , return ::tan (FLT(*args)); )
68FUNC( atan , return ::atan (FLT(*args)); )
69
70FUNC( sinh , return ::sinh (FLT(*args)); )
71FUNC( asinh , return ::asinh (FLT(*args)); )
72FUNC( cosh , return ::cosh (FLT(*args)); )
73FUNC( acosh , return ::acosh (FLT(*args)); )
74FUNC( tanh , return ::tanh (FLT(*args)); )
75FUNC( atanh , return ::atanh (FLT(*args)); )
76
77FUNC( exp , return ::exp (FLT(*args)); )
78FUNC( exp2 , return ::exp2 (FLT(*args)); )
79#if defined(_WIN32) || defined(__APPLE__) || defined(__ANDROID_NDK__)
80 FUNC(exp10 , return ::pow (10.0 ,FLT(*args)); )
81#else
82 FUNC(exp10 , return ::exp10 (FLT(*args)); )
83#endif
84
85
86FUNC( log , return ::log (FLT(*args)); )
87FUNC( log2 , return ::log2 (FLT(*args)); )
88#if defined(_WIN32)
89 FUNC( log10 , return ::log (FLT(*args)) / M_LN10; )
90#else
91 FUNC( log10 , return ::log10 (FLT(*args)); )
92#endif
93
94FUNC( pow , return ::pow (FLT(*args++),FLT(*args)); )
95#if defined(_WIN32) || defined(__APPLE__) || defined(__ANDROID_NDK__)
96 FUNC(pow10 , return ::pow (10.0 ,FLT(*args)); )
97#else
98 FUNC(pow10 , return ::exp10 (FLT(*args)); )
99#endif
100FUNC( sqrt , return ::sqrt (FLT(*args)); )
101FUNC( cbrt , return ::cbrt (FLT(*args)); )
102
103
104
105} // anonymous namespace
106
107
108// #################################################################################################
109// ### Math - Constructor. Creates the hash map
110// #################################################################################################
111Math::Math( Compiler& compiler )
112: Calculus( "ALib Math", compiler, CompilePriorities::Math )
113{
114 constPI= M_PI; // Initialize constant static boxes. This must not be done
115 constE = M_E; // in the C++ bootstrap code.
116
117 // load identifier/function names from resources
118 constexpr int tableSize= 34;
119 Token functionNames[tableSize];
120 Token::LoadResourcedTokens( EXPRESSIONS, "CPM", functionNames
121 ALIB_DBG(,tableSize) );
122
124 Token* descriptor= functionNames;
125
126 // Constant identifiers
127 ConstantIdentifiers=
128 {
129 { *descriptor++, constPI },
130 { *descriptor++, constE },
131 };
132
133 Functions=
134 {
135 { *descriptor , CALCULUS_SIGNATURE(Signatures::I ), CALCULUS_CALLBACK(abs_I ), &Types::Integer , CTI },
136 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(abs ), &Types::Float , CTI },
137 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(ceil ), &Types::Float , CTI },
138 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(floor ), &Types::Float , CTI },
139 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(trunc ), &Types::Float , CTI },
140 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(round ), &Types::Float , CTI },
141 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(rint ), &Types::Integer , CTI },
142 { *descriptor , CALCULUS_SIGNATURE(Signatures::II), CALCULUS_CALLBACK(max_I ), &Types::Integer , CTI },
143 { *descriptor++, CALCULUS_SIGNATURE(Signatures::FF), CALCULUS_CALLBACK(max_F ), &Types::Float , CTI },
144 { *descriptor , CALCULUS_SIGNATURE(Signatures::II), CALCULUS_CALLBACK(min_I ), &Types::Integer , CTI },
145 { *descriptor++, CALCULUS_SIGNATURE(Signatures::FF), CALCULUS_CALLBACK(min_F ), &Types::Float , CTI },
146 { *descriptor++, CALCULUS_SIGNATURE(Signatures::FF), CALCULUS_CALLBACK(remainder), &Types::Float , CTI },
147 { *descriptor++, CALCULUS_SIGNATURE(nullptr ), CALCULUS_CALLBACK(rand ), &Types::Float , ETI },
148 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(sin ), &Types::Float , CTI },
149 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(asin ), &Types::Float , CTI },
150 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(cos ), &Types::Float , CTI },
151 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(acos ), &Types::Float , CTI },
152 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(tan ), &Types::Float , CTI },
153 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(atan ), &Types::Float , CTI },
154 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(sinh ), &Types::Float , CTI },
155 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(asinh ), &Types::Float , CTI },
156 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(cosh ), &Types::Float , CTI },
157 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(acosh ), &Types::Float , CTI },
158 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(tanh ), &Types::Float , CTI },
159 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(atanh ), &Types::Float , CTI },
160 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(exp ), &Types::Float , CTI },
161 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(exp2 ), &Types::Float , CTI },
162 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(exp10 ), &Types::Float , CTI },
163 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(log ), &Types::Float , CTI },
164 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(log2 ), &Types::Float , CTI },
165 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(log10 ), &Types::Float , CTI },
166 { *descriptor++, CALCULUS_SIGNATURE(Signatures::FF), CALCULUS_CALLBACK(pow ), &Types::Float , CTI },
167 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(pow10 ), &Types::Float , CTI },
168 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(sqrt ), &Types::Float , CTI },
169 { *descriptor++, CALCULUS_SIGNATURE(Signatures::F ), CALCULUS_CALLBACK(cbrt ), &Types::Float , CTI },
170 };
171
172 ALIB_ASSERT_ERROR( descriptor - functionNames == tableSize, "EXPR",
173 "Descriptor table size mismatch: Consumed {} descriptors, {} available.",
174 descriptor - functionNames, tableSize )
176}
177
178
179}}} // namespace [alib::expressions::detail]
180
181#undef INT
182#undef FLT
183#undef FUNC
184
185
186//! @endcond
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:849
#define CALCULUS_CALLBACK(func)
Definition calculus.hpp:29
#define CALCULUS_SIGNATURE(BoxPointerArray)
Definition calculus.hpp:36
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
#define ALIB_DBG(...)
Definition alib.hpp:390
@ Math
Collection of built-in unary and binary operators for boolean, integer and floating point values.
Definition alib.cpp:69
expressions::plugins::Calculus Calculus
Type alias in namespace alib.
Definition calculus.hpp:851
strings::util::Token Token
Type alias in namespace alib.
Definition token.hpp:551
boxing::Box Box
Type alias in namespace alib.
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.hpp:553
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
ALIB_API Math(Compiler &compiler)