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 T >
91 BLAZE_ALWAYS_INLINE constexpr T nextMultiple( T value, T factor ) noexcept;
92 
93 template< typename T1, typename T2 >
94 BLAZE_ALWAYS_INLINE constexpr bool lessThan( T1 a, T2 b )
95  noexcept( IsBuiltin< CommonType_<T1,T2> >::value );
97 //*************************************************************************************************
98 
99 
100 //*************************************************************************************************
110 template< typename T >
111 inline constexpr int sign( T a ) noexcept
112 {
114 
116  ?( T(0) < a ) - ( a < T(0) )
117  :( T(0) < a );
118 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
140 template< typename T >
141 inline size_t digits( T a ) noexcept
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 constexpr CommonType_<T1,T2>
171  min( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value )
172 {
173  return ( a < b )?( a ):( b );
174 }
175 //*************************************************************************************************
176 
177 
178 //*************************************************************************************************
191 template< typename T1, typename T2, typename... Ts >
192 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
193  min( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value )
194 {
195  return min( a, min( b, args... ) );
196 }
197 //*************************************************************************************************
198 
199 
200 //*************************************************************************************************
212 template< typename T1, typename T2 >
213 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2>
214  max( const T1& a, const T2& b ) noexcept( All<IsNumeric,T1,T2>::value )
215 {
216  return ( a < b )?( b ):( a );
217 }
218 //*************************************************************************************************
219 
220 
221 //*************************************************************************************************
234 template< typename T1, typename T2, typename... Ts >
235 BLAZE_ALWAYS_INLINE constexpr CommonType_<T1,T2,Ts...>
236  max( const T1& a, const T2& b, const Ts&... args ) noexcept( All<IsNumeric,T1,T2,Ts...>::value )
237 {
238  return max( a, max( b, args... ) );
239 }
240 //*************************************************************************************************
241 
242 
243 //*************************************************************************************************
257 template< typename T >
258 BLAZE_ALWAYS_INLINE constexpr T nextMultiple( T value, T factor ) noexcept
259 {
261 
262  return ( value > T(0) && factor > T(0) )
263  ?( value + ( factor - ( value % factor ) ) % factor )
264  :( T(0) );
265 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
280 template< typename T >
281 BLAZE_ALWAYS_INLINE constexpr bool lessThan_backend( const T& a, const T& b )
282  noexcept( IsBuiltin<T>::value )
283 {
284  return a < b;
285 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
304 BLAZE_ALWAYS_INLINE constexpr bool lessThan_backend( float a, float b ) noexcept
305 {
306  return ( b - a ) > 1E-8F;
307 }
309 //*************************************************************************************************
310 
311 
312 //*************************************************************************************************
326 BLAZE_ALWAYS_INLINE constexpr bool lessThan_backend( double a, double b ) noexcept
327 {
328  return ( b - a ) > 1E-8;
329 }
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
348 BLAZE_ALWAYS_INLINE constexpr bool lessThan_backend( long double a, long double b ) noexcept
349 {
350  return ( b - a ) > 1E-10;
351 }
353 //*************************************************************************************************
354 
355 
356 //*************************************************************************************************
368 template< typename T1, typename T2 >
369 BLAZE_ALWAYS_INLINE constexpr bool lessThan( const T1& a, const T2& b )
370  noexcept( IsBuiltin< CommonType_<T1,T2> >::value )
371 {
372  return lessThan_backend< CommonType_<T1,T2> >( a, b );
373 }
374 //*************************************************************************************************
375 
376 } // namespace blaze
377 
378 #endif
Header file for basic type definitions.
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:1669
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
constexpr int sign(T a) noexcept
Sign function.
Definition: Functions.h:111
Compile time check for signed data types.This type trait tests whether or not the given template para...
Definition: IsSigned.h:77
#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:141
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
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.
BLAZE_ALWAYS_INLINE constexpr T nextMultiple(T value, T factor) noexcept
Rounds up an integral value to the next multiple of a given factor.
Definition: Functions.h:258
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.