imate
C++/CUDA Reference
Loading...
Searching...
No Matches
_cu_abs.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3 * SPDX-License-Identifier: BSD-3-Clause
4 * SPDX-FileType: SOURCE
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the license found in the LICENSE.txt file in the root
8 * directory of this source tree.
9 */
10
11#ifndef _CU_ARITHMETICS_CU_ABS_H_
12#define _CU_ARITHMETICS_CU_ABS_H_
13
14// =======
15// Headers
16// =======
17
18#include "../_cu_definitions/cu_types.h" // __nv_fp8_e5m2, __nv_fp8_e4m3,
19 // __half, __nv_bfloat16, __habs
20
21#include <cmath> // std::abs
22#include <cassert> // assert
23
24
25// =============
26// cu arithmetic
27// =============
28
41
43{
44 // ===
45 // abs
46 // ===
47
56
57 template <typename DataType>
58 inline __host__ __device__ DataType abs(const DataType x);
59
60
61 // ===
62 // abs (__nv_fp8_e5m2)
63 // ===
64
74
75 #if defined(USE_CUDA_FP8_E5M2) && (USE_CUDA_FP8_E5M2 == 1)
76 template<>
77 inline __host__ __device__ __nv_fp8_e5m2 abs<__nv_fp8_e5m2>(
78 const __nv_fp8_e5m2 x)
79 {
80 // Not implemented
81 assert(false);
82
83 return __nv_fp8_e5m2(NAN);
84 }
85 #endif
86
87
88 // ===
89 // abs (__nv_fp8_e4m3)
90 // ===
91
101
102 #if defined(USE_CUDA_FP8_E4M3) && (USE_CUDA_FP8_E4M3 == 1)
103 template<>
105 const __nv_fp8_e4m3 x)
106 {
107 // Not implemented
108 assert(false);
109
110 return __nv_fp8_e4m3(NAN);
111 }
112 #endif
113
114
115 // ===
116 // abs (__half)
117 // ===
118
127
128 #if defined(USE_CUDA_FP16) && (USE_CUDA_FP16 == 1)
129 template<>
131 {
132 return __habs(x);
133 }
134 #endif
135
136
137 // ===
138 // abs (__nv_bfloat16)
139 // ===
140
150
151 #if defined(USE_CUDA_BF16) && (USE_CUDA_BF16 == 1)
152 template<>
154 const __nv_bfloat16 x)
155 {
156 return __habs(x);
157 }
158 #endif
159
160
161 // ===
162 // abs (float)
163 // ===
164
173
174 #if defined(USE_CUDA_FP32) && (USE_CUDA_FP32 == 1)
175 template<>
176 inline __host__ __device__ float abs<float>(const float x)
177 {
178 return std::abs(x);
179 }
180 #endif
181
182
183 // ===
184 // abs (double)
185 // ===
186
195
196 #if defined(USE_CUDA_FP64) && (USE_CUDA_FP64 == 1)
197 template<>
198 inline __host__ __device__ double abs<double>(const double x)
199 {
200 return std::abs(x);
201 }
202 #endif
203
204} // namespace cu_arithmetics
205
206#endif // _CU_ARITHMETICS_CU_ABS_H_
Cast from float to __half and __nv_bfloat16 types and vice-versa, and float to double and vice-versa.
Definition _cu_abs.h:43
__host__ __device__ float abs< float >(const float x)
Absolute value of a floating point number in __nv_fp8_e5m2 type.
Definition _cu_abs.h:176
__host__ __device__ DataType abs(const DataType x)
Absolute value of a floating point number.
__host__ __device__ double abs< double >(const double x)
Absolute value of a floating point number in double type.
Definition _cu_abs.h:198