-
-
Notifications
You must be signed in to change notification settings - Fork 362
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
Fix bugs in fixed division #5698
Open
hexagonrecursion
wants to merge
5
commits into
pioneerspacesim:master
Choose a base branch
from
hexagonrecursion:divide-and-bugfix
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b689dc
Fix bugs in fixed division
hexagonrecursion 43a8208
Add copyright
hexagonrecursion aa8d723
Use std::abs()
hexagonrecursion 859bf26
Oops: forgot inclide
hexagonrecursion 0ed527c
Add more tests; Fix a bug in test; Better comments
hexagonrecursion File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <limits> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add: // Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. I forgot |
||
#include "doctest.h" | ||
#include "fixed.h" | ||
|
||
TEST_CASE("Fixed") | ||
{ | ||
SUBCASE("operator ==") | ||
{ | ||
// 42.0 == 42.0 | ||
CHECK(fixed(42, 1) == fixed(42, 1)); | ||
// 123.0 != 321.0 | ||
CHECK(fixed(123, 1) != fixed(321, 1)); | ||
// 7.0 == 7.0 | ||
CHECK(fixed(0x7'00000000) == fixed(7, 1)); | ||
} | ||
|
||
SUBCASE("operator /") | ||
{ | ||
// 3.0 / 3.0 == 1.0 | ||
CHECK(fixed(3, 1) / fixed(3, 1) == fixed(1, 1)); | ||
// -3.0 / 3.0 == -1.0 | ||
// There used to be a bug causing this to produce an incorrect result | ||
CHECK(fixed(-3'00000000) / fixed(3, 1) == fixed(-1'00000000)); | ||
// 0x40000000.00000000 / 0x40000000.00000001 == 0x00000000.ffffffff (because we round down) | ||
// There used to be a bug causing this to produce an incorrect result | ||
CHECK(fixed(0x40000000'00000000) / fixed(0x40000000'00000001) == fixed(0x00000000'ffffffff)); | ||
// fixed(INT_MIN) / fixed(INT_MIN) used to trigger a signed int overflow | ||
Sint64 min = std::numeric_limits<Sint64>::min(); | ||
CHECK(fixed(min) / fixed(min) == fixed(1, 1)); | ||
// fixed(INT_MIN) / -1.0 used to trigger a signed int overflow | ||
CHECK(fixed(min) / fixed(-1'00000000) == fixed(min) / fixed(-1'00000000)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would strongly recommend using bitwise ops and std::abs rather than explicit branches, as it will generally compile into optimized code under more scenarios. E.g. something like:
(Note: this is well-defined,
true
is defined to promote to the integral value1
.)See this Godbolt for example of how this affects code that would be generated in debug mode: https://godbolt.org/z/6MsTPv8dz
Yes, under
-O3
or equivalent this code would produce the same generated assembly, but the above suggestion will both produce better performance in debug mode (wasting less programmer time) and more clearly expresses what is logically occurring in the if statements without the extra cognitive overhead of the order-dependent logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please note that with the requested changes dividing fixed(INT64_MIN) by anything or dividing anything by fixed(INT64_MIN) causes undefined behaviour because std::abs(INT64_MIN) is undefined behaviour.