Date: Mon, 28 Apr 2025 21:32:59 -0700
On Monday, 28 April 2025 20:34:57 Pacific Daylight Time None via Std-Proposals
wrote:
> #include <iostream>
> #include <cstdint>
>
> uint64_t myLog(uint64_t a, uint64_t b) {
> if consteval (b) { // first judge whether b is constexpr
> if (isPowerOfTwo(b)) { // then judge whether b is power of two
> // Optimized implementation for b being compile-time known and a
> power of 2 return ...;
> }
> }
> // Generic implementation
> }
If you want this to go further, please study GCC's __builtin_constant_p()
intrinsic, which is exactly what this is. Research where it's used and useful
for, as well as where it doesn't work and its weaknesses.
The Linux kernel uses it a lot. But it uses a lot of it in macros, which may
be the weakness.
wrote:
> #include <iostream>
> #include <cstdint>
>
> uint64_t myLog(uint64_t a, uint64_t b) {
> if consteval (b) { // first judge whether b is constexpr
> if (isPowerOfTwo(b)) { // then judge whether b is power of two
> // Optimized implementation for b being compile-time known and a
> power of 2 return ...;
> }
> }
> // Generic implementation
> }
If you want this to go further, please study GCC's __builtin_constant_p()
intrinsic, which is exactly what this is. Research where it's used and useful
for, as well as where it doesn't work and its weaknesses.
The Linux kernel uses it a lot. But it uses a lot of it in macros, which may
be the weakness.
-- Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org Principal Engineer - Intel DCAI Platform & System Engineering
Received on 2025-04-29 04:33:01