Skip to content

Conversation

@kv2019i
Copy link
Collaborator

@kv2019i kv2019i commented Dec 11, 2025

Zephyr commit ("sys: util: Add gcd and lcm utilities") added a version of gcd() to sys/util.h that now can conflict with SOF numbers.h definition of gcd().

Make the SOF definition conditional. The Zephyr default gcd() version takes unsigned numbers while SOF version took signed, but it seems SOF existing usage is fine to make this change.

Link: #10428

Zephyr commit ("sys: util: Add gcd and lcm utilities") added a version
of gcd() to sys/util.h that now can conflict with SOF numbers.h
definition of gcd().

Make the SOF definition conditional. The Zephyr default gcd() version
takes unsigned numbers while SOF version took signed, but it seems
SOF existing usage is fine to make this change.

Link: thesofproject#10428
Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Copilot AI review requested due to automatic review settings December 11, 2025 09:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR resolves a naming conflict between SOF's gcd() function and a newer Zephyr implementation by making the SOF definition conditional. The change ensures that if Zephyr's gcd() is available, it will be used instead of defining SOF's version.

Key changes:

  • Made SOF's gcd() function definition conditional using preprocessor guards
  • Added USE_SOF_GCD macro to control when SOF's implementation should be compiled

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/include/sof/math/numbers.h Added conditional compilation guard to only declare gcd() if not already defined by Zephyr
src/math/numbers.c Wrapped gcd() implementation in conditional compilation block matching the header

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kv2019i
Copy link
Collaborator Author

kv2019i commented Dec 11, 2025

FYI @singalsu . I'll proceed with merge given tests seem to pass and this fixes the PR CI. Please follow-up if you have/had something in plans where we need to bring signed gcd variant back.

@kv2019i
Copy link
Collaborator Author

kv2019i commented Dec 11, 2025

Known issues seen https://github.com/thesofproject/sof/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Bugs%20seen%20in%20PR%20CI%22 -- otherwise looks good, proceeding with merge.

@kv2019i kv2019i merged commit 0855423 into thesofproject:main Dec 11, 2025
37 of 43 checks passed
@singalsu
Copy link
Collaborator

Sounds honestly quite stupid to make a gcd function that takes only positive numbers, but since I don't think I've needed signed gcd() it's OK. Should scrutinize the Zephyr gcd() instead. Someone will fall into that trap for sure. It should be called gcd_unsigned() instead. Again an overly eager usage of unsigned when you can case. I don't get the C experts' preference for unsigned types. I've crashed and burned with those in my previous life with signal processing a mass market device so better to not be unsigned unless really needed (for that extra positive range and can't go int64_t or for bitfields).

@kv2019i
Copy link
Collaborator Author

kv2019i commented Dec 12, 2025

@singalsu Looking at sys/util.h again, it seems it's doing some cleverness to handle both signed and unsigned use. The gcd() is a macro that is defined as:

#define gcd(a, b) ((((__typeof__(a))-1) < 0) ? gcd_s(a, b) : gcd_u(a, b))

So if 'a' is a signed type, the gdc_s() will be called (which takes signed arguments). So I think we are good, unless "gdc()" is called in some inner algo loop, which it probably isn't (and shouldn't be).

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.

5 participants