numeric.hpp Source File

numeric.hpp Source File#

Composable Kernel: numeric.hpp Source File
tile/core/numeric/numeric.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
7#include <limits>
8#include <stdint.h>
9
10namespace ck_tile {
11
12// this struct has the information of
13// 1. limit of a certain type, simliar to std::numeric_limits
14// 2. some pre-defined value, zero, one...
15//
16template <typename T>
17struct numeric
18{
19 // minimum finite value, or minimum positive normalized value for float
20 CK_TILE_HOST_DEVICE static constexpr T min() { return std::numeric_limits<T>::min(); }
21
22 // minumum finite value
23 CK_TILE_HOST_DEVICE static constexpr T lowest() { return std::numeric_limits<T>::lowest(); }
24
25 // maximum finite value
26 CK_TILE_HOST_DEVICE static constexpr T max() { return std::numeric_limits<T>::max(); }
27
28 // difference between 1.0 and next value representable by float
29 CK_TILE_HOST_DEVICE static constexpr T epsilon() { return std::numeric_limits<T>::epsilon(); }
30
31 // maximum rounding error
32 CK_TILE_HOST_DEVICE static constexpr T round_error()
33 {
34 return std::numeric_limits<T>::round_error();
35 }
36
37 // positive infinity value
38 CK_TILE_HOST_DEVICE static constexpr T infinity() { return std::numeric_limits<T>::infinity(); }
39
40 // quiet NaN
41 CK_TILE_HOST_DEVICE static constexpr T quiet_NaN()
42 {
43 return std::numeric_limits<T>::quiet_NaN();
44 }
45
46 // signaling NaN
48 {
49 return std::numeric_limits<T>::signaling_NaN();
50 }
51
52 // smallest positive subnormal value
53 CK_TILE_HOST_DEVICE static constexpr T denorm_min()
54 {
55 return std::numeric_limits<T>::denorm_min();
56 }
57
58 CK_TILE_HOST_DEVICE static constexpr T zero() { return static_cast<T>(0); }
59
60 CK_TILE_HOST_DEVICE static constexpr T one() { return static_cast<T>(1); }
61
62#ifndef C_LOG2E
63#define C_LOG2E 1.44269504088896340736 // log2(e)
64#endif
65
66 CK_TILE_HOST_DEVICE static constexpr T log2e()
67 {
68 if constexpr(std::is_same_v<T, float> || std::is_same_v<T, double>)
69 {
70 return static_cast<T>(C_LOG2E);
71 }
72 else
73 {
74 return 0; // TODO: integer?
75 }
76 }
77};
78
79template <typename T>
81{
82 static constexpr int PackedSize = 1;
83};
84
85template <>
86struct numeric_traits<float>
87{
88 static constexpr int exp = 8;
89 static constexpr int mant = 23;
90 static constexpr int bias = 127;
91 static constexpr uint32_t nan_mask = 0x7F800000;
92 static constexpr uint32_t head_mask = 0xFF800000;
93 static constexpr uint32_t mant_mask = 0x7FFFFF;
94 static constexpr uint32_t exp_mask = 0xFF;
95 static constexpr uint32_t abs_mask = 0x7FFFFFFF;
96 static constexpr uint32_t Inf = 0x7F800000;
97 static constexpr uint32_t NegInf = 0xFF800000;
98 static constexpr uint32_t NaN = 0x7F800001;
99 static constexpr uint32_t Neg0 = 0x80000000;
100 static constexpr int PackedSize = 1;
102};
103
104} // namespace ck_tile
105
106#define CK_TILE_ARITHMETIC_USING_FLOAT(attr_, type_) \
107 attr_ bool operator==(const type_& x, const type_& y) \
108 { \
109 return std::abs(static_cast<float>(x) - static_cast<float>(y)) < \
110 static_cast<float>(numeric<type_>::epsilon()); \
111 } \
112 attr_ bool operator!=(const type_& x, const type_& y) { return not operator==(x, y); } \
113 attr_ bool operator<(const type_& x, const type_& y) \
114 { \
115 return static_cast<float>(x) < static_cast<float>(y); \
116 } \
117 attr_ bool operator<=(const type_& x, const type_& y) \
118 { \
119 return static_cast<float>(x) <= static_cast<float>(y); \
120 } \
121 attr_ bool operator>(const type_& x, const type_& y) \
122 { \
123 return static_cast<float>(x) > static_cast<float>(y); \
124 } \
125 attr_ bool operator>=(const type_& x, const type_& y) \
126 { \
127 return static_cast<float>(x) >= static_cast<float>(y); \
128 } \
129 attr_ type_ operator+(const type_& x, const type_& y) \
130 { \
131 return type_(static_cast<float>(x) + static_cast<float>(y)); \
132 } \
133 attr_ type_ operator-(const type_& x) \
134 { \
135 constexpr uint32_t bits = sizeof(type_) * 8; \
136 constexpr uint32_t mask = 1 << (bits - 1); \
137 type_ y = x; \
138 y.data ^= static_cast<typename type_::raw_type>(mask); \
139 return y; \
140 } \
141 attr_ type_ operator-(const type_& x, const type_& y) \
142 { \
143 return type_(static_cast<float>(x) - static_cast<float>(y)); \
144 } \
145 attr_ type_ operator*(const type_& x, const type_& y) \
146 { \
147 return type_(static_cast<float>(x) * static_cast<float>(y)); \
148 } \
149 attr_ type_ operator/(const type_& x, const type_& y) \
150 { \
151 return type_(static_cast<float>(x) / static_cast<float>(y)); \
152 } \
153 attr_ type_& operator+=(type_& x, const type_& y) \
154 { \
155 x = type_(static_cast<float>(x) + static_cast<float>(y)); \
156 return x; \
157 } \
158 attr_ type_& operator-=(type_& x, const type_& y) \
159 { \
160 x = type_(static_cast<float>(x) - static_cast<float>(y)); \
161 return x; \
162 } \
163 attr_ type_& operator*=(type_& x, const type_& y) \
164 { \
165 x = type_(static_cast<float>(x) * static_cast<float>(y)); \
166 return x; \
167 } \
168 attr_ type_& operator/=(type_& x, const type_& y) \
169 { \
170 x = type_(static_cast<float>(x) / static_cast<float>(y)); \
171 return x; \
172 } \
173 attr_ type_& operator++(type_& x) \
174 { \
175 x = type_(static_cast<float>(x) + 1.f); \
176 return x; \
177 } \
178 attr_ type_& operator--(type_& x) \
179 { \
180 x = type_(static_cast<float>(x) - 1.f); \
181 return x; \
182 } \
183 attr_ type_ operator++(type_& x, int) \
184 { \
185 type_ y(x); \
186 x = type_(static_cast<float>(x) + 1.f); \
187 return y; \
188 } \
189 attr_ type_ operator--(type_& x, int) \
190 { \
191 type_ y(x); \
192 x = type_(static_cast<float>(x) - 1.f); \
193 return y; \
194 }
#define CK_TILE_HOST_DEVICE
Definition config.hpp:42
Definition tile/core/algorithm/cluster_descriptor.hpp:13
unsigned int uint32_t
Definition stdint.h:126
static constexpr uint32_t mant_mask
Definition tile/core/numeric/numeric.hpp:93
static constexpr uint32_t nan_mask
Definition tile/core/numeric/numeric.hpp:91
static constexpr uint32_t NaN
Definition tile/core/numeric/numeric.hpp:98
static constexpr uint32_t Inf
Definition tile/core/numeric/numeric.hpp:96
uint32_t bitwise_type
Definition tile/core/numeric/numeric.hpp:101
static constexpr int mant
Definition tile/core/numeric/numeric.hpp:89
static constexpr int bias
Definition tile/core/numeric/numeric.hpp:90
static constexpr uint32_t abs_mask
Definition tile/core/numeric/numeric.hpp:95
static constexpr uint32_t Neg0
Definition tile/core/numeric/numeric.hpp:99
static constexpr int exp
Definition tile/core/numeric/numeric.hpp:88
static constexpr int PackedSize
Definition tile/core/numeric/numeric.hpp:100
static constexpr uint32_t head_mask
Definition tile/core/numeric/numeric.hpp:92
static constexpr uint32_t exp_mask
Definition tile/core/numeric/numeric.hpp:94
static constexpr uint32_t NegInf
Definition tile/core/numeric/numeric.hpp:97
Definition tile/core/numeric/numeric.hpp:81
static constexpr int PackedSize
Definition tile/core/numeric/numeric.hpp:82
Definition tile/core/numeric/numeric.hpp:18
static CK_TILE_HOST_DEVICE constexpr T quiet_NaN()
Definition tile/core/numeric/numeric.hpp:41
static CK_TILE_HOST_DEVICE constexpr T infinity()
Definition tile/core/numeric/numeric.hpp:38
static CK_TILE_HOST_DEVICE constexpr T lowest()
Definition tile/core/numeric/numeric.hpp:23
static CK_TILE_HOST_DEVICE constexpr T one()
Definition tile/core/numeric/numeric.hpp:60
static CK_TILE_HOST_DEVICE constexpr T zero()
Definition tile/core/numeric/numeric.hpp:58
static CK_TILE_HOST_DEVICE constexpr T denorm_min()
Definition tile/core/numeric/numeric.hpp:53
static CK_TILE_HOST_DEVICE constexpr T round_error()
Definition tile/core/numeric/numeric.hpp:32
static CK_TILE_HOST_DEVICE constexpr T signaling_NaN()
Definition tile/core/numeric/numeric.hpp:47
static CK_TILE_HOST_DEVICE constexpr T max()
Definition tile/core/numeric/numeric.hpp:26
static CK_TILE_HOST_DEVICE constexpr T min()
Definition tile/core/numeric/numeric.hpp:20
static CK_TILE_HOST_DEVICE constexpr T epsilon()
Definition tile/core/numeric/numeric.hpp:29
static CK_TILE_HOST_DEVICE constexpr T log2e()
Definition tile/core/numeric/numeric.hpp:66
#define C_LOG2E
Definition tile/core/numeric/math.hpp:469