<linux/bcd.h> space reduction
PATCH 1/1 <linux/bcd.h> space reduction
This updates <linux/bcd.h> to define the key routines as static constant functions, which the macros will then call.
In effect this lets each user of these BCD routines shrink their codespace, when GCC observes that's a win, by using N function calls and one copy of the routines, instead of N inlined copies of these constant functions.
These routines aren't used in speed-critical code. Almost all callers are from the RTC framework. Typical per-driver savings top 200 bytes. On one RTC driver that's almost a 20% reduction in runtime I-space footprint; for many, closer to 10%; for rtc-cmos it's about 4%.
--- include/linux/bcd.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
include/linux/bcd.h +14 -2
@@ -10,8 +10,20 @@#ifndef _BCD_H#define _BCD_H -#define BCD2BIN (val) (((val) & 0x0f) + ((val)>>4)*10) -#define BIN2BCD (val) ((((val)/10)<<4) + (val)%10) +static int __bcd2bin(u8 val) __attribute_const__ __maybe_unused; +static int __bcd2bin (u8 val) +{ +return (val &0x0f ) + (val >>4 ) *10 ; +} + +static u8 __bin2bcd(unsigned val) __attribute_const__ __maybe_unused; +static u8 __bin2bcd (unsigned val) +{ +return ((val/10 ) <<4 ) + val %10 ; +} + +#define BCD2BIN (val) __bcd2bin(val) +#define BIN2BCD (val) __bin2bcd(val)/* backwards compat */ #define BCD_TO_BIN (val) ((val)=BCD2BIN(val))
Quoted 8 lines · David Brownell
This updates <
linux/bcd.h> to define the key routines as static constant functions, which the macros will then call.In effect this lets each user of these BCD routines shrink their codespace, when GCC observes that's a win, by using N function calls and one copy of the routines, instead of N inlined copies of these constant functions.
Quoted 4 lines
These routines aren't used in speed-critical code. Almost all callers are from the RTC framework. Typical per-driver savings top 200 bytes. On one RTC driver that's almost a 20% reduction in runtime I-space footprint; for many, closer to 10%; for rtc-cmos it's about 4%.
The patch breaks the compilation:
<-- snip -->
...
CC drivers/rtc/rtc-ds1511.o
In file included from /home/bunk/linux/kernel-2.6/git/linux-2.6/drivers/rtc/rtc-ds1511.c:17:/home/bunk/linux/kernel-2.6/git/linux-2.6/include/linux/bcd.h:13: error: expected ‘)’ before ‘val’
...
make[3]: *** [drivers/rtc/rtc-ds1511.o] Error 1
<-- snip -->
Additionally, I'd suggest to make them out-of-line functions instead of shipping multiple copies in different object files.
Quoted 20 lines
Signed-off-by David Brownell <[email protected]>--- include/linux/bcd.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)include/linux/bcd.h +7
-2@@ -10,8 +10,20 @@#ifndef _BCD_H#define _BCD_H -#define BCD2BIN (val) (((val) & 0x0f) + ((val)>>4)*10) -#define BIN2BCD (val) ((((val)/10)<<4) + (val)%10) +static int __bcd2bin(u8 val) __attribute_const__ __maybe_unused; +static int __bcd2bin (u8 val) +{ +return (val &0x0f ) + (val >>4 ) *10 ; +} + +static u8 __bin2bcd(unsigned val) __attribute_const__ __maybe_unused;
include/linux/bcd.h line 29
Quoted 10 lines
include/linux/bcd.h +7
-0+static u8 __bin2bcd (unsigned val) +{ +return ((val/10 ) <<4 ) + val %10 ; +} + +#define BCD2BIN (val) __bcd2bin(val) +#define BIN2BCD (val) __bin2bcd(val)/* backwards compat */ #define BCD_TO_BIN (val) ((val)=BCD2BIN(val))
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 7 lines · Adrian Bunk
Quoted 4 lines · David Brownell
This updates <
linux/bcd.h> to define the key routines as static constant functions, which the macros will then call.... Typical per-driver savings top 200 bytes. ...
The patch breaks the compilation:
Needs to #include <linux/types.h> then; "u8" isn't always known. Or maybe just spell it out "unsigned char".
Quoted 4 lines
...Additionally, I'd suggest to make them out-of-line functions instead of shipping multiple copies in different object files.
That was one of the options. I thought I'd start with something simpler, and see what folk thought.
Where would that be ... lib/bcd.c, always linked? And if it goes that way I'd prefer bcd2bin() and bin2bcd(), not these names. A small impetus to have LESS SOURCE CODE SHOUTING AT ME.
- Dave
Quoted 15 lines · David Brownell
Quoted 7 lines · Adrian Bunk
Quoted 4 lines · David Brownell
This updates <
linux/bcd.h> to define the key routines as static constant functions, which the macros will then call.... Typical per-driver savings top 200 bytes. ...
The patch breaks the compilation:
Needs to #include <
linux/types.h> then; "u8" isn't always known. Or maybe just spell it out "unsigned char".Quoted 2 lines
...
Quoted 7 lines
Quoted 2 lines
Additionally, I'd suggest to make them out-of-line functions instead of shipping multiple copies in different object files.
That was one of the options. I thought I'd start with something simpler, and see what folk thought.
Where would that be ...
lib/bcd.c, always linked?
Sounds good.
Quoted 3 lines
And if it goes
that way I'd preferbcd2bin()andbin2bcd(), not these names. A small impetus to have LESS SOURCE CODE SHOUTING AT ME.
Sounds good, but start with SHOUTING COMPATIBILITY #DEFINES FOR THE EXISTING USERS and either clean them up later or let me do the cleanup.
Quoted 1 line
- Dave
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 11 lines · Adrian Bunk
Quoted 1 line · David Brownell
Where would that be ...
lib/bcd.c, always linked?Sounds good.
Quoted 3 lines
And if it goes
that way I'd preferbcd2bin()andbin2bcd(), not these names. A small impetus to have LESS SOURCE CODE SHOUTING AT ME.Sounds good, but start with SHOUTING COMPATIBILITY #DEFINES FOR THE EXISTING USERS and either clean them up later or let me do the cleanup.
Like this then (against RC8)?
========= CUT HERE
This updates <linux/bcd.h> to define the key routines as constant functions, which the macros will then call. Newer code can now call bcd2bin() instead of SCREAMING BCD2BIN() TO THE FOUR WINDS.
This lets each driver shrink their codespace by using N function calls to a single (global) copy of those routines, instead of N inlined copies of these functions per driver.
These routines aren't used in speed-critical code. Almost all callers are in the RTC framework. Typical per-driver savings is near 300 bytes.
--- include/linux/bcd.h | 9 +++++++-- lib/Makefile | 2 +- lib/bcd.c | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-)
include/linux/bcd.h +7 -2
@@ -10,8 +10,13 @@#ifndef _BCD_H#define _BCD_H -#define BCD2BIN (val) (((val) & 0x0f) + ((val)>>4)*10) -#define BIN2BCD (val) ((((val)/10)<<4) + (val)%10) +#include <linux/compiler.h> + +unsigned bcd2bin(unsigned char val) __attribute_const__; +unsigned char bin2bcd(unsigned val) __attribute_const__; + +#define BCD2BIN (val) bcd2bin(val) +#define BIN2BCD (val) bin2bcd(val)/* backwards compat */ #define BCD_TO_BIN (val) ((val)=BCD2BIN(val))
lib/Makefile +1 -1
@@ -13,7 +13,7 @@ lib-$(CONFIG_SMP) += cpumask.o lib-y += kobject.o kref.o klist.o -obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ +obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o ifeq ($(CONFIG_DEBUG_KOBJECT),y)
lib/bcd.c new file +14 -0
@@ -0,0 +1,14 @@ +#include <linux/bcd.h> +#include <linux/module.h> + +unsigned bcd2bin (unsigned char val) +{ +return (val &0x0f ) + (val >>4 ) *10 ; +} +EXPORT_SYMBOL (bcd2bin); + +unsigned char bin2bcd (unsigned val) +{ +return ((val /10 ) <<4 ) + val %10 ; +} +EXPORT_SYMBOL (bin2bcd);
Quoted 10 lines · David Brownell
Quoted 9 lines · Adrian Bunk
Quoted 1 line · David Brownell
Where would that be ...
lib/bcd.c, always linked?Sounds good.
Quoted 3 lines
And if it goes
that way I'd preferbcd2bin()andbin2bcd(), not these names. A small impetus to have LESS SOURCE CODE SHOUTING AT ME.
Quoted 5 lines
Quoted 2 lines
Sounds good, but start with SHOUTING COMPATIBILITY #DEFINES FOR THE EXISTING USERS and either clean them up later or let me do the cleanup.
Like this then (against RC8)?
...
Looks good.
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon Seed