Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MAINT: Avoid dereferencing/strict aliasing warnings#28892

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ngoldbaum merged 2 commits intonumpy:mainfromilayn:unbark_npymath
May 6, 2025

Conversation

ilayn
Copy link
Contributor

@ilaynilayn commentedMay 3, 2025
edited
Loading

I am aware that this file went through many iterations and complex support is a constant headache, and if there is any more appetite left, I'd like to offer another minor change.

We have been having some warnings coming from non-C99-compliant compilers and C++ runs over SciPy since NumPy 2.0. This is mainly due to thenpy_crealX/npy_cimagX functions, casting the inputstruct first and then dereferencing it in the new type, causing the strict-aliasing (type punning) rule to fire.

Here is a recent example how this manifests itself in the build processhttps://github.com/scipy/scipy/actions/runs/14770722795/job/41470198162#step:6:300

[snip] In file included from ..\scipy\_build_utils\src/npy_2_complexcompat.h:4,                 from ../scipy/special/xsf_wrappers.h:13,                 from ../scipy/special/xsf_wrappers.cpp:1:C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\site-packages\numpy\_core\include/numpy/npy_math.h: Infunction'npy_longdouble npy_creall(npy_clongdouble)':C:\hostedtoolcache\windows\Python\3.12.10\x64\Lib\site-packages\numpy\_core\include/numpy/npy_math.h:422:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]  422 |     return ((longdouble_t *) &z)[0];      |            ~^~~~~~~~~~~~~~~~~~~~[snip]

I don't know if the SciPy code is also commiting some C++ sins but the NumPy code can do better to avoid strange bugs. I don't claim to have the right solution but seems like the most obvious one to me to let the compiler handle the possible padding/endianness issues with a straight struct field access. However I don't have a strong opinion since I'm not a C++ expert.

This is also somewhat related to our ambition to be warning free at some point.

@charrischarris added the 09 - Backport-CandidatePRs tagged should be backported labelMay 4, 2025
@ngoldbaum
Copy link
Member

Ping@lysnikolaou - do you happen to remember why you spelled these accesses this way originally?

Copy link
Member

@lysnikolaoulysnikolaou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This looks good to me.

Initially the#ifdef wasn't there, so casting to the corresponding type worked across all different complex types. Now that the#ifdef is there, using the struct field makes more sense.

@ngoldbaum
Copy link
Member

OK, in it goes. Thanks@ilayn!

ilayn reacted with thumbs up emoji

@ngoldbaumngoldbaum merged commit83a4e6a intonumpy:mainMay 6, 2025
73 checks passed
charris pushed a commit to charris/numpy that referenced this pull requestMay 6, 2025
…sts in `npy_math.h` for C++ runs (numpy#28892)* MAINT: Avoid type-punning complex casts in npy_math.h* MAINT: Add missing typecast in npy_cimagl
@charrischarris changed the titleMAINT: Avoid dereferencing/strict aliasing warnings during complex casts innpy_math.h for C++ runsMAINT: Avoid dereferencing/strict aliasing warningsMay 6, 2025
@charrischarris removed the 09 - Backport-CandidatePRs tagged should be backported labelMay 6, 2025
@ilaynilayn deleted the unbark_npymath branchMay 6, 2025 23:29
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull requestMay 8, 2025
…sts in `npy_math.h` for C++ runs (numpy#28892)* MAINT: Avoid type-punning complex casts in npy_math.h* MAINT: Add missing typecast in npy_cimagl
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@lysnikolaoulysnikolaoulysnikolaou approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@ilayn@ngoldbaum@lysnikolaou@charris

[8]ページ先頭

©2009-2025 Movatter.jp