Commit 80e4a6c
authored
Refactor float_to_float16_untyped_slow for clarity and optimization (#330)
This pull request refactors the `float_to_float16_untyped_slow` function
in `float16.h` to improve code clarity and maintainability. The logic is
now expressed with clear conditional branches instead of a single
complex expression, making it easier to understand and maintain while
preserving the original functionality.
> Note: Change in PR is inspired by #324 made by @mnorris11
**Refactoring and code clarity:**
* Replaced the single complex return statement in
`float_to_float16_untyped_slow` with explicit conditional branches for
handling saturation, normalization, denormalization, and sign
extraction, improving readability and maintainability.
* Refactored code is clear and simple. Modern C++ compilers generate
well-optimized assembly for the new code - better than previous. See
QuickBench
[results](https://quick-bench.com/q/AUjkLSDctpsAf6tKavxwDzxrFsM) for
benchmark code:
[float_to_float16_bench.cpp](https://github.com/user-attachments/files/27627387/float_to_float16_bench.cpp)1 parent dc52d34 commit 80e4a6c
1 file changed
Lines changed: 20 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
65 | 79 | | |
66 | 80 | | |
67 | 81 | | |
| |||
0 commit comments