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/mpl/And.h>
48 #include <blaze/util/Types.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // MATHEMATICAL UTILITY FUNCTIONS
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
68 template< typename T >
69 inline constexpr int sign( T a ) noexcept;
70 
71 template< typename T >
72 inline size_t digits( T a ) noexcept;
73 
74 template< typename T1, typename T2 >
75 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2>
76  min( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value );
77 
78 template< typename T1, typename T2, typename... Ts >
79 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
80  min( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value );
81 
82 template< typename T1, typename T2 >
83 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2>
84  max( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value );
85 
86 template< typename T1, typename T2, typename... Ts >
87 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
88  max( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value );
89 
90 template< typename T1, typename T2 >
91 BLAZE_ALWAYS_INLINE constexpr auto nextMultiple( T1 value, T2 factor ) noexcept;
92 
93 template< typename T1, typename T2 >
94 BLAZE_ALWAYS_INLINE constexpr bool less( const T1& a, const T2& b )
95  noexcept( IsBuiltin< CommonType_<T1,T2> >::value );
96 
97 template< typename T1, typename T2 >
98 BLAZE_ALWAYS_INLINE constexpr bool greater( const T1& a, const T2& b )
99  noexcept( IsBuiltin< CommonType_<T1,T2> >::value );
101 //*************************************************************************************************
102 
103 
104 //*************************************************************************************************
114 template< typename T >
115 inline constexpr int sign( T a ) noexcept
116 {
118 
120  ?( T(0) < a ) - ( a < T(0) )
121  :( T(0) < a );
122 }
123 //*************************************************************************************************
124 
125 
126 //*************************************************************************************************
144 template< typename T >
145 inline size_t digits( T a ) noexcept
146 {
148 
149  size_t count( 0 );
150 
151  while( a != 0 ) {
152  a /= 10;
153  ++count;
154  }
155 
156  return count;
157 }
158 //*************************************************************************************************
159 
160 
161 //*************************************************************************************************
173 template< typename T1, typename T2 >
175  min( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value )
176 {
177  return ( a < b )?( a ):( b );
178 }
179 //*************************************************************************************************
180 
181 
182 //*************************************************************************************************
195 template< typename T1, typename T2, typename... Ts >
196 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
197  min( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value )
198 {
199  return min( a, min( b, args... ) );
200 }
201 //*************************************************************************************************
202 
203 
204 //*************************************************************************************************
216 template< typename T1, typename T2 >
218  max( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value )
219 {
220  return ( a < b )?( b ):( a );
221 }
222 //*************************************************************************************************
223 
224 
225 //*************************************************************************************************
238 template< typename T1, typename T2, typename... Ts >
239 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
240  max( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value )
241 {
242  return max( a, max( b, args... ) );
243 }
244 //*************************************************************************************************
245 
246 
247 //*************************************************************************************************
260 template< typename T1, typename T2 >
261 BLAZE_ALWAYS_INLINE constexpr auto nextMultiple( T1 value, T2 factor ) noexcept
262 {
263  return ( value + ( factor - ( value % factor ) ) % factor );
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
279 template< typename T >
280 BLAZE_ALWAYS_INLINE constexpr bool less_backend( const T& a, const T& b )
281  noexcept( IsBuiltin<T>::value )
282 {
283  return a < b;
284 }
286 //*************************************************************************************************
287 
288 
289 //*************************************************************************************************
303 BLAZE_ALWAYS_INLINE constexpr bool less_backend( float a, float b ) noexcept
304 {
305  return ( b - a ) > 1E-8F;
306 }
308 //*************************************************************************************************
309 
310 
311 //*************************************************************************************************
325 BLAZE_ALWAYS_INLINE constexpr bool less_backend( double a, double b ) noexcept
326 {
327  return ( b - a ) > 1E-8;
328 }
330 //*************************************************************************************************
331 
332 
333 //*************************************************************************************************
347 BLAZE_ALWAYS_INLINE constexpr bool less_backend( long double a, long double b ) noexcept
348 {
349  return ( b - a ) > 1E-10;
350 }
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
367 template< typename T1, typename T2 >
368 BLAZE_ALWAYS_INLINE constexpr bool less( const T1& a, const T2& b )
369  noexcept( IsBuiltin< CommonType_<T1,T2> >::value )
370 {
371  return less_backend< CommonType_<T1,T2> >( a, b );
372 }
373 //*************************************************************************************************
374 
375 
376 //*************************************************************************************************
387 template< typename T >
388 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( const T& a, const T& b )
389  noexcept( IsBuiltin<T>::value )
390 {
391  return a > b;
392 }
394 //*************************************************************************************************
395 
396 
397 //*************************************************************************************************
411 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( float a, float b ) noexcept
412 {
413  return ( b - a ) > 1E-8F;
414 }
416 //*************************************************************************************************
417 
418 
419 //*************************************************************************************************
433 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( double a, double b ) noexcept
434 {
435  return ( b - a ) > 1E-8;
436 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
455 BLAZE_ALWAYS_INLINE constexpr bool greater_backend( long double a, long double b ) noexcept
456 {
457  return ( b - a ) > 1E-10;
458 }
460 //*************************************************************************************************
461 
462 
463 //*************************************************************************************************
475 template< typename T1, typename T2 >
476 BLAZE_ALWAYS_INLINE constexpr bool greater( const T1& a, const T2& b )
477  noexcept( IsBuiltin< CommonType_<T1,T2> >::value )
478 {
479  return greater_backend< CommonType_<T1,T2> >( a, b );
480 }
481 //*************************************************************************************************
482 
483 } // namespace blaze
484 
485 #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:368
Header file for the And class template.
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1755
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1802
constexpr int sign(T a) noexcept
Sign function.
Definition: Functions.h:115
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:476
#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:57
#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:145
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 IsNumeric type trait.
Header file for the CommonType type trait.
Compile time type check.This type trait determines whether the given type trait TypeTrait evaluates t...
Definition: All.h:80
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:261
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 All type trait.
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.