CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers
relatively_equal.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  **************************************************************************************************/
25 /* \file
26  \brief Performs comparison between two elements with support for floating-point comparisons.
27 */
28 
29 #pragma once
30 
31 #include "numeric_types.h"
32 
33 namespace cutlass {
34 
36 
37 template <typename T>
39 bool relatively_equal(T a, T b, T epsilon, T nonzero_floor);
40 
42 
43 namespace detail {
44 
45 // This floating-point comparison function implements the method described in
46 //
47 // https://floating-point-gui.de/errors/comparison/
48 //
49 template <typename T>
51 bool relatively_equal_float(T a, T b, T epsilon, T nonzero_floor) {
52 
53  using std::abs;
54 
55  T abs_A = abs(a);
56  T abs_B = abs(b);
57  T diff = abs(a - b);
58  T zero = T(0);
59 
60  if (a == b) {
61  return true;
62  }
63  else if (a == zero || b == zero || diff < nonzero_floor) {
64  return diff < epsilon * nonzero_floor;
65  }
66 
67  return diff < epsilon * (abs_A + abs_B);
68 }
69 
70 } // namespace detail
71 
73 
74 template <>
77  return (a == b);
78 }
79 
80 template <>
83  return (a == b);
84 }
85 
86 template <>
89  return (a == b);
90 }
91 
92 template <>
94 bool relatively_equal<int8_t>(int8_t a, int8_t b, int8_t, int8_t) {
95  return (a == b);
96 }
97 
98 template <>
100 bool relatively_equal<uint8_t>(uint8_t a, uint8_t b, uint8_t, uint8_t) {
101  return (a == b);
102 }
103 
104 template <>
106 bool relatively_equal<int16_t>(int16_t a, int16_t b, int16_t, int16_t) {
107  return (a == b);
108 }
109 
110 template <>
112 bool relatively_equal<uint16_t>(uint16_t a, uint16_t b, uint16_t, uint16_t) {
113  return (a == b);
114 }
115 
116 template <>
118 bool relatively_equal<int32_t>(int32_t a, int32_t b, int32_t, int32_t) {
119  return (a == b);
120 }
121 
122 template <>
124 bool relatively_equal<uint32_t>(uint32_t a, uint32_t b, uint32_t, uint32_t) {
125  return (a == b);
126 }
127 
128 template <>
130 bool relatively_equal<int64_t>(int64_t a, int64_t b, int64_t, int64_t) {
131  return (a == b);
132 }
133 
134 template <>
136 bool relatively_equal<uint64_t>(uint64_t a, uint64_t b, uint64_t, uint64_t) {
137  return (a == b);
138 }
139 
141 
142 template <>
144 bool relatively_equal<half_t>(half_t a, half_t b, half_t epsilon, half_t nonzero_floor) {
145  return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
146 }
147 
148 template <>
150 bool relatively_equal<float>(float a, float b, float epsilon, float nonzero_floor) {
151  return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
152 }
153 
154 
155 template <>
157 bool relatively_equal<double>(double a, double b, double epsilon, double nonzero_floor) {
158  return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
159 }
160 
162 
163 } // namespace cutlass
CUTLASS_HOST_DEVICE bool relatively_equal< int8_t >(int8_t a, int8_t b, int8_t, int8_t)
Definition: relatively_equal.h:94
CUTLASS_HOST_DEVICE bool relatively_equal< double >(double a, double b, double epsilon, double nonzero_floor)
Definition: relatively_equal.h:157
integer_subbyte< 4, false > uint4b_t
4-bit Unsigned integer type
Definition: integer_subbyte.h:158
Definition: aligned_buffer.h:35
CUTLASS_HOST_DEVICE T abs(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:313
integer_subbyte< 1, false > uint1b_t
1-bit Unsigned integer type
Definition: integer_subbyte.h:152
4-bit signed integer type
Definition: integer_subbyte.h:42
CUTLASS_HOST_DEVICE bool relatively_equal(T a, T b, T epsilon, T nonzero_floor)
IEEE half-precision floating-point type.
Definition: half.h:126
CUTLASS_HOST_DEVICE bool relatively_equal< uint8_t >(uint8_t a, uint8_t b, uint8_t, uint8_t)
Definition: relatively_equal.h:100
CUTLASS_HOST_DEVICE bool relatively_equal< int32_t >(int32_t a, int32_t b, int32_t, int32_t)
Definition: relatively_equal.h:118
CUTLASS_HOST_DEVICE bool relatively_equal< uint16_t >(uint16_t a, uint16_t b, uint16_t, uint16_t)
Definition: relatively_equal.h:112
CUTLASS_HOST_DEVICE bool relatively_equal< int4b_t >(int4b_t a, int4b_t b, int4b_t, int4b_t)
Definition: relatively_equal.h:82
CUTLASS_HOST_DEVICE bool relatively_equal< int16_t >(int16_t a, int16_t b, int16_t, int16_t)
Definition: relatively_equal.h:106
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
Top-level include for all CUTLASS numeric types.
CUTLASS_HOST_DEVICE bool relatively_equal_float(T a, T b, T epsilon, T nonzero_floor)
Definition: relatively_equal.h:51
CUTLASS_HOST_DEVICE bool relatively_equal< uint32_t >(uint32_t a, uint32_t b, uint32_t, uint32_t)
Definition: relatively_equal.h:124
CUTLASS_HOST_DEVICE bool relatively_equal< int64_t >(int64_t a, int64_t b, int64_t, int64_t)
Definition: relatively_equal.h:130
CUTLASS_HOST_DEVICE bool relatively_equal< float >(float a, float b, float epsilon, float nonzero_floor)
Definition: relatively_equal.h:150
CUTLASS_HOST_DEVICE bool relatively_equal< uint64_t >(uint64_t a, uint64_t b, uint64_t, uint64_t)
Definition: relatively_equal.h:136
CUTLASS_HOST_DEVICE bool relatively_equal< uint4b_t >(uint4b_t a, uint4b_t b, uint4b_t, uint4b_t)
Definition: relatively_equal.h:88
CUTLASS_HOST_DEVICE bool relatively_equal< uint1b_t >(uint1b_t a, uint1b_t b, uint1b_t, uint1b_t)
Definition: relatively_equal.h:76
integer_subbyte< 4, true > int4b_t
4-bit Integer type
Definition: integer_subbyte.h:155
CUTLASS_HOST_DEVICE bool relatively_equal< half_t >(half_t a, half_t b, half_t epsilon, half_t nonzero_floor)
Definition: relatively_equal.h:144