cts icon indicating copy to clipboard operation
cts copied to clipboard

wgsl: test hex float literal exact representation as f32

Open dneto0 opened this issue 3 years ago • 8 comments

see test cases: https://dawn-review.googlesource.com/c/dawn/+/91429/comments/b5af104c_967ac87d

fn foo() {
  let a =    0x8.0000ep+0f;  // exactly represented

           // Interesting case  because f32(0x8.00001p+0) would round down to exactly representable
  let b1 =   0x8.00001p+0f  // not exactly representable. shader-creation error. 

           // Interesting case  because f32(0x8.0000f8p+0) would round up to exactly representable
  let b2 =   0x8.0000f8p+0f  // not exactly representable. shader-creation error.
}

edited to fix comments

dneto0 avatar May 25 '22 20:05 dneto0

The f16 tests should also similar cases, with h suffix

dneto0 avatar May 25 '22 20:05 dneto0

fn foo() {
  let h = 0x1p-200f;
}

was the test case from the other issue. Small magnitude which should fail as not representable.

dj2 avatar May 25 '22 20:05 dj2

Another thing that might happen is that a hex float might have more mantissa bits than is representable in an f64 (presumed AbstractFloat).

fn foo() {
   // both these cases should be static error:
   // need 54 bits mantissa.
   //                         1234123412341
   let rounds_to_1_flat = 0x1.0000000000001p0;  // rounds down  to f64(1.0)
   let rounds_above_1   = 0x1.0000000000008p0;  // rounds up to f64(1.0) + f64 epsilon
}

dneto0 avatar May 26 '22 19:05 dneto0

Also, hex float values which are positive and negative infinity should be a shader error

dj2 avatar May 26 '22 19:05 dj2

Also, hex float values which are positive and negative infinity should be a shader error

Anything outside the finite range of the target type.

dneto0 avatar May 26 '22 20:05 dneto0

There are great explanations in this Tint code patch. https://dawn-review.googlesource.com/c/dawn/+/93100

It handles cases where a value maps to a subnormal f32 value. It also shows the f16 cases.

dneto0 avatar Jun 10 '22 13:06 dneto0

https://github.com/gpuweb/gpuweb/pull/3565 clarified the rule for hex floats: only 16 significant digits are required to be captured. If the mantissa truncates to 16 significant digits, then the either teh truncated mantissa or truncated_mantissa_next can be used to get the mathematical value. If the mantissa is 0 then 0 must be computed.

The test needs to check actual values parsed, not just pass/fail of parsing.

dneto0 avatar Nov 16 '22 20:11 dneto0

I updated the hex float parsing in SPIRV-Tools, and have crafted some good tests that demonstrate dropping extra significand bits. See https://github.com/KhronosGroup/SPIRV-Tools/pull/5025

dneto0 avatar Dec 19 '22 20:12 dneto0