ftz shaders 0.0.1
HLSL 2021 interface
Loading...
Searching...
No Matches
ftz32.h
Go to the documentation of this file.
1#pragma once
2#include "ftz/config.h"
3#include "ftz/ftz32_ops.h"
4
5// HLSL 2021 has no user constructors or conversion operators. Use the named
6// imports/exports; a raw HLSL aggregate cast does not normalize FP32 words.
7// The field is public because HLSL has no access control. Treat it as storage.
8namespace ftz {
20 struct ftz32 {
21 unsigned int bits_;
22
25 static ftz32 from_bits(unsigned int bits) {
26 ftz32 result;
27 result.bits_ = detail::ftz32_canonical(bits);
28 return result;
29 }
30
32 static ftz32 from_float(float value) { return from_bits(asuint(value)); }
33 // Caller guarantees a canonical word. This performs no normalization.
36 static ftz32 unsafe_from_float32(float value) {
37 ftz32 result;
38 result.bits_ = asuint(value);
39 return result;
40 }
41
43 static ftz32 zero() { return from_bits(0u); }
46 unsigned int to_bits() { return bits_; }
49 float to_float() { return asfloat(bits_); }
50
53 ftz32 result;
54 result.bits_ = detail::ftz32_add(bits_, rhs.bits_);
55 return result;
56 }
57
59 ftz32 operator+(float rhs) {
60 ftz32 result;
61 result.bits_ = detail::ftz32_add(bits_, detail::ftz32_canonical(asuint(rhs)));
62 return result;
63 }
64
67 ftz32 result;
68 result.bits_ = detail::ftz32_sub(bits_, rhs.bits_);
69 return result;
70 }
71
73 ftz32 operator-(float rhs) {
74 ftz32 result;
75 result.bits_ = detail::ftz32_sub(bits_, detail::ftz32_canonical(asuint(rhs)));
76 return result;
77 }
78
81 ftz32 result;
82 result.bits_ = detail::ftz32_mul(bits_, rhs.bits_);
83 return result;
84 }
85
87 ftz32 operator*(float rhs) {
88 ftz32 result;
89 result.bits_ = detail::ftz32_mul(bits_, detail::ftz32_canonical(asuint(rhs)));
90 return result;
91 }
92
95 ftz32 result;
96 result.bits_ = detail::ftz32_div(bits_, rhs.bits_);
97 return result;
98 }
99
101 ftz32 operator/(float rhs) {
102 ftz32 result;
103 result.bits_ = detail::ftz32_div(bits_, detail::ftz32_canonical(asuint(rhs)));
104 return result;
105 }
106
107 bool operator<(ftz32 rhs) { return detail::ftz32_less(bits_, rhs.bits_); }
109 bool operator>(ftz32 rhs) { return detail::ftz32_less(rhs.bits_, bits_); }
111 bool operator==(ftz32 rhs) { return detail::ftz32_equal(bits_, rhs.bits_); }
113 bool operator!=(ftz32 rhs) { return !detail::ftz32_equal(bits_, rhs.bits_); }
115 bool operator<=(ftz32 rhs) { return detail::ftz32_less(bits_, rhs.bits_) || detail::ftz32_equal(bits_, rhs.bits_); }
117 bool operator>=(ftz32 rhs) { return detail::ftz32_less(rhs.bits_, bits_) || detail::ftz32_equal(bits_, rhs.bits_); }
119 bool operator<(float rhs) {
120 unsigned int word = detail::ftz32_canonical(asuint(rhs));
121 return detail::ftz32_less(bits_, word);
122 }
123
124 bool operator>(float rhs) {
125 unsigned int word = detail::ftz32_canonical(asuint(rhs));
126 return detail::ftz32_less(word, bits_);
127 }
128
129 bool operator==(float rhs) {
130 unsigned int word = detail::ftz32_canonical(asuint(rhs));
131 return detail::ftz32_equal(bits_, word);
132 }
133
134 bool operator!=(float rhs) {
135 unsigned int word = detail::ftz32_canonical(asuint(rhs));
136 return !detail::ftz32_equal(bits_, word);
137 }
138
139 bool operator<=(float rhs) {
140 unsigned int word = detail::ftz32_canonical(asuint(rhs));
141 return detail::ftz32_less(bits_, word) || detail::ftz32_equal(bits_, word);
142 }
143
144 bool operator>=(float rhs) {
145 unsigned int word = detail::ftz32_canonical(asuint(rhs));
146 return detail::ftz32_less(word, bits_) || detail::ftz32_equal(bits_, word);
147 }
148 };
149
150 // Current DXC rejects unary and compound operators on structs. These named
151 // functions preserve the same word graph without converting to builtin float.
154 ftz32 neg(ftz32 value) {
155 ftz32 result;
156 result.bits_ = detail::ftz32_neg(value.bits_);
157 return result;
158 }
159
161 ftz32 abs(ftz32 value) {
162 ftz32 result;
163 result.bits_ = detail::ftz32_abs(value.bits_);
164 return result;
165 }
166
169 ftz32 sqrt(ftz32 value) {
170 ftz32 result;
171 result.bits_ = detail::ftz32_sqrt(value.bits_);
172 return result;
173 }
174 // Rounding canonical inputs produces zero or an integer, never a subnormal.
180 }
181
184 ftz32 ceil(ftz32 value) {
186 }
187
192 }
193
196 ftz32 sin(ftz32 value) {
197 ftz32 result;
198 result.bits_ = detail::ftz32_sin(value.bits_);
199 return result;
200 }
201
204 ftz32 cos(ftz32 value) {
205 ftz32 result;
206 result.bits_ = detail::ftz32_cos(value.bits_);
207 return result;
208 }
209
213 template <unsigned int Degree = 6>
214 ftz32 exp(ftz32 value) {
215 ftz32 result;
216 result.bits_ = detail::ftz32_exp<Degree>(value.bits_);
217 return result;
218 }
219
222 ftz32 result;
223 result.bits_ = detail::ftz32_expm1(value.bits_);
224 return result;
225 }
226
229 ftz32 tanh(ftz32 value) {
230 ftz32 result;
231 result.bits_ = detail::ftz32_tanh(value.bits_);
232 return result;
233 }
234
237 ftz32 log(ftz32 value) {
238 ftz32 result;
239 result.bits_ = detail::ftz32_log(value.bits_);
240 return result;
241 }
242
246 ftz32 result;
247 result.bits_ = detail::ftz32_log1p(value.bits_);
248 return result;
249 }
250
254 ftz32 result;
255 result.bits_ = detail::ftz32_atan2(y.bits_, x.bits_);
256 return result;
257 }
258
259 struct ftz32_sincos_result { ftz32 sine, cosine; };
265 detail::ftz32_sincos_bits words = detail::ftz32_sincos(value.bits_);
266 ftz32_sincos_result result;
267 result.sine.bits_ = words.sine;
268 result.cosine.bits_ = words.cosine;
269 return result;
270 }
271
273 bool isnan(ftz32 value) { return detail::ftz32_isnan(value.bits_); }
276 bool isinf(ftz32 value) { return (value.bits_ & 0x7fffffffu) == detail::ftz32_infinity; }
279 bool isfinite(ftz32 value) { return (value.bits_ & 0x7fffffffu) < detail::ftz32_infinity; }
282 bool signbit(ftz32 value) { return (value.bits_ & detail::ftz32_sign) != 0u; }
286 ftz32 copysign(ftz32 value, ftz32 sign) {
287 return ftz32::from_bits((value.bits_ & 0x7fffffffu) | (sign.bits_ & detail::ftz32_sign));
288 }
289
292 ftz32 result;
293 result.bits_ = detail::ftz32_fma(a.bits_, b.bits_, c.bits_);
294 return result;
295 }
296}
297
307 * \brief HLSL 2021 FTZ value type, conversion factories, operators, and math overloads.
308 */
bool isinf(ftz32 value)
Tests for either signed infinity by its encoded bits.
Definition ftz32.h:276
ftz32 expm1(ftz32 value)
Approximates exp(x)-1 without subtracting one for tiny x; signed zero is preserved.
Definition ftz32.h:221
ftz32 fma(ftz32 a, ftz32 b, ftz32 c)
Computes a*b+c with one fused rounding and the FTZ boundary repair.
Definition ftz32.h:291
static ftz32 from_float(float value)
Imports a float through its bits and normalizes subnormals.
Definition ftz32.h:32
ftz32_sincos_result sincos(ftz32 value)
Returns sine followed by cosine, sharing reduction; C++ uses a pair and HLSL uses named fields.
Definition ftz32.h:264
bool signbit(ftz32 value)
Tests the sign bit, including negative zero and signed NaNs.
Definition ftz32.h:282
bool isnan(ftz32 value)
Tests the encoded NaN class without floating-point arithmetic or quieting a signaling NaN.
Definition ftz32.h:273
ftz32 cos(ftz32 value)
Returns the reproducible cosine approximation in radians; either zero gives one and infinity gives Na...
Definition ftz32.h:204
ftz32 ceil(ftz32 value)
Rounds toward positive infinity, independently of the ambient rounding direction; retains signed zero...
Definition ftz32.h:184
ftz32 log1p(ftz32 value)
Approximates log(1+x); -1 gives negative infinity, x below -1 gives NaN, and signed zero is preserved...
Definition ftz32.h:245
ftz32 sqrt(ftz32 value)
Returns the reciprocal-refined square-root approximation; signed zero and positive infinity are prese...
Definition ftz32.h:169
ftz32 abs(ftz32 value)
Clears the sign bit without floating-point arithmetic.
Definition ftz32.h:161
ftz32 atan2(ftz32 y, ftz32 x)
Returns the angle in radians for (y,x), retaining signed-axis and infinity quadrants; NaN inputs give...
Definition ftz32.h:253
float to_float()
Exports stored bits as an ordinary HLSL float, leaving this value contract.
Definition ftz32.h:49
ftz32 log(ftz32 value)
Returns the natural-log approximation; either zero gives negative infinity, negative nonzero values g...
Definition ftz32.h:237
ftz32 trunc(ftz32 value)
Rounds toward zero, independently of the ambient rounding direction; retains signed zero and infiniti...
Definition ftz32.h:190
static ftz32 zero()
Returns positive zero.
Definition ftz32.h:43
ftz32 tanh(ftz32 value)
Returns the reproducible hyperbolic tangent; signed zero is preserved and infinities give signed one.
Definition ftz32.h:229
ftz32 sin(ftz32 value)
Returns the reproducible sine approximation in radians; signed zero is preserved and infinity gives N...
Definition ftz32.h:196
static ftz32 from_bits(unsigned int bits)
Imports a binary32 word and replaces a subnormal magnitude with signed zero.
Definition ftz32.h:25
ftz32 floor(ftz32 value)
Rounds toward negative infinity, independently of the ambient rounding direction; retains signed zero...
Definition ftz32.h:178
static ftz32 unsafe_from_float32(float value)
Wraps bits unchanged; the caller must supply normal, signed zero, infinity or NaN.
Definition ftz32.h:36
ftz32 neg(ftz32 value)
Flips the sign bit without floating-point arithmetic.
Definition ftz32.h:154
ftz32 copysign(ftz32 value, ftz32 sign)
Copies the second operand's sign to the first operand's magnitude, preserving the magnitude's NaN pay...
Definition ftz32.h:286
ftz32 exp(ftz32 value)
Returns the reproducible exponential; negative infinity gives positive zero and positive infinity is ...
Definition ftz32.h:214
bool isfinite(ftz32 value)
Tests for zero or finite magnitude by its encoded bits.
Definition ftz32.h:279
unsigned int to_bits()
Exports the exact stored word without arithmetic.
Definition ftz32.h:46
Paired HLSL trig values; sine precedes cosine.
Definition ftz32.h:259
Canonical binary32 word wrapper with named HLSL imports and exports. Ordinary aggregate casts/default...
Definition ftz32.h:20
ftz32 operator/(ftz32 rhs)
Uses the selected shader FTZ policy for canonical operands.
Definition ftz32.h:94
ftz32 operator+(ftz32 rhs)
Uses the selected shader FTZ policy for canonical operands.
Definition ftz32.h:52
bool operator==(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:129
bool operator>=(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:144
bool operator<=(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:139
bool operator>(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:109
bool operator==(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:111
bool operator!=(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:113
bool operator<(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:119
ftz32 operator-(ftz32 rhs)
Uses the selected shader FTZ policy for canonical operands.
Definition ftz32.h:66
bool operator>(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:124
ftz32 operator*(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:87
ftz32 operator+(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:59
bool operator>=(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:117
ftz32 operator/(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:101
bool operator<(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:107
ftz32 operator-(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:73
bool operator<=(ftz32 rhs)
Compares canonical values; NaN is unordered and the two zero signs compare equal.
Definition ftz32.h:115
bool operator!=(float rhs)
Imports a raw float operand before the operation.
Definition ftz32.h:134
ftz32 operator*(ftz32 rhs)
Uses the selected shader FTZ policy for canonical operands.
Definition ftz32.h:80