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