Page MenuHomeFreeBSD

libc: implement C23 <stdbit.h> functions
Needs ReviewPublic

Authored by fuz on Mon, Nov 10, 9:30 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Nov 16, 5:15 PM
Unknown Object (File)
Sat, Nov 15, 3:14 PM
Unknown Object (File)
Fri, Nov 14, 3:55 PM
Unknown Object (File)
Fri, Nov 14, 3:55 PM
Unknown Object (File)
Fri, Nov 14, 3:55 PM
Unknown Object (File)
Fri, Nov 14, 1:30 PM
Unknown Object (File)
Fri, Nov 14, 6:56 AM
Unknown Object (File)
Fri, Nov 14, 5:26 AM
Subscribers

Details

Summary

This new header complies with ISO/IEC 9899:2024 (C23). See N3220 for the specification.

Contrary to glibc, we do not provide inline definitions in
<stdbit.h> as we expect our system compiler to soon recognise
these as builtins anyway.

Documentation is added in D53658, D53659, D53661.

Relnotes: yes

Test Plan

See D53660 for unit tests for the newly added functions.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 68739
Build 65622: arc lint + arc unit

Event Timeline

fuz requested review of this revision.Mon, Nov 10, 9:30 AM
kib added inline comments.
lib/libc/stdbit/Makefile.inc
22

Perhaps this should be removed?

lib/libc/stdbit/stdc_bit_ceil.c
13

Why? What is broken if sizeofs are equal?

lib/libc/stdbit/Makefile.inc
22

This hunk goes away in D53659, I forgot to edit it out of this patch set. Will do so.

lib/libc/stdbit/stdc_bit_ceil.c
13

If both types have the same size and the top bit and at least one other bit is set, __builtin_clz(x - 1) returns 0 and 1U << (CHAR_BIT * sizeof(unsigned) - __builtin_clz(x - 1)) shifts by the width of the type, an operation with undefined behaviour. See ISO/IEC 9899:2024 § 6.5.8 Bitwise shift operators ¶ 3 “[...] If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.”

  • Makefile.inc: remove commented out MAN and MKLINKS variables
  • remove - from /*- as per recent style(9)
lib/libc/stdbit/stdc_bit_ceil.c
13

I see, also I see that the standard defines that in this case the return value should be 0.

I think you should either add a comment from above nearby one of the asserts, or instead change code to check for x > the max value where the power of two number larger than x exists and return 0. IMO just add a comment.

@kib Did you get a chance to review the other functions, too? Is everything else ok?

lib/libc/stdbit/stdc_bit_ceil.c
13

Avoiding this if statement (which is present on sizes int and larger) is the whole point here. Not going to put it in, it costs performance after all.

But I can do a comment, that's ok.

fuz edited the test plan for this revision. (Show Details)
fuz added reviewers: dougm, imp, des.
This revision is now accepted and ready to land.Tue, Nov 18, 3:06 PM
  • add comment to stdc_bit_ceil.c as requested by @kib
  • sort SRCS alphabetically
This revision now requires review to proceed.Tue, Nov 18, 5:28 PM
des requested changes to this revision.Wed, Nov 19, 12:27 AM

Needs tests.

lib/libc/stdbit/stdc_bit_ceil.c
38

implicit int? in this economy?

This revision now requires changes to proceed.Wed, Nov 19, 12:27 AM
In D53657#1229282, @des wrote:

Needs tests.

Linked from the stack (I missed it too): https://reviews.freebsd.org/D53660

Ah sorry, I didn't notice that there was a separate review with tests. But please add explicit ints where appropriate.

kib added inline comments.
lib/libc/stdbit/stdc_bit_ceil.c
25

This should be a complete sentence.

38

Implicit int means something different; namely not providing a type designator at all. 'unsigned' alone is the std-compliant type name.

lib/libc/stdbit/stdc_bit_ceil.c
25

In fact, this should be the assertion message instead of the comment.

lib/libc/stdbit/stdc_bit_ceil.c
25

Can we please keep the bikeshedding down to sustainable levels?

The important part with this DR is that someone checks if my reading of the standard with respect to what these functions are supposed to do is correct, and nobody has so far indicated that this has been done.

  • use explicit unsigned int in <stdbit.h>
  • use explicit unsigned int in stdbit source files
  • add period to comment

The important thing in this review is to check that my understanding
of these functions is correct. The C23 standard does not have
pseudocode and I may have misunderstood the prose. Please may someone
cross-check.

  • use C23 *_WIDTH macros from <limits.h>, see D53825
  • add descriptive comments to all static_assert uses

This is cleaner than sizeof(type) * CHAR_BIT and works on platforms
with padding bits.

lib/libc/stdbit/stdc_leading_zeros.c
20

This looks right; you're putting a 1 at the size boundary bit so it will stop there if it's zero.

+ versus | ? That's about it from me.

32

Looks right.