summaryrefslogtreecommitdiffstats
path: root/Tests/HIP/MathFunctions/main.hip
blob: dae89fc3cf7738c56c67d7cd16a13527c294795a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <stdexcept>
#include <cmath>
#include <math.h>
#include <memory>

#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>

namespace {
template<class T, class F>
__global__ void global_entry_point(F f, T *out) {
  *out = f();
}

template <class T, class F>
bool verify(F f, T expected)
{
  std::unique_ptr<T> cpu_T(new T);
  T* gpu_T = nullptr;
  if (hipMalloc((void**)&gpu_T, sizeof(T)) != hipSuccess) {
    return false;
  }
  bool result = true;
  hipLaunchKernelGGL(global_entry_point, 1, 1, 0, 0, f, gpu_T);
  result = hipMemcpy(cpu_T.get(), gpu_T, sizeof(T), hipMemcpyDeviceToHost) == hipSuccess && result;
  result = hipFree(gpu_T) == hipSuccess && result;
  result = *cpu_T == expected && result;
  return result;
}
}

int main(int argc, char** argv)
{
  bool valid = verify([]__device__(){ return std::round(1.4f); }, 1.0f);
  valid &= verify([]__device__(){ return max<_Float16>(1.0f, 2.0f); }, 2.0f);
  valid &= verify([]__device__(){ return min<_Float16>(1.0f, 2.0f); }, 1.0f);

  if (valid) {
    return 0;
  } else {
    return 1;
  }
}