THCNumerics.cuh 1.25 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
#ifndef THC_NUMERICS_INC
#define THC_NUMERICS_INC

rusty1s's avatar
rusty1s committed
4
#include <THC/THCHalf.h>
rusty1s's avatar
rusty1s committed
5
6
7
8
9
10
11
12

#ifdef CUDA_HALF_TENSOR
#ifdef __CUDA_ARCH__
#define h2f(A) __half2float(A)
#define f2h(A) __float2half(A)
#else  // CUDA_ARCH__
#define h2f(A) THC_half2float(A)
#define f2h(A) THC_float2half(A)
rusty1s's avatar
rusty1s committed
13
14
15
16
17
18
#endif  // CUDA_ARCH__
#endif  // CUDA_HALF_TENSOR

template<typename T>
struct THCNumerics {
  static inline __host__ __device__ T div(T a, T b) { return a / b; }
rusty1s's avatar
rusty1s committed
19
  static inline __host__ __device__ bool gte(T a, T b) { return a >= b; }
rusty1s's avatar
rusty1s committed
20
21
22
};

#ifdef CUDA_HALF_TENSOR
rusty1s's avatar
rusty1s committed
23
24
25
template<>
struct THCNumerics<half> {
  static inline __host__ __device__ half div(half a, half b) { return f2h(h2f(a) / h2f(b)); }
rusty1s's avatar
rusty1s committed
26
  static inline __host__ __device__ bool gte(half a, half b) { return h2f(a) >= h2f(b); }
rusty1s's avatar
rusty1s committed
27
28
29
};
#endif  // CUDA_HALF_TENSOR

rusty1s's avatar
rusty1s committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
template <typename In, typename Out>
struct ScalarConvert {
  static __host__ __device__ Out to(const In v) { return (Out) v; }
};

#ifdef CUDA_HALF_TENSOR
template <typename Out>
struct ScalarConvert<half, Out> {
  static __host__ __device__ Out to(const half v) { return (Out) h2f(v); }
};

template <typename In>
struct ScalarConvert<In, half> {
  static __host__ __device__ half to(const In v) { return f2h((float) v); }
};
#endif  // CUDA_HALF_TENSOR

rusty1s's avatar
rusty1s committed
47
#endif  // THC_NUMERICS_INC