clk-divider: fix is_power_of_two()
PATCH 1/1 clk-divider: fix is_power_of_two()
The macro is_power_of_two() in clk-divider.c was defined as !(i & ~i) which is always true. Correct it to !(i & (i - 1)).
Also add brackets around the macro arguments in div_mask and is_power_of_two, as well as around the is_power_of_two expression as a whole to avoid any future operator precedence problems.
---
I haven't tested this other than with a couple of compile time asserts.
drivers/clk/clk-divider.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk-divider.c +2 -2
@@ -29,8 +29,8 @@#define to_clk_divider (_hw) container_of(_hw, struct clk_divider, hw) -#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1) +#define is_power_of_two (i) (!((i) & ((i) - 1)))static unsigned int _get_table_maxdiv (const struct clk_div_table *table) {
Signature · 1 line
1.7.7.6
Quoted 2 lines · James Hogan
The macro
is_power_of_two()inclk-divider.cwas defined as !(i & ~i) which is always true. Correct it to !(i & (i - 1)).
[]
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0
[]
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0@@ -29,8 +29,8 @@
[]
drivers/clk/clk-divider.c line 33
Quoted 4 lines
drivers/clk/clk-divider.c +2
-2-#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1) +#define is_power_of_two (i) (!((i) & ((i) - 1)))
Use is_power_of_2 in log2.h instead?
Quoting Joe Perches (2013-01-15 10:31:05)
Quoted 6 lines · James Hogan
Quoted 2 lines
The macro
is_power_of_two()inclk-divider.cwas defined as !(i & ~i) which is always true. Correct it to !(i & (i - 1)).[]
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0[]
Quoted 8 lines
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0@@ -29,8 +29,8 @@[]
drivers/clk/clk-divider.c line 30
Quoted 4 lines
drivers/clk/clk-divider.c +2
-2-#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1) +#define is_power_of_two (i) (!((i) & ((i) - 1)))Use
is_power_of_2inlog2.hinstead?
Both are fine suggestions. How about I apply the below patch?
Thanks,
Mike
Quoted 1 line
From
54c6c9e5f61693effbe6e4fb28ef9f2f9b3d7a23Mon Sep 17 00:00:00 2001
From: James Hogan <[email protected]>
Date: Tue, 15 Jan 2013 10:28:05 +0000
Subject: [PATCH] clk-divider: fix macros
The macro is_power_of_two() in clk-divider.c was defined as !(i & ~i) which is always true. Instead use is_power_of_2() from log2.h.
Also add brackets around the macro arguments in div_mask to avoid any future operator precedence problems.
--- drivers/clk/clk-divider.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/clk-divider.c +3 -3
@@ -16,6 +16,7 @@#include <linux/io.h> #include <linux/err.h> #include <linux/string.h> +#include <linux/log2.h> /* * DOC: basic adjustable divider clock that cannot gate
@@ -29,8 +30,7 @@#define to_clk_divider (_hw) container_of(_hw, struct clk_divider, hw) -#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1)static unsigned int _get_table_maxdiv (const struct clk_div_table *table) {
@@ -137,7 +137,7 @@ static bool _is_valid_table_div(const struct clk_div_table *table,static bool _is_valid_div (struct clk_divider *divider,unsigned int div) {if (divider->flags &CLK_DIVIDER_POWER_OF_TWO ) -return is_power_of_two (div); +return is_power_of_2 (div);if (divider->table)return _is_valid_table_div (divider->table, div);return true;
Signature · 1 line
1.7.10.4
Quoted 9 lines · Mike Turquette
Quoting Joe Perches (2013-01-15 10:31:05)
Quoted 8 lines · James Hogan
Quoted 2 lines
The macro
is_power_of_two()inclk-divider.cwas defined as !(i & ~i) which is always true. Correct it to !(i & (i - 1)).[]
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0[]
drivers/clk/clk-divider.c
Quoted 1 line
drivers/clk/clk-divider.c +0
-0@@ -29,8 +29,8 @@[]
Quoted 8 lines
Quoted 6 lines
drivers/clk/clk-divider.c
Quoted 4 lines
drivers/clk/clk-divider.c +2
-2-#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1) +#define is_power_of_two (i) (!((i) & ((i) - 1)))Use
is_power_of_2inlog2.hinstead?Both are fine suggestions. How about I apply the below patch?
Looks good to me, thanks for taking care of the log.h change.
Cheers
James
Quoted 51 lines
From
54c6c9e5f61693effbe6e4fb28ef9f2f9b3d7a23Mon Sep 17 00:00:00 2001
From: James Hogan <[email protected]>
Date: Tue, 15 Jan 2013 10:28:05 +0000
Subject: [PATCH] clk-divider: fix macrosThe macro
is_power_of_two()inclk-divider.cwas defined as !(i & ~i) which is always true. Instead useis_power_of_2()fromlog2.h.Also add brackets around the macro arguments in
div_maskto avoid any future operator precedence problems.Signed-off-by James Hogan <[email protected]>Cc Joe Perches <[email protected]>Signed-off-by Mike Turquette <[email protected]>[mturquette at linaro.org: use log2.h per Joe Perches; update changelog]--- drivers/clk/clk-divider.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/clk/clk-divider.c +3
-3@@ -16,6 +16,7 @@#include <linux/io.h> #include <linux/err.h> #include <linux/string.h> +#include <linux/log2.h> /* * DOC: basic adjustable divider clock that cannot gate @@ -29,8 +30,7 @@#define to_clk_divider (_hw) container_of(_hw, struct clk_divider, hw) -#define div_mask (d) ((1 << (d->width)) - 1) -#define is_power_of_two (i) !(i & ~i) +#define div_mask (d) ((1 << ((d)->width)) - 1)static unsigned int _get_table_maxdiv (const struct clk_div_table *table) {@@ -137,7 +137,7 @@ static bool _is_valid_table_div(const struct clk_div_table *table,static bool _is_valid_div (struct clk_divider *divider,unsigned int div) {if (divider->flags &CLK_DIVIDER_POWER_OF_TWO ) -return is_power_of_two (div); +return is_power_of_2 (div);if (divider->table)return _is_valid_table_div (divider->table, div);return true;