Functions.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_FUNCTIONS_H_
36 #define _BLAZE_MATH_FUNCTIONS_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cmath>
44 #include <blaze/system/Inline.h>
47 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // MATHEMATICAL UTILITY FUNCTIONS
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
65 template< typename T >
66 inline constexpr int sign( T a ) noexcept;
67 
68 template< typename T >
69 inline size_t digits( T a ) noexcept;
70 
71 template< typename T1, typename T2 >
72 BLAZE_ALWAYS_INLINE constexpr auto nextMultiple( T1 value, T2 factor ) noexcept;
73 
74 template< typename T1, typename T2 >
75 BLAZE_ALWAYS_INLINE constexpr bool less( const T1& a, const T2& b )
76  noexcept( IsBuiltin< CommonType_<T1,T2> >::value );
77 
78 template< typename T1, typename T2 >
79 BLAZE_ALWAYS_INLINE constexpr bool greater( const T1& a, const T2& b )
80  noexcept( IsBuiltin< CommonType_<T1,T2> >::value );
82 //*************************************************************************************************
83 
84 
85 //*************************************************************************************************
95 template< typename T >
96 inline constexpr int sign( T a ) noexcept
97 {
99 
101  ?( T(0) < a ) - ( a < T(0) )
102  :( T(0) < a );
103 }
104 //*************************************************************************************************
105 
106 
107 //*************************************************************************************************
125 template< typename T >
126 inline size_t digits( T a ) noexcept
127 {
129 
130  size_t count( 0 );
131 
132  while( a != 0 ) {
133  a /= 10;
134  ++count;
135  }
136 
137  return count;
138 }
139 //*************************************************************************************************
140 
141 
142 //*************************************************************************************************
155 template< typename T1, typename T2 >
156 BLAZE_ALWAYS_INLINE constexpr auto nextMultiple( T1 value, T2 factor ) noexcept
157 {
158  return ( value + ( factor - ( value % factor ) ) % factor );
159 }
160 //*************************************************************************************************
161 
162 
163 //*************************************************************************************************
174 template< typename T >
175 BLAZE_ALWAYS_INLINE constexpr bool less_backend( const T& a, const T& b )
176  noexcept( IsBuiltin<T>::value )
177 {
178  return a < b;
179 }
181 //*************************************************************************************************
182 
183 
184 //*************************************************************************************************
198 BLAZE_ALWAYS_INLINE constexpr bool less_backend( float a, float b ) noexcept
199 {
200  return ( b - a ) > 1E-8F;
201 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
220 BLAZE_ALWAYS_INLINE constexpr bool less_backend( double a, double b ) noexcept
221 {
222  return ( b - a ) > 1E-8;
223 }
225 //*************************************************************************************************
226 
227 
228 //*************************************************************************************************
242 BLAZE_ALWAYS_INLINE constexpr bool less_backend( long double a, long double b ) noexcept
243 {
244  return ( b - a ) > 1E-10;
245 }
247 //*************************************************************************************************
248 
249 
250 //*************************************************************************************************
262 template< typename T1, typename T2 >
263 BLAZE_ALWAYS_INLINE constexpr bool less( const T1& a, const T2& b )
264  noexcept( IsBuiltin< CommonType_<T1,T2> >::value )
265 {
266  return less_backend< CommonType_<T1,T2> >( a, b );
267 }
268 //*************************************************************************************************
269 
270 
271 //*************************************************************************************************
282 template< typename T >
283 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( const T& a, const T& b )
284  noexcept( IsBuiltin<T>::value )
285 {
286  return a > b;
287 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
306 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( float a, float b ) noexcept
307 {
308  return ( b - a ) > 1E-8F;
309 }
311 //*************************************************************************************************
312 
313 
314 //*************************************************************************************************
328 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( double a, double b ) noexcept
329 {
330  return ( b - a ) > 1E-8;
331 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
350 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( long double a, long double b ) noexcept
351 {
352  return ( b - a ) > 1E-10;
353 }
355 //*************************************************************************************************
356 
357 
358 //*************************************************************************************************
370 template< typename T1, typename T2 >
371 BLAZE_ALWAYS_INLINE constexpr bool greater( const T1& a, const T2& b )
372  noexcept( IsBuiltin< CommonType_<T1,T2> >::value )
373 {
374  return greater_backend< CommonType_<T1,T2> >( a, b );
375 }
376 //*************************************************************************************************
377 
378 } // namespace blaze
379 
380 #endif
Header file for basic type definitions.
BLAZE_ALWAYS_INLINE constexpr bool less(const T1 &a, const T2 &b) noexcept(IsBuiltin< CommonType_< T1, T2 > >::value)
Generic less-than comparison.
Definition: Functions.h:263
constexpr int sign(T a) noexcept
Sign function.
Definition: Functions.h:96
Compile time check for signed data types.This type trait tests whether or not the given template para...
Definition: IsSigned.h:77
BLAZE_ALWAYS_INLINE constexpr bool greater(const T1 &a, const T2 &b) noexcept(IsBuiltin< CommonType_< T1, T2 > >::value)
Generic greater-than comparison.
Definition: Functions.h:371
#define BLAZE_CONSTRAINT_MUST_BE_INTEGRAL_TYPE(T)
Constraint on the data type.In case the given data type T is not an integral data type...
Definition: Integral.h:60
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
Header file for the IsFloatingPoint type trait.
size_t digits(T a) noexcept
Returns the number of valid digits of an integral value.
Definition: Functions.h:126
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:75
Constraint on the data type.
Header file for the CommonType type trait.
BLAZE_ALWAYS_INLINE constexpr auto nextMultiple(T1 value, T2 factor) noexcept
Rounds up an integral value to the next multiple of a given factor.
Definition: Functions.h:156
Compile time check for built-in data types.This type trait tests whether or not the given template pa...
Definition: IsBuiltin.h:75
Header file for the IsSigned type trait.
#define BLAZE_CONSTRAINT_MUST_BE_BUILTIN_TYPE(T)
Constraint on the data type.In case the given data type T is not a built-in data type, a compilation error is created.
Definition: Builtin.h:60
Header file for the IsBuiltin type trait.
typename CommonType< T... >::Type CommonType_
Auxiliary alias declaration for the CommonType type trait.The CommonType_ alias declaration provides ...
Definition: CommonType.h:95
System settings for the inline keywords.
Constraint on the data type.