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>
45 #include <blaze/system/Inline.h>
49 #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 int sign( T a );
67 
68 template< typename T >
69 inline size_t digits( T a );
70 
71 template< typename T1, typename T2 >
72 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType
73  min( const T1& a, const T2& b );
74 
75 template< typename T1, typename T2, typename T3 >
76 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
77  min( const T1& a, const T2& b, const T3& c );
78 
79 template< typename T1, typename T2 >
80 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType
81  max( const T1& a, const T2& b );
82 
83 template< typename T1, typename T2, typename T3 >
84 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
85  max( const T1& a, const T2& b, const T3& c );
86 
87 template< typename T >
88 BLAZE_ALWAYS_INLINE T round( T a );
89 
90 template< typename T >
91 BLAZE_ALWAYS_INLINE T nextMultiple( T value, T factor );
92 
93 template< typename T1, typename T2 >
94 BLAZE_ALWAYS_INLINE bool lessThan( T1 a, T2 b );
96 //*************************************************************************************************
97 
98 
99 //*************************************************************************************************
109 template< typename T >
110 inline int sign( T a )
111 {
113 
115  return ( T(0) < a ) - ( a < T(0) );
116  else
117  return ( T(0) < a );
118 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
140 template< typename T >
141 inline size_t digits( T a )
142 {
144 
145  size_t count( 0 );
146 
147  while( a != 0 ) {
148  a /= 10;
149  ++count;
150  }
151 
152  return count;
153 }
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
169 template< typename T1, typename T2 >
170 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType min( const T1& a, const T2& b )
171 {
172  // The return type of the function is only a copy of the one of the arguments for two reasons:
173  // - in case the data types T1 and T2 are equal, a reference return type could result in a
174  // bug if combined with literals
175  // - in case the two data types are unequal, the result of the comparison could be converted
176  // to the more significant data type, which results in a local temporary value
177  // The copy operation might cause a performance decrease for class types, which is probably
178  // avoided if the function is inlined.
179  return ( a < b )?( a ):( b );
180 }
181 //*************************************************************************************************
182 
183 
184 //*************************************************************************************************
197 template< typename T1, typename T2, typename T3 >
198 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
199  min( const T1& a, const T2& b, const T3& c )
200 {
201  // The return type of the function is only a copy of the one of the arguments for two reasons:
202  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
203  // a bug if combined with literals
204  // - in case the three data types are unequal, the result of the comparison could be converted
205  // to the more significant data type, which results in a local temporary value
206  // The copy operation might cause a performance decrease for class types, which is probably
207  // avoided if the function is inlined.
208  return ( a < b )?( ( a < c )?( a ):( c ) ):( ( b < c )?( b ):( c ) );
209 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
225 template< typename T1, typename T2 >
226 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType max( const T1& a, const T2& b )
227 {
228  // The return type of the function is only a copy of the one of the arguments for two reasons:
229  // - in case the data types T1 and T2 are equal, a reference return type could result in a
230  // bug if combined with literals
231  // - in case the two data types are unequal, the result of the comparison could be converted
232  // to the more significant data type, which results in a local temporary value
233  // The copy operation might cause a performance decrease for class types, which is probably
234  // avoided if the function is inlined.
235  return ( a < b )?( b ):( a );
236 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
253 template< typename T1, typename T2, typename T3 >
254 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
255  max( const T1& a, const T2& b, const T3& c )
256 {
257  // The return type of the function is only a copy of the one of the arguments for two reasons:
258  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
259  // a bug if combined with literals
260  // - in case the three data types are unequal, the result of the comparison could be converted
261  // to the more significant data type, which results in a local temporary value
262  // The copy operation might cause a performance decrease for class types, which is probably
263  // avoided if the function is inlined.
264  return ( a < b )?( ( b < c )?( c ):( b ) ):( ( a < c )?( c ):( a ) );
265 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
281 template< typename T >
283 {
285  return a;
286 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
302 template<>
303 BLAZE_ALWAYS_INLINE float round( float a )
304 {
305  return std::floor( a + 0.5F );
306 }
308 //*************************************************************************************************
309 
310 
311 //*************************************************************************************************
323 template<>
324 BLAZE_ALWAYS_INLINE double round<double>( double a )
325 {
326  return std::floor( a + 0.5 );
327 }
329 //*************************************************************************************************
330 
331 
332 //*************************************************************************************************
344 template<>
345 BLAZE_ALWAYS_INLINE long double round<long double>( long double a )
346 {
347  return std::floor( a + 0.5L );
348 }
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
367 template< typename T >
368 BLAZE_ALWAYS_INLINE T nextMultiple( T value, T factor )
369 {
371 
372  if( value > T(0) && factor > T(0) )
373  return value + ( factor - ( value % factor ) ) % factor;
374  else return T(0);
375 }
376 //*************************************************************************************************
377 
378 
379 //*************************************************************************************************
390 template< typename T >
391 BLAZE_ALWAYS_INLINE bool lessThan_backend( T a, T b )
392 {
393  return a < b;
394 }
396 //*************************************************************************************************
397 
398 
399 //*************************************************************************************************
413 template<>
414 BLAZE_ALWAYS_INLINE bool lessThan_backend<float>( float a, float b )
415 {
416  return ( b - a ) > 1E-8F;
417 }
419 //*************************************************************************************************
420 
421 
422 //*************************************************************************************************
436 template<>
437 BLAZE_ALWAYS_INLINE bool lessThan_backend<double>( double a, double b )
438 {
439  return ( b - a ) > 1E-8;
440 }
442 //*************************************************************************************************
443 
444 
445 //*************************************************************************************************
459 template<>
460 BLAZE_ALWAYS_INLINE bool lessThan_backend<long double>( long double a, long double b )
461 {
462  return ( b - a ) > 1E-10;
463 }
465 //*************************************************************************************************
466 
467 
468 //*************************************************************************************************
480 template< typename T1, typename T2 >
481 BLAZE_ALWAYS_INLINE bool lessThan( T1 a, T2 b )
482 {
483  typedef typename MathTrait<T1,T2>::HighType High;
484  return lessThan_backend<High>( a, b );
485 }
486 //*************************************************************************************************
487 
488 } // namespace blaze
489 
490 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
Header file for basic type definitions.
Base template for the MathTrait class.
Definition: MathTrait.h:136
Compile time check for signed data types.This type trait tests whether or not the given template para...
Definition: IsSigned.h:96
int sign(T a)
Sign function.
Definition: Functions.h:110
#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:78
BLAZE_ALWAYS_INLINE T nextMultiple(T value, T factor)
Rounds up an integral value to the next multiple of a given factor.
Definition: Functions.h:368
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.
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:94
Constraint on the data type.
Constraint on the data type.
Header file for the IsSigned type trait.
Header file for the mathematical 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:78
BLAZE_ALWAYS_INLINE T round(T a)
Rounds the given input value.
Definition: Functions.h:282
size_t digits(T a)
Returns the number of valid digits of an integral value.
Definition: Functions.h:141
BLAZE_ALWAYS_INLINE bool lessThan(T1 a, T2 b)
Generic less-than comparison.
Definition: Functions.h:481
System settings for the inline keywords.
Constraint on the data type.