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

include: sys: util_macro FIX BIT_MASK(32) #42226

Closed

Conversation

julien-massot
Copy link
Collaborator

@julien-massot julien-massot commented Jan 27, 2022

When using BIT_MASK(32) on a arm 32bits processor, the macro
generate a warning:

warning: left shift count >= width of type [-Wshift-count-overflow]
44 | #define BIT(n) (1UL << (n))

To generate a bit mask we set the n bit to 1 and then do minus -1.
So we need an intermediate 33 bits variable to have a 32 bit mask.

fixes #42163

Signed-off-by: Julien Massot julien.massot@iot.bzh

When generating BIT_MASK(32) on a arm 32 processor, the macro
generate a warning:

warning: left shift count >= width of type [-Wshift-count-overflow]
44 | #define BIT(n) (1UL << (n))

To generate a bit mask we set the n bit to 1 and then do minus -1.
So we need an intermediate 33 bits variable to have a 32 bit mask.

Signed-off-by: Julien Massot <julien.massot@iot.bzh>
@github-actions github-actions bot added the area: API Changes to public APIs label Jan 27, 2022
@julien-massot
Copy link
Collaborator Author

julien-massot commented Jan 27, 2022

fix #42163

@julien-massot julien-massot added the bug The issue is a bug, or the PR is fixing a bug label Jan 27, 2022
@zephyrbot zephyrbot added the area: Base OS Base OS Library (lib/os) label Feb 1, 2022
@@ -65,7 +65,7 @@ extern "C" {
* @brief Bit mask with bits 0 through <tt>n-1</tt> (inclusive) set,
* or 0 if @p n is 0.
*/
#define BIT_MASK(n) (BIT(n) - 1UL)
#define BIT_MASK(n) (BIT64(n) - 1UL)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is going to produce an rvalue with a 64 bit type, which is very likely not what you want as it will widen the math of any expression it's placed in, likely produce extra instructions to compute, and potentially change the result vs. what we had before. You want to cast this back to "unsigned long"[1] to preserve compatibility with the existing API.

[1] And not to a fixed-side type! On 64 bit platforms obviously all the expanded constants are longs, and thus 64 bit.

Copy link
Member

Choose a reason for hiding this comment

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

I completely agree, I do not think we should do this at all. Instead, please add a new BIT_MASK64() that uses BIT64

Copy link
Member

Choose a reason for hiding this comment

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

I completely agree, I do not think we should do this at all. Instead, please add a new BIT_MASK64() that uses BIT64

In fact, there is already BIT64_MASK right below that does just that:

#define BIT64_MASK(n) (BIT64(n) - 1ULL)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi, @andyross @carlescufi @stephanosio
Not sure I was clear enough, what I'm trying to fix is this code sample

uint32_t mask = BIT_MASK(32); /* 0xffffffff */

Are you suggesting to use this instead ?
uint32_t mask = BIT64_MASK(32); /* 0xffffffff */

Copy link
Contributor

Choose a reason for hiding this comment

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

This fix is a legitimate warning workaround for the 32 bit version of the macro. It just needs a cast on the final expression to prevent changing the API (effectively what the code as written does is, in fact, implement BIT64_MASK(), and that's not what you want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok thanks you all !

@@ -65,7 +65,7 @@ extern "C" {
* @brief Bit mask with bits 0 through <tt>n-1</tt> (inclusive) set,
* or 0 if @p n is 0.
*/
#define BIT_MASK(n) (BIT(n) - 1UL)
#define BIT_MASK(n) (BIT64(n) - 1UL)
Copy link
Member

Choose a reason for hiding this comment

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

I completely agree, I do not think we should do this at all. Instead, please add a new BIT_MASK64() that uses BIT64

julien-massot pushed a commit to iotbzh/zephyr that referenced this pull request Mar 10, 2022
On 32bit compiler the BIT_MASK(32) generate a warning,
after discussion on zephyrproject-rtos#42226 and zephyrproject-rtos#42163, advise was to use
BIT64_MASK instead.

Signed-off-by: Julien Massot <julien.massot@iot.bzh>
carlescufi pushed a commit that referenced this pull request Mar 11, 2022
On 32bit compiler the BIT_MASK(32) generate a warning,
after discussion on #42226 and #42163, advise was to use
BIT64_MASK instead.

Signed-off-by: Julien Massot <julien.massot@iot.bzh>
RodinHaker pushed a commit to abelsensors/zephyr that referenced this pull request Mar 19, 2022
On 32bit compiler the BIT_MASK(32) generate a warning,
after discussion on zephyrproject-rtos#42226 and zephyrproject-rtos#42163, advise was to use
BIT64_MASK instead.

Signed-off-by: Julien Massot <julien.massot@iot.bzh>
@aaillet aaillet deleted the iot/fix-bitmask branch September 7, 2022 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Base OS Base OS Library (lib/os) bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BIT_MASK(32) generate warning on 32 bits processor
5 participants