Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #94, with unspecified behavior in pointer comparisons. #95

Merged
merged 1 commit into from
Aug 21, 2021

Conversation

Alexhuszagh
Copy link
Contributor

@Alexhuszagh Alexhuszagh commented Aug 21, 2021

Fixes #94, by replacing unspecified pointer comparisons with well-defined behavior using std::distance with the current pointer and the pointer to one-past-the-end of the array.

As a simple schematic of why this is important, we have:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| x | x | x | p | - | - | - | - | - | - | - | - |

As you can if we have already the x digits, with an array with 10 elements, and try to compare p+8 to pend, we compare after the end of the array. This could optimize to being always true, or the integer arithmetic could wrap, leading to another always-true comparison.

The conformant solution is therefore: std::distance(p, pend) >= 8, which also optimizes well, so there's no issue of a performance hit (just ensuring the compiler does not cause an undesired optimizations due to unspecified behavior, such as a potential infinite loop).

@lemire
Copy link
Member

lemire commented Aug 21, 2021

Thanks for the detailed information. (I was convinced in about 3 seconds after reading the title of the issue, but it is always helpful to document the issue because other people read these things over time.)

@lemire lemire merged commit 898f54f into fastfloat:main Aug 21, 2021
@lemire
Copy link
Member

lemire commented Aug 21, 2021

Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unspecified Behavior in Multi-Digit Optimizations
2 participants