! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Test CUDA Fortran procedures available in cudadevice module

attributes(global) subroutine test_sad()
  integer :: res
  integer :: i, j, k
  res = __sad(i, j, k)
end subroutine

! CHECK-LABEL: _QPtest_sad
! CHECK: %{{.*}} = fir.call @__nv_sad(%{{.*}}, %{{.*}}, %{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32, i32, i32) -> i32

attributes(global) subroutine test_usad()
  integer :: res
  integer :: i, j, k
  res = __usad(i, j, k)
end subroutine

! CHECK-LABEL: _QPtest_usad
! CHECK: %{{.*}} = fir.call @__nv_usad(%{{.*}}, %{{.*}}, %{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32, i32, i32) -> i32

attributes(global) subroutine test_dsqrt_rX()
  double precision :: res
  double precision :: p
  res = __dsqrt_rd(p)
  res = __dsqrt_rn(p)
  res = __dsqrt_ru(p)
  res = __dsqrt_rz(p)
end subroutine

! CHECK-LABEL: _QPtest_dsqrt_rx
! CHECK: %{{.*}} = fir.call @__nv_dsqrt_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_dsqrt_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_dsqrt_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_dsqrt_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64

attributes(global) subroutine test_uint2float_rX()
  real :: res
  integer :: i
  res = __uint2float_rd(i)
  res = __uint2float_rn(i)
  res = __uint2float_ru(i)
  res = __uint2float_rz(i)
end subroutine

! CHECK-LABEL: _QPtest_uint2float_rx
! CHECK: %{{.*}} = fir.call @__nv_uint2float_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_uint2float_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_uint2float_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_uint2float_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32

attributes(global) subroutine test_uint2double_rn()
  double precision :: res
  integer :: i
  res = __uint2double_rn(i)
end subroutine

! CHECK-LABEL: _QPtest_uint2double_rn
! CHECK: %{{.*}} = fir.call @__nv_uint2double_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f64

attributes(global) subroutine test_ull2dloat_rX()
  real :: res
  integer(8) :: i
  res = __ull2float_rd(i)
  res = __ull2float_rn(i)
  res = __ull2float_ru(i)
  res = __ull2float_rz(i)
end subroutine

! CHECK-LABEL: _QPtest_ull2dloat_rx
! CHECK: %{{.*}} = fir.call @__nv_ull2float_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ull2float_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ull2float_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ull2float_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32

attributes(global) subroutine test_log()
  real :: res
  real :: r
  res = __logf(r)
  res = __log2f(r)
  res = __log10f(r)
end subroutine

! CHECK-LABEL: _QPtest_log
! CHECK: %{{.*}} = fir.call @__nv_fast_logf(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_fast_log2f(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_fast_log10f(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32

attributes(global) subroutine test_sincosf()
  real :: r, s, c
  call __sincosf(r, s, c)
end subroutine

! CHECK-LABEL: _QPtest_sincosf
! CHECK: fir.call @__nv_fast_sincosf(%{{.*}}, %{{.*}}#0, %{{.*}}#0) proc_attrs<bind_c> fastmath<contract> : (f32, !fir.ref<f32>, !fir.ref<f32>) -> () 

attributes(global) subroutine test_sinf()
  real :: res
  real :: r
  res = __sinf(r)
end subroutine

! CHECK-LABEL: _QPtest_sinf
! CHECK: %{{.*}} = fir.call @__nv_fast_sinf(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32

attributes(global) subroutine test_tanf()
  real :: res
  real :: r
  res = __tanf(r)
end subroutine

! CHECK-LABEL: _QPtest_tanf
! CHECK: %{{.*}} = fir.call @__nv_fast_tanf(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32

attributes(global) subroutine test_exp()
  real :: res
  real :: r
  res = __expf(r)
  res = __exp10f(r)
end subroutine

! CHECK-LABEL: _QPtest_exp
! CHECK: %{{.*}} = fir.call @__nv_fast_expf(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_fast_exp10f(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32

attributes(global) subroutine test_double2ll_rX()
  integer(8) :: res
  double precision :: r
  res = __double2ll_rd(r)
  res = __double2ll_rn(r)
  res = __double2ll_ru(r)
  res = __double2ll_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_double2ll_rx
! CHECK: %{{.*}} = fir.call @__nv_double2ll_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ll_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ll_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ll_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64

attributes(global) subroutine test_drcp_rX()
  double precision :: res
  double precision :: r
  res = __drcp_rd(r)
  res = __drcp_rn(r)
  res = __drcp_ru(r)
  res = __drcp_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_drcp_rx
! CHECK: %{{.*}} = fir.call @__nv_drcp_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_drcp_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_drcp_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64
! CHECK: %{{.*}} = fir.call @__nv_drcp_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> f64

attributes(global) subroutine test_double2ull_rX()
  integer(8) :: res
  double precision :: r
  res = __double2ull_rd(r)
  res = __double2ull_rn(r)
  res = __double2ull_ru(r)
  res = __double2ull_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_double2ull_rx
! CHECK: %{{.*}} = fir.call @__nv_double2ull_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ull_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ull_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64
! CHECK: %{{.*}} = fir.call @__nv_double2ull_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64

attributes(global) subroutine test_saturatef()
  real :: res
  real :: r
  res = __saturatef(r)
end subroutine

! CHECK-LABEL: _QPtest_saturatef
! CHECK: %{{.*}} = fir.call @__nv_saturatef(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> f32

attributes(global) subroutine test_float2ll_rX()
  integer(8) :: res
  real :: r
  res = __float2ll_rd(r)
  res = __float2ll_rn(r)
  res = __float2ll_ru(r)
  res = __float2ll_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_float2ll_rx
! CHECK: %{{.*}} = fir.call @__nv_float2ll_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i64
! CHECK: %{{.*}} = fir.call @__nv_float2ll_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i64
! CHECK: %{{.*}} = fir.call @__nv_float2ll_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i64
! CHECK: %{{.*}} = fir.call @__nv_float2ll_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i64

attributes(global) subroutine test_ll2float_rX()
  real :: res
  integer(8) :: i
  res = __ll2float_rd(i)
  res = __ll2float_rn(i)
  res = __ll2float_ru(i)
  res = __ll2float_rz(i)
end subroutine

! CHECK-LABEL: _QPtest_ll2float_rx
! CHECK: %{{.*}} = fir.call @__nv_ll2float_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ll2float_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ll2float_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32
! CHECK: %{{.*}} = fir.call @__nv_ll2float_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f32

attributes(global) subroutine test_int2float_rX()
  real :: res
  integer :: i
  res = __int2float_rd(i)
  res = __int2float_rn(i)
  res = __int2float_ru(i)
  res = __int2float_rz(i)
end subroutine

! CHECK-LABEL: _QPtest_int2float_rx
! CHECK: %{{.*}} = fir.call @__nv_int2float_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_int2float_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_int2float_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32
! CHECK: %{{.*}} = fir.call @__nv_int2float_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32

attributes(global) subroutine test_float2int_rX()
  integer :: res
  real :: r
  res = __float2int_rd(r)
  res = __float2int_rn(r)
  res = __float2int_ru(r)
  res = __float2int_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_float2int_rx
! CHECK: %{{.*}} = fir.call @__nv_float2int_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32    
! CHECK: %{{.*}} = fir.call @__nv_float2int_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32
! CHECK: %{{.*}} = fir.call @__nv_float2int_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32
! CHECK: %{{.*}} = fir.call @__nv_float2int_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32

attributes(global) subroutine test_float2uint_rX()
  integer :: res
  real :: r
  res = __float2uint_rd(r)
  res = __float2uint_rn(r)
  res = __float2uint_ru(r)
  res = __float2uint_rz(r)
end subroutine

! CHECK-LABEL: _QPtest_float2uint_rx
! CHECK: %{{.*}} = fir.call @__nv_float2uint_rd(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32
! CHECK: %{{.*}} = fir.call @__nv_float2uint_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32
! CHECK: %{{.*}} = fir.call @__nv_float2uint_ru(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32
! CHECK: %{{.*}} = fir.call @__nv_float2uint_rz(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32

attributes(global) subroutine test_int2double_rn()
  double precision :: res
  integer :: r
  res = __int2double_rn(r)
end subroutine

! CHECK-LABEL: _QPtest_int2double_rn
! CHECK: %{{.*}} = fir.call @__nv_int2double_rn(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f64

attributes(global) subroutine test_fdividef()
  real :: res
  real :: r
  res = __fdividef(r, r)
end subroutine

! CHECK-LABEL: _QPtest_fdividef
! CHECK: %{{.*}} = fir.call @__nv_fast_fdividef(%{{.*}}, %{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32, f32) -> f32

attributes(global) subroutine test_double_as_longlong()
  integer(8) :: res
  real(8) :: r
  res = double_as_longlong(r)
end subroutine

! CHECK-LABEL: _QPtest_double_as_longlong
! CHECK: %{{.*}} = fir.call @__nv_double_as_longlong(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i64

attributes(global) subroutine test_longlong_as_double()
  integer(8) :: i
  real(8) :: res
  res = longlong_as_double(i)
end subroutine

! CHECK-LABEL: _QPtest_longlong_as_double
! CHECK: %{{.*}} = fir.call @__nv_longlong_as_double(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i64) -> f64

attributes(global) subroutine test_int_as_float()
  integer :: i
  real :: res
  res = int_as_float(i)
end subroutine

! CHECK-LABEL: _QPtest_int_as_float
! CHECK: %{{.*}} = fir.call @__nv_int_as_float(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32) -> f32

attributes(global) subroutine test_float_as_int()
  integer :: res
  real :: r
  res = float_as_int(r)
end subroutine

! CHECK-LABEL: _QPtest_float_as_int
! CHECK: %{{.*}} = fir.call @__nv_float_as_int(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f32) -> i32

attributes(global) subroutine test_double2loint()
  integer :: res
  double precision :: r
  res = __double2loint(r)
end subroutine

! CHECK-LABEL: _QPtest_double2loint
! CHECK: %{{.*}} = fir.call @__nv_double2loint(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i32

attributes(global) subroutine test_double2hiint()
  integer :: res
  double precision :: r
  res = __double2hiint(r)
end subroutine
  
! CHECK-LABEL: _QPtest_double2hiint
! CHECK: %{{.*}} = fir.call @__nv_double2hiint(%{{.*}}) proc_attrs<bind_c> fastmath<contract> : (f64) -> i32

attributes(global) subroutine test_hiloint2double()
  double precision :: res
  integer :: i, j
  res = __hiloint2double(i, j)
end subroutine

! CHECK-LABEL: _QPtest_hiloint2double
! CHECK: %{{.*}} = fir.call @__nv_hiloint2double(%{{.*}}, %{{.*}}) proc_attrs<bind_c> fastmath<contract> : (i32, i32) -> f64
