yf13 commented on code in PR #12427: URL: https://github.com/apache/nuttx/pull/12427#discussion_r1625173897
########## include/nuttx/lib/math32.h: ########## @@ -52,11 +52,15 @@ #define FLS2(n) ((n) & 0x2 ? 1 + FLS1 ((n) >> 1) : FLS1 (n)) #define FLS1(n) ((n) & 0x1 ? 1 : 0) +/* Checks if an integer is power of two at compile time */ + +#define IS_POWER_OF_2(n) ((n) > 0 && ((n) & (n - 1)) == 0) + /* Returns round up and round down value of log2(n). Note: it can be used at * compile time. */ -#define LOG2_CEIL(n) ((n) & (n - 1) ? FLS(n) : FLS(n) - 1) +#define LOG2_CEIL(n) (IS_POWER_OF_2(n) ? FLS(n) - 1 : FLS(n)) Review Comment: @pkarashchenko my observations: - Tests for `LOG2_CEIL(0)`: previously it returns `-1` and now it returns `0`. - Python reports `domain error` for `ceil(log2(0))`. - There are 3 uses of it in upstream: two with `size_t` input, one with OPEN_MAX. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
