All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
50 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // MATHEMATICAL UTILITY FUNCTIONS
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
66 template< typename T >
67 inline int sign( T a );
68 
69 template< typename T >
70 inline size_t digits( T a );
71 
72 template< typename T1, typename T2 >
73 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType
74  min( const T1& a, const T2& b );
75 
76 template< typename T1, typename T2, typename T3 >
77 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
78  min( const T1& a, const T2& b, const T3& c );
79 
80 template< typename T1, typename T2 >
81 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType
82  max( const T1& a, const T2& b );
83 
84 template< typename T1, typename T2, typename T3 >
85 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
86  max( const T1& a, const T2& b, const T3& c );
87 
88 template< typename T >
89 BLAZE_ALWAYS_INLINE T round( T a );
90 
91 template< typename T1, typename T2 >
92 BLAZE_ALWAYS_INLINE bool lessThan( T1 a, T2 b );
94 //*************************************************************************************************
95 
96 
97 //*************************************************************************************************
107 template< typename T >
108 inline int sign( T a )
109 {
111 
113  return ( T(0) < a ) - ( a < T(0) );
114  else
115  return ( T(0) < a );
116 }
117 //*************************************************************************************************
118 
119 
120 //*************************************************************************************************
138 template< typename T >
139 inline size_t digits( T a )
140 {
142 
143  size_t count( 0 );
144 
145  while( a != 0 ) {
146  a /= 10;
147  ++count;
148  }
149 
150  return count;
151 }
152 //*************************************************************************************************
153 
154 
155 //*************************************************************************************************
167 template< typename T1, typename T2 >
168 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType min( const T1& a, const T2& b )
169 {
170  // The return type of the function is only a copy of the one of the arguments for two reasons:
171  // - in case the data types T1 and T2 are equal, a reference return type could result in a
172  // bug if combined with literals
173  // - in case the two data types are unequal, the result of the comparison could be converted
174  // to the more significant data type, which results in a local temporary value
175  // The copy operation might cause a performance decrease for class types, which is probably
176  // avoided if the function is inlined.
177  return ( a < b )?( a ):( b );
178 }
179 //*************************************************************************************************
180 
181 
182 //*************************************************************************************************
195 template< typename T1, typename T2, typename T3 >
196 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
197  min( const T1& a, const T2& b, const T3& c )
198 {
199  // The return type of the function is only a copy of the one of the arguments for two reasons:
200  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
201  // a bug if combined with literals
202  // - in case the three data types are unequal, the result of the comparison could be converted
203  // to the more significant data type, which results in a local temporary value
204  // The copy operation might cause a performance decrease for class types, which is probably
205  // avoided if the function is inlined.
206  return ( a < b )?( ( a < c )?( a ):( c ) ):( ( b < c )?( b ):( c ) );
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
223 template< typename T1, typename T2 >
224 BLAZE_ALWAYS_INLINE const typename MathTrait<T1,T2>::HighType max( const T1& a, const T2& b )
225 {
226  // The return type of the function is only a copy of the one of the arguments for two reasons:
227  // - in case the data types T1 and T2 are equal, a reference return type could result in a
228  // bug if combined with literals
229  // - in case the two data types are unequal, the result of the comparison could be converted
230  // to the more significant data type, which results in a local temporary value
231  // The copy operation might cause a performance decrease for class types, which is probably
232  // avoided if the function is inlined.
233  return ( a < b )?( b ):( a );
234 }
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
251 template< typename T1, typename T2, typename T3 >
252 BLAZE_ALWAYS_INLINE const typename MathTrait< typename MathTrait<T1,T2>::HighType, T3 >::HighType
253  max( const T1& a, const T2& b, const T3& c )
254 {
255  // The return type of the function is only a copy of the one of the arguments for two reasons:
256  // - in case the data types T1, T2, and T3 are equal, a reference return type could result in
257  // a bug if combined with literals
258  // - in case the three data types are unequal, the result of the comparison could be converted
259  // to the more significant data type, which results in a local temporary value
260  // The copy operation might cause a performance decrease for class types, which is probably
261  // avoided if the function is inlined.
262  return ( a < b )?( ( b < c )?( c ):( b ) ):( ( a < c )?( c ):( a ) );
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
279 template< typename T >
281 {
283  return a;
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
300 template<>
301 BLAZE_ALWAYS_INLINE float round( float a )
302 {
303  return std::floor( a + 0.5F );
304 }
306 //*************************************************************************************************
307 
308 
309 //*************************************************************************************************
321 template<>
322 BLAZE_ALWAYS_INLINE double round<double>( double a )
323 {
324  return std::floor( a + 0.5 );
325 }
327 //*************************************************************************************************
328 
329 
330 //*************************************************************************************************
342 template<>
343 BLAZE_ALWAYS_INLINE long double round<long double>( long double a )
344 {
345  return std::floor( a + 0.5L );
346 }
348 //*************************************************************************************************
349 
350 
351 //*************************************************************************************************
362 template< typename T >
363 BLAZE_ALWAYS_INLINE bool lessThan_backend( T a, T b )
364 {
365  return a < b;
366 }
368 //*************************************************************************************************
369 
370 
371 //*************************************************************************************************
385 template<>
386 BLAZE_ALWAYS_INLINE bool lessThan_backend<float>( float a, float b )
387 {
388  return ( b - a ) > 1E-8F;
389 }
391 //*************************************************************************************************
392 
393 
394 //*************************************************************************************************
408 template<>
409 BLAZE_ALWAYS_INLINE bool lessThan_backend<double>( double a, double b )
410 {
411  return ( b - a ) > 1E-8;
412 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
431 template<>
432 BLAZE_ALWAYS_INLINE bool lessThan_backend<long double>( long double a, long double b )
433 {
434  return ( b - a ) > 1E-10;
435 }
437 //*************************************************************************************************
438 
439 
440 //*************************************************************************************************
452 template< typename T1, typename T2 >
453 BLAZE_ALWAYS_INLINE bool lessThan( T1 a, T2 b )
454 {
455  typedef typename MathTrait<T1,T2>::HighType High;
456  return lessThan_backend<High>( a, b );
457 }
458 //*************************************************************************************************
459 
460 } // namespace blaze
461 
462 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
Base template for the MathTrait class.
Definition: MathTrait.h:128
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:108
#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
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
Compile time assertion.
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:947
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:280
Header file for basic type definitions.
size_t digits(T a)
Returns the number of valid digits of an integral value.
Definition: Functions.h:139
BLAZE_ALWAYS_INLINE bool lessThan(T1 a, T2 b)
Generic less-than comparison.
Definition: Functions.h:453
System settings for the inline keywords.
Constraint on the data type.