QUDA  v1.1.0
A library for QCD on GPUs
convert.h
Go to the documentation of this file.
1 #pragma once
2 
11 #include <type_traits>
12 #include <quda_internal.h> // for maximum short, char traits.
13 #include <register_traits.h>
14 
15 namespace quda
16 {
17 
18  template <typename T> __host__ __device__ inline float i2f(T a)
19  {
20 #if 1
21  return static_cast<float>(a);
22 #else
23  // will work for up to 23-bit int
24  union {
25  int32_t i;
26  float f;
27  };
28  i = a + 0x4B400000;
29  return f - 12582912.0f;
30 #endif
31  }
32 
33  // Fast float to integer round
34  __device__ __host__ inline int f2i(float f)
35  {
36 #ifdef __CUDA_ARCH__
37  f += 12582912.0f;
38  return reinterpret_cast<int &>(f);
39 #else
40  return static_cast<int>(f);
41 #endif
42  }
43 
44  // Fast double to integer round
45  __device__ __host__ inline int d2i(double d)
46  {
47 #ifdef __CUDA_ARCH__
48  d += 6755399441055744.0;
49  return reinterpret_cast<int &>(d);
50 #else
51  return static_cast<int>(d);
52 #endif
53  }
54 
62  template <typename T1, typename T2>
63  __host__ __device__ inline typename std::enable_if<!isFixed<T1>::value && !isFixed<T2>::value, void>::type
64  copy(T1 &a, const T2 &b)
65  {
66  a = b;
67  }
68 
69  template <typename T1, typename T2>
70  __host__ __device__ inline typename std::enable_if<!isFixed<T1>::value && isFixed<T2>::value, void>::type
71  copy(T1 &a, const T2 &b)
72  {
74  }
75 
76  template <typename T1, typename T2>
77  __host__ __device__ inline typename std::enable_if<isFixed<T1>::value && !isFixed<T2>::value, void>::type
78  copy(T1 &a, const T2 &b)
79  {
81  }
82 
87  template <typename T1, typename T2>
88  __host__ __device__ inline typename std::enable_if<!isFixed<T1>::value, void>::type copy_scaled(T1 &a, const T2 &b)
89  {
90  copy(a, b);
91  }
92 
93  template <typename T1, typename T2>
94  __host__ __device__ inline typename std::enable_if<isFixed<T1>::value, void>::type copy_scaled(T1 &a, const T2 &b)
95  {
96  a = f2i(b);
97  }
98 
104  template <typename T1, typename T2, typename T3>
105  __host__ __device__ inline typename std::enable_if<!isFixed<T2>::value, void>::type copy_and_scale(T1 &a, const T2 &b,
106  const T3 &c)
107  {
108  copy(a, b);
109  }
110 
111  template <typename T1, typename T2, typename T3>
112  __host__ __device__ inline typename std::enable_if<isFixed<T2>::value, void>::type copy_and_scale(T1 &a, const T2 &b,
113  const T3 &c)
114  {
115  a = i2f(b) * fixedInvMaxValue<T2>::value * c;
116  }
117 
118 } // namespace quda
__host__ __device__ float i2f(T a)
Definition: convert.h:18
__device__ __host__ int f2i(float f)
Definition: convert.h:34
__device__ __host__ int d2i(double d)
Definition: convert.h:45
__host__ __device__ std::enable_if<!isFixed< T1 >::value, void >::type copy_scaled(T1 &a, const T2 &b)
Specialized variants of the copy function that assumes the scaling factor has already been done.
Definition: convert.h:88
__host__ __device__ std::enable_if<!isFixed< T1 >::value &&!isFixed< T2 >::value, void >::type copy(T1 &a, const T2 &b)
Copy function which is trival between floating point types. When converting to an integer type,...
Definition: convert.h:64
__host__ __device__ std::enable_if<!isFixed< T2 >::value, void >::type copy_and_scale(T1 &a, const T2 &b, const T3 &c)
Specialized variants of the copy function that include an additional scale factor....
Definition: convert.h:105
Provides precision abstractions and defines the register precision given the storage precision using ...
static const bool value