-
Notifications
You must be signed in to change notification settings - Fork 86
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
Use int instead of char for flag variables #490
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #490 +/- ##
==========================================
- Coverage 85.52% 85.50% -0.03%
==========================================
Files 50 50
Lines 11656 11646 -10
Branches 2202 2201 -1
==========================================
- Hits 9969 9958 -11
- Misses 1687 1688 +1 ☔ View full report in Codecov by Sentry. |
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.
Mostly LGTM from me, thanks for the fix. Build failure seems to be unrelated, I'll try to address this in a separate PR.
Suggestion: maybe you could expand commit message with the body of your pr description? Reduce failed example a little.
This comment was marked as outdated.
This comment was marked as outdated.
The char type can be equivalent to either signed char or unsigned char. If the latter, assigning -1 to endian in GMPy_MPZ_Method_From_Bytes actually stores the value 255, a positive value. This leads to incorrect conversions, manifesting as failure of test_mpz_from_bytes. Using char doesn't save any space, since local variables are stored in 32- or 64-bit CPU registers, or in 32-bit aligned stack slots. Also, using char instead of int generates less efficient code. Modern 64-bit processors are optimized for 64- and 32-bit quantities. Operating on 8-bit quantities takes more clock cycles. Finally, negative values cannot be stored safely in char variables due to implicit conversion to positive values of platforms where char == unsigned char.
I think the PR comment is unnecessarily verbose. How does this look? |
Yep, fine. |
Thanks for the fix. |
Maybe this should go to the next bugfix release? |
I agree. I can release 2.2.1 this weekend.
…On Thu, Jul 11, 2024, 11:46 PM Sergey B Kirpichev ***@***.***> wrote:
Maybe this should go to the next bugfix release?
—
Reply to this email directly, view it on GitHub
<#490 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAMR233MLC63XWDGHKDBIM3ZL53TZAVCNFSM6AAAAABKTGOBYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRUHEZDOMRXGY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
I attempted to build version 2.2.0 for Fedora Rawhide. All of the non-x86 architectures (aarch, ppc64le, and s390x) failed a test:
The problem is the use of the
char
type for flag variables. The C standard says thatchar
can be equivalent to eithersigned char
orunsigned char
. The code inGMPy_MPZ_Method_From_Bytes
assigns -1 to such a variable,endian
. On platforms wherechar
==unsigned char
, the value stored is actually 255, a positive value.This PR converts flag variables declared with type
char
to typeint
instead, for the following reasons:char
doesn't save any space. These are local variables, so they are either in a CPU register (which is 32 or 64 bits, even if we only use 8 bits), or on the stack where, for alignment reasons, 32 bits will be reserved even though we only use 8 bits of it.char
instead ofint
generates less efficient code. Modern 64-bit processors are optimized for processing 64-bit and 32-bit quantities. They can operate on 8-bit quantities, but take more clock cycles to do so.char
type is only useful for unsigned 7-bit values; i.e., 0 to 127. That is the intersection of thesigned char
andunsigned char
types.With this change, the build succeeds on all architectures.