hpet: Optimize local variable data type in hpet_alloc()
PATCH 1/1 hpet: Optimize local variable data type in hpet_alloc()
The local variable period uses at most 32 bits and can be a u32 instead of unsigned long. The upper 32 bits are all 0 after masking and right shifting cap by HPET_COUNTER_CLK_PERIOD_SHIFT and can be discarded.
Since do_div() casts the divisor to u32 anyway, changing the data type of period to u32 also removes the following Coccinelle/coccicheck warning reported by do_div.cocci:
WARNING: do_div() does a 64-by-32 division, please consider using div64_ul instead
--- drivers/char/hpet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/hpet.c +1 -1
@@ -784,7 +784,7 @@ int hpet_alloc(struct hpet_data *hdp)struct hpets *hpetp;struct hpet __iomem *hpet;static struct hpets *last; -unsigned long period; +u32 period;unsigned long long temp;u32 remainder;
Signature · 1 line
2.45.2
Quoted 4 lines · Thorsten Blum
The local variable period uses at most 32 bits and can be a u32 instead of unsigned long. The upper 32 bits are all 0 after masking and right shifting cap by
HPET_COUNTER_CLK_PERIOD_SHIFTand can be discarded.
Quoted 8 lines
Since
do_div()casts the divisor to u32 anyway, changing the data type of period to u32 also removes the following Coccinelle/coccicheck warning reported bydo_div.cocci:WARNING:
do_div()does a 64-by-32 division, please consider usingdiv64_ulinsteadSigned-off-by Thorsten Blum <[email protected]>
Quoted 9 lines
--- drivers/char/hpet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/hpet.c +0
-0
drivers/char/hpet.c int hpet_alloc(struct hpet_data *hdp) · line 788
Quoted 6 lines
drivers/char/hpet.c +1
-1@@ -784,7 +784,7 @@ int hpet_alloc(struct hpet_data *hdp)struct hpets *hpetp;struct hpet __iomem *hpet;static struct hpets *last; -unsigned long period; +u32 period;unsigned long long temp;
The change is sufficient to address the issue. I had another look at this and came up with a different approach that would also help readability:
drivers/char/hpet.c +3 -4
@@ -855,10 +855,9 @@ int hpet_alloc(struct hpet_data *hdp) period = (cap &HPET_COUNTER_CLK_PERIOD_MASK ) >>HPET_COUNTER_CLK_PERIOD_SHIFT ;/* fs, 10^-15 */ - temp =1000000000000000uLL ;/* 10^15 femtoseconds per second */ - temp += period >>1 ;/* round */ -do_div (temp, period); - hpetp->hp_tick_freq = temp;/* ticks per second */ + +/* femtosecond period to ticks per second */ + hpetp->hp_tick_freq =div64_round (1000000000000000uLL , period); printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s", hpetp->hp_which, hdp->hd_phys_address,
This would also work by itself, or we could do both. Clemens, any opinion?
Arnd