Skip to content

Commit

Permalink
Re-organize numeric constants to be consistent (gpuweb#3025)
Browse files Browse the repository at this point in the history
Moves `subnormals` and `infinity` entries down into appropriate
`positive` and `negative` locations.

e.g. kValue.f32.subnormal.positive.max becomes
kValue.f32.positive.subnormal.max

Co-authored-by: jzm-intel <[email protected]>
  • Loading branch information
zoddicus and jzm-intel authored Sep 28, 2023
1 parent e61392d commit 6442db8
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 493 deletions.
20 changes: 10 additions & 10 deletions src/unittests/conversion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,15 @@ g.test('pack2x16float')

// f32 subnormals
// prettier-ignore
{ inputs: [kValue.f32.subnormal.positive.max, 1], result: [0x3c000000, 0x3c008000, 0x3c000001] },
{ inputs: [kValue.f32.positive.subnormal.max, 1], result: [0x3c000000, 0x3c008000, 0x3c000001] },
// prettier-ignore
{ inputs: [kValue.f32.subnormal.negative.min, 1], result: [0x3c008001, 0x3c000000, 0x3c008000] },
{ inputs: [kValue.f32.negative.subnormal.min, 1], result: [0x3c008001, 0x3c000000, 0x3c008000] },

// f16 subnormals
// prettier-ignore
{ inputs: [kValue.f16.subnormal.positive.max, 1], result: [0x3c0003ff, 0x3c000000, 0x3c008000] },
{ inputs: [kValue.f16.positive.subnormal.max, 1], result: [0x3c0003ff, 0x3c000000, 0x3c008000] },
// prettier-ignore
{ inputs: [kValue.f16.subnormal.negative.min, 1], result: [0x03c0083ff, 0x3c000000, 0x3c008000] },
{ inputs: [kValue.f16.negative.subnormal.min, 1], result: [0x03c0083ff, 0x3c000000, 0x3c008000] },

// f16 out of bounds
{ inputs: [kValue.f16.positive.max + 1, 1], result: [undefined] },
Expand Down Expand Up @@ -481,8 +481,8 @@ g.test('pack2x16snorm')
{ inputs: [-0.1, -0.5], result: 0xc001f333 },

// Subnormals
{ inputs: [kValue.f32.subnormal.positive.max, 1], result: 0x7fff0000 },
{ inputs: [kValue.f32.subnormal.negative.min, 1], result: 0x7fff0000 },
{ inputs: [kValue.f32.positive.subnormal.max, 1], result: 0x7fff0000 },
{ inputs: [kValue.f32.negative.subnormal.min, 1], result: 0x7fff0000 },
] as const)
.fn(test => {
const inputs = test.params.inputs;
Expand All @@ -506,7 +506,7 @@ g.test('pack2x16unorm')
{ inputs: [10, 10], result: 0xffffffff },

// Subnormals
{ inputs: [kValue.f32.subnormal.positive.max, 1], result: 0xffff0000 },
{ inputs: [kValue.f32.positive.subnormal.max, 1], result: 0xffff0000 },
] as const)
.fn(test => {
const inputs = test.params.inputs;
Expand Down Expand Up @@ -542,8 +542,8 @@ g.test('pack4x8snorm')
{ inputs: [-0.1, -0.5, -0.1, -0.5], result: 0xc1f3c1f3 },

// Subnormals
{ inputs: [kValue.f32.subnormal.positive.max, 1, 1, 1], result: 0x7f7f7f00 },
{ inputs: [kValue.f32.subnormal.negative.min, 1, 1, 1], result: 0x7f7f7f00 },
{ inputs: [kValue.f32.positive.subnormal.max, 1, 1, 1], result: 0x7f7f7f00 },
{ inputs: [kValue.f32.negative.subnormal.min, 1, 1, 1], result: 0x7f7f7f00 },
] as const)
.fn(test => {
const inputs = test.params.inputs;
Expand All @@ -570,7 +570,7 @@ g.test('pack4x8unorm')
{ inputs: [0.1, 0.5, 0.1, 0.5], result: 0x801a801a },

// Subnormals
{ inputs: [kValue.f32.subnormal.positive.max, 1, 1, 1], result: 0xffffff00 },
{ inputs: [kValue.f32.positive.subnormal.max, 1, 1, 1], result: 0xffffff00 },
] as const)
.fn(test => {
const inputs = test.params.inputs;
Expand Down
130 changes: 65 additions & 65 deletions src/unittests/floating_point.spec.ts

Large diffs are not rendered by default.

430 changes: 215 additions & 215 deletions src/unittests/maths.spec.ts

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions src/unittests/serialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ g.test('value').fn(t => {
f32(-0.5),
f32(kValue.f32.positive.max),
f32(kValue.f32.positive.min),
f32(kValue.f32.subnormal.positive.max),
f32(kValue.f32.subnormal.positive.min),
f32(kValue.f32.subnormal.negative.max),
f32(kValue.f32.subnormal.negative.min),
f32(kValue.f32.infinity.positive),
f32(kValue.f32.infinity.negative),
f32(kValue.f32.positive.subnormal.max),
f32(kValue.f32.positive.subnormal.min),
f32(kValue.f32.negative.subnormal.max),
f32(kValue.f32.negative.subnormal.min),
f32(kValue.f32.positive.infinity),
f32(kValue.f32.negative.infinity),

f16(0),
f16(-0),
Expand All @@ -119,12 +119,12 @@ g.test('value').fn(t => {
f16(-0.5),
f16(kValue.f32.positive.max),
f16(kValue.f32.positive.min),
f16(kValue.f32.subnormal.positive.max),
f16(kValue.f32.subnormal.positive.min),
f16(kValue.f32.subnormal.negative.max),
f16(kValue.f32.subnormal.negative.min),
f16(kValue.f32.infinity.positive),
f16(kValue.f32.infinity.negative),
f16(kValue.f32.positive.subnormal.max),
f16(kValue.f32.positive.subnormal.min),
f16(kValue.f32.negative.subnormal.max),
f16(kValue.f32.negative.subnormal.min),
f16(kValue.f32.positive.infinity),
f16(kValue.f32.negative.infinity),

bool(true),
bool(false),
Expand Down Expand Up @@ -225,20 +225,20 @@ g.test('fpinterval_f32').fn(t => {
FP.f32.toInterval(-0.5),
FP.f32.toInterval(kValue.f32.positive.max),
FP.f32.toInterval(kValue.f32.positive.min),
FP.f32.toInterval(kValue.f32.subnormal.positive.max),
FP.f32.toInterval(kValue.f32.subnormal.positive.min),
FP.f32.toInterval(kValue.f32.subnormal.negative.max),
FP.f32.toInterval(kValue.f32.subnormal.negative.min),
FP.f32.toInterval(kValue.f32.infinity.positive),
FP.f32.toInterval(kValue.f32.infinity.negative),
FP.f32.toInterval(kValue.f32.positive.subnormal.max),
FP.f32.toInterval(kValue.f32.positive.subnormal.min),
FP.f32.toInterval(kValue.f32.negative.subnormal.max),
FP.f32.toInterval(kValue.f32.negative.subnormal.min),
FP.f32.toInterval(kValue.f32.positive.infinity),
FP.f32.toInterval(kValue.f32.negative.infinity),

FP.f32.toInterval([-0, 0]),
FP.f32.toInterval([-1, 1]),
FP.f32.toInterval([-0.5, 0.5]),
FP.f32.toInterval([kValue.f32.positive.min, kValue.f32.positive.max]),
FP.f32.toInterval([kValue.f32.subnormal.positive.min, kValue.f32.subnormal.positive.max]),
FP.f32.toInterval([kValue.f32.subnormal.negative.min, kValue.f32.subnormal.negative.max]),
FP.f32.toInterval([kValue.f32.infinity.negative, kValue.f32.infinity.positive]),
FP.f32.toInterval([kValue.f32.positive.subnormal.min, kValue.f32.positive.subnormal.max]),
FP.f32.toInterval([kValue.f32.negative.subnormal.min, kValue.f32.negative.subnormal.max]),
FP.f32.toInterval([kValue.f32.negative.infinity, kValue.f32.positive.infinity]),
]) {
const serialized = serializeFPInterval(interval);
const deserialized = deserializeFPInterval(serialized);
Expand All @@ -259,20 +259,20 @@ g.test('fpinterval_abstract').fn(t => {
FP.abstract.toInterval(-0.5),
FP.abstract.toInterval(kValue.f64.positive.max),
FP.abstract.toInterval(kValue.f64.positive.min),
FP.abstract.toInterval(kValue.f64.subnormal.positive.max),
FP.abstract.toInterval(kValue.f64.subnormal.positive.min),
FP.abstract.toInterval(kValue.f64.subnormal.negative.max),
FP.abstract.toInterval(kValue.f64.subnormal.negative.min),
FP.abstract.toInterval(kValue.f64.infinity.positive),
FP.abstract.toInterval(kValue.f64.infinity.negative),
FP.abstract.toInterval(kValue.f64.positive.subnormal.max),
FP.abstract.toInterval(kValue.f64.positive.subnormal.min),
FP.abstract.toInterval(kValue.f64.negative.subnormal.max),
FP.abstract.toInterval(kValue.f64.negative.subnormal.min),
FP.abstract.toInterval(kValue.f64.positive.infinity),
FP.abstract.toInterval(kValue.f64.negative.infinity),

FP.abstract.toInterval([-0, 0]),
FP.abstract.toInterval([-1, 1]),
FP.abstract.toInterval([-0.5, 0.5]),
FP.abstract.toInterval([kValue.f64.positive.min, kValue.f64.positive.max]),
FP.abstract.toInterval([kValue.f64.subnormal.positive.min, kValue.f64.subnormal.positive.max]),
FP.abstract.toInterval([kValue.f64.subnormal.negative.min, kValue.f64.subnormal.negative.max]),
FP.abstract.toInterval([kValue.f64.infinity.negative, kValue.f64.infinity.positive]),
FP.abstract.toInterval([kValue.f64.positive.subnormal.min, kValue.f64.positive.subnormal.max]),
FP.abstract.toInterval([kValue.f64.negative.subnormal.min, kValue.f64.negative.subnormal.max]),
FP.abstract.toInterval([kValue.f64.negative.infinity, kValue.f64.positive.infinity]),
]) {
const serialized = serializeFPInterval(interval);
const deserialized = deserializeFPInterval(serialized);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ const f32InfAndNaNInU32: number[] = [
// The positive NaN with the lowest integer representation is the integer
// for infinity, plus one.
// The positive NaN with the highest integer representation is i32.max (!)
...linearRange(kBit.f32.infinity.positive + 1, kBit.i32.positive.max, numNaNs),
...linearRange(kBit.f32.positive.infinity + 1, kBit.i32.positive.max, numNaNs),
// The negative NaN with the lowest integer representation is the integer
// for negative infinity, plus one.
// The negative NaN with the highest integer representation is u32.max (!)
...linearRange(kBit.f32.infinity.negative + 1, kBit.u32.max, numNaNs),
kBit.f32.infinity.positive,
kBit.f32.infinity.negative,
...linearRange(kBit.f32.negative.infinity + 1, kBit.u32.max, numNaNs),
kBit.f32.positive.infinity,
kBit.f32.negative.infinity,
];
const f32InfAndNaNInF32 = f32InfAndNaNInU32.map(u => reinterpretU32AsF32(u));
const f32InfAndNaNInI32 = f32InfAndNaNInU32.map(u => reinterpretU32AsI32(u));
Expand All @@ -102,13 +102,13 @@ const f16InfAndNaNInU16: number[] = [
// The positive NaN with the lowest integer representation is the integer
// for infinity, plus one.
// The positive NaN with the highest integer representation is u16 0x7fff i.e. 32767.
...linearRange(kBit.f16.infinity.positive + 1, 32767, numNaNs).map(v => Math.ceil(v)),
...linearRange(kBit.f16.positive.infinity + 1, 32767, numNaNs).map(v => Math.ceil(v)),
// The negative NaN with the lowest integer representation is the integer
// for negative infinity, plus one.
// The negative NaN with the highest integer representation is u16 0xffff i.e. 65535
...linearRange(kBit.f16.infinity.negative + 1, 65535, numNaNs).map(v => Math.floor(v)),
kBit.f16.infinity.positive,
kBit.f16.infinity.negative,
...linearRange(kBit.f16.negative.infinity + 1, 65535, numNaNs).map(v => Math.floor(v)),
kBit.f16.positive.infinity,
kBit.f16.negative.infinity,
];
const f16InfAndNaNInF16 = f16InfAndNaNInU16.map(u => reinterpretU16AsF16(u));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const d = makeCaseCache('quantizeToF16', {
[
kValue.f16.negative.min,
kValue.f16.negative.max,
kValue.f16.subnormal.negative.min,
kValue.f16.subnormal.negative.max,
kValue.f16.subnormal.positive.min,
kValue.f16.subnormal.positive.max,
kValue.f16.negative.subnormal.min,
kValue.f16.negative.subnormal.max,
kValue.f16.positive.subnormal.min,
kValue.f16.positive.subnormal.max,
kValue.f16.positive.min,
kValue.f16.positive.max,
...fullF16Range(),
Expand All @@ -44,10 +44,10 @@ export const d = makeCaseCache('quantizeToF16', {
[
kValue.f16.negative.min,
kValue.f16.negative.max,
kValue.f16.subnormal.negative.min,
kValue.f16.subnormal.negative.max,
kValue.f16.subnormal.positive.min,
kValue.f16.subnormal.positive.max,
kValue.f16.negative.subnormal.min,
kValue.f16.negative.subnormal.max,
kValue.f16.positive.subnormal.min,
kValue.f16.positive.subnormal.max,
kValue.f16.positive.min,
kValue.f16.positive.max,
...fullF32Range(),
Expand Down
4 changes: 2 additions & 2 deletions src/webgpu/shader/execution/expression/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ function abstractFloatSnippet(expr: string, case_idx: number, accessor: string =
//
// // Detect if the value is zero or subnormal, so that FTZ behaviour
// // can occur
// const subnormal_or_zero : bool = (${expr} <= ${kValue.f64.subnormal.positive.max}) && (${expr} >= ${kValue.f64.subnormal.negative.min});
// const subnormal_or_zero : bool = (${expr} <= ${kValue.f64.positive.subnormal.max}) && (${expr} >= ${kValue.f64.negative.subnormal.min});
//
// // MSB of the upper u32 is 1 if the value is negative, otherwise 0
// // Extract the sign bit early, so that abs() can be used with
Expand Down Expand Up @@ -903,7 +903,7 @@ function abstractFloatSnippet(expr: string, case_idx: number, accessor: string =
// prettier-ignore
return ` {
const kExponentBias = 1022;
const subnormal_or_zero : bool = (${expr}${accessor} <= ${kValue.f64.subnormal.positive.max}) && (${expr}${accessor} >= ${kValue.f64.subnormal.negative.min});
const subnormal_or_zero : bool = (${expr}${accessor} <= ${kValue.f64.positive.subnormal.max}) && (${expr}${accessor} >= ${kValue.f64.negative.subnormal.min});
const sign_bit : u32 = select(0, 0x80000000, ${expr}${accessor} < 0);
const f = frexp(abs(${expr}${accessor}));
const f_fract = select(f.fract, 0, subnormal_or_zero);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ const f32InfAndNaNInU32: number[] = [
// The positive NaN with the lowest integer representation is the integer
// for infinity, plus one.
// The positive NaN with the highest integer representation is i32.max (!)
...linearRange(kBit.f32.infinity.positive + 1, kBit.i32.positive.max, numNaNs),
...linearRange(kBit.f32.positive.infinity + 1, kBit.i32.positive.max, numNaNs),
// The negative NaN with the lowest integer representation is the integer
// for negative infinity, plus one.
// The negative NaN with the highest integer representation is u32.max (!)
...linearRange(kBit.f32.infinity.negative + 1, kBit.u32.max, numNaNs),
kBit.f32.infinity.positive,
kBit.f32.infinity.negative,
...linearRange(kBit.f32.negative.infinity + 1, kBit.u32.max, numNaNs),
kBit.f32.positive.infinity,
kBit.f32.negative.infinity,
];

g.test('bad_const_to_f32')
Expand Down
Loading

0 comments on commit 6442db8

Please sign in to comment.