imate
C++/CUDA Reference
Loading...
Searching...
No Matches
_cu_is_equal.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_IS_EQUAL_H_
12#define _CU_ARITHMETICS_CU_IS_EQUAL_H_
13
14
15// =======
16// Headers
17// =======
18
19#include <cmath> // std::fabs
20#include <limits> // epsilon
21#include "../_cu_definitions/cu_types.h" // __nv_fp8_e5m2, __nv_fp8_e4m3,
22 // __half, __nv_bfloat16, __heq
23#include <cassert> // assert
24
25
26// ==============
27// cu arithmetics
28// ==============
29
37
38namespace cu_arithmetics
39{
40
41 // ========
42 // is equal
43 // ========
44
53
54 template <typename DataType>
55 inline bool is_equal(DataType x, DataType y);
56
57
58 // ========
59 // is equal (__nv_fp8_e5m2)
60 // ========
61
70
71 #if defined(USE_CUDA_FP8_E5M2) && (USE_CUDA_FP8_E5M2 == 1)
72 template<>
74 {
75 // Not implemented
76 assert(false);
77
78 return false;
79 }
80 #endif
81
82
83 // ========
84 // is equal (__nv_fp8_e4m3)
85 // ========
86
95
96 #if defined(USE_CUDA_FP8_E4M3) && (USE_CUDA_FP8_E4M3 == 1)
97 template<>
99 {
100 // Not implemented
101 assert(false);
102
103 return false;
104 }
105 #endif
106
107
108 // ========
109 // is equal (__half)
110 // ========
111
120
121 #if defined(USE_CUDA_FP16) && (USE_CUDA_FP16 == 1)
122 template<>
123 inline bool is_equal(__half x, __half y)
124 {
125 return __heq(x, y);
126 }
127 #endif
128
129
130 // ========
131 // is equal (__nv_bfloat16)
132 // ========
133
142
143 #if defined(USE_CUDA_BF16) && (USE_CUDA_BF16 == 1)
144 template<>
146 {
147 return __heq(x, y);
148 }
149 #endif
150
151
152 // ========
153 // is equal (float)
154 // ========
155
164
165 #if defined(USE_CUDA_FP32) && (USE_CUDA_FP32 == 1)
166 template<>
167 inline bool is_equal(float x, float y)
168 {
169 if (std::fabs(x - y) <= 2.0 * std::numeric_limits<float>::epsilon())
170 {
171 return true;
172 }
173 else
174 {
175 return false;
176 }
177 }
178 #endif
179
180
181 // ========
182 // is equal (double)
183 // ========
184
193
194 #if defined(USE_CUDA_FP64) && (USE_CUDA_FP64 == 1)
195 template<>
196 inline bool is_equal(double x, double y)
197 {
198 if (std::fabs(x - y) <= 2.0 * std::numeric_limits<double>::epsilon())
199 {
200 return true;
201 }
202 else
203 {
204 return false;
205 }
206 }
207 #endif
208
209} // namespace cu_arithmetics
210
211#endif // _CU_ARITHMETICS_CU_IS_EQUAL_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__ DataType abs(const DataType x)
Absolute value of a floating point number.
bool is_equal(DataType x, DataType y)
Check if two floating point numbers are equal within a tolerance.