| 486 | } |
| 487 | |
| 488 | void TestAvoidBias() { |
| 489 | for (int i = 0; i < 256; ++i) { |
| 490 | const float as_float = QuantizedToFloat<quint8>(i, 0.0f, 2.0f); |
| 491 | const int back_to_int = FloatToQuantized<quint8>(as_float, 0.0f, 2.0f); |
| 492 | EXPECT_EQ(i, back_to_int); |
| 493 | } |
| 494 | |
| 495 | // All perfectly representable floats should survive quantization, even |
| 496 | // if we pick a range where min is not itself perfectly representable. |
| 497 | const float min = -0.1375f; |
| 498 | const float max = 1.1385f; |
| 499 | const float step_size = (max - min) / 255.0f; |
| 500 | const float tolerance = step_size / 1000.0f; |
| 501 | // This is the smallest perfectly representable float in the range. |
| 502 | float first_float = std::ceil(min / step_size) * step_size; |
| 503 | for (float f = first_float; f <= max; f += step_size) { |
| 504 | const int as_int = FloatToQuantized<quint8>(f, min, max); |
| 505 | const float back_to_float = QuantizedToFloat<quint8>(as_int, min, max); |
| 506 | EXPECT_NEAR(f, back_to_float, tolerance); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void TestRequantizeInNewRange() { |
| 511 | // These are the float values we're going to test the conversions on. |