pkarashchenko commented on code in PR #12427:
URL: https://github.com/apache/nuttx/pull/12427#discussion_r1624267727


##########
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:
   In general I recommend to add only necessary changes if possible. Especially 
that updated `LOG2_CEIL` macro leads to a different behavior than before (`n > 
0` is not in the current code). Also `IS_POWER_OF_2` macro seems to produce a 
`false` result if `n` is `1` (`2^0=1`), I'm not sure if the intention was to 
cover this case, but if somebody will use a new macro without looking into 
implementation it can lead to unexpected results.



-- 
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]

Reply via email to