| 109 | |
| 110 | template <> |
| 111 | void Float16ConversionTest<float>::TestRoundTrip() { |
| 112 | // Expected values were also manually validated with numpy-1.24.3 |
| 113 | const RoundTripTestCase test_cases[] = { |
| 114 | // +/-0.0f |
| 115 | {F32(0x80000000u), 0b1000000000000000u, -0.0f}, |
| 116 | {F32(0x00000000u), 0b0000000000000000u, +0.0f}, |
| 117 | // 32-bit exp is 102 => 2^-25. Rounding to nearest. |
| 118 | {F32(0xb3000001u), 0b1000000000000001u, -5.96046447754e-8f}, |
| 119 | // 32-bit exp is 102 => 2^-25. Rounding to even. |
| 120 | {F32(0xb3000000u), 0b1000000000000000u, -0.0f}, |
| 121 | // 32-bit exp is 101 => 2^-26. Underflow to zero. |
| 122 | {F32(0xb2800001u), 0b1000000000000000u, -0.0f}, |
| 123 | // 32-bit exp is 108 => 2^-19. |
| 124 | {F32(0xb61a0000u), 0b1000000000100110u, -2.26497650146e-6f}, |
| 125 | // 32-bit exp is 108 => 2^-19. |
| 126 | {F32(0xb61e0000u), 0b1000000000101000u, -2.38418579102e-6f}, |
| 127 | // 32-bit exp is 112 => 2^-15. Rounding to nearest. |
| 128 | {F32(0xb87fa001u), 0b1000001111111111u, -6.09755516052e-5f}, |
| 129 | // 32-bit exp is 112 => 2^-15. Rounds to 16-bit exp of 1 => 2^-14 |
| 130 | {F32(0xb87fe001u), 0b1000010000000000u, -6.103515625e-5f}, |
| 131 | // 32-bit exp is 142 => 2^15. Rounding to nearest. |
| 132 | {F32(0xc7001001u), 0b1111100000000001u, -32800.0f}, |
| 133 | // 32-bit exp is 142 => 2^15. Rounding to even. |
| 134 | {F32(0xc7001000u), 0b1111100000000000u, -32768.0f}, |
| 135 | // 65520.0f rounds to inf |
| 136 | {F32(0x477ff000u), 0b0111110000000000u, Limits<float>::infinity()}, |
| 137 | // 65488.0039062f rounds to 65504.0 (float16 max) |
| 138 | {F32(0x477fd001u), 0b0111101111111111u, 65504.0f}, |
| 139 | // 32-bit exp is 127 => 2^0, rounds to 16-bit exp of 16 => 2^1. |
| 140 | {F32(0xbffff000u), 0b1100000000000000u, -2.0f}, |
| 141 | // Extreme values should safely clamp to +/-inf |
| 142 | {Limits<float>::max(), 0b0111110000000000u, +Limits<float>::infinity()}, |
| 143 | {Limits<float>::lowest(), 0b1111110000000000u, -Limits<float>::infinity()}, |
| 144 | }; |
| 145 | |
| 146 | TestRoundTrip(std::span(test_cases, std::size(test_cases))); |
| 147 | } |
| 148 | |
| 149 | template <> |
| 150 | void Float16ConversionTest<double>::TestRoundTrip() { |
no test coverage detected