2008-06-23 02:55:57

by David Brownell

[permalink] [raw]
Subject: [patch 2.6.26-rc7] <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%.

Signed-off-by: David Brownell <[email protected]>
---
include/linux/bcd.h | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)

--- a/include/linux/bcd.h 2008-06-21 22:26:14.000000000 -0700
+++ b/include/linux/bcd.h 2008-06-21 22:37:48.000000000 -0700
@@ -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))


2008-06-23 06:52:39

by Adrian Bunk

[permalink] [raw]
Subject: Re: [patch 2.6.26-rc7] <linux/bcd.h> space reduction

On Sun, Jun 22, 2008 at 07:54:36PM -0700, David Brownell wrote:
> 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%.

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.

> Signed-off-by: David Brownell <[email protected]>
> ---
> include/linux/bcd.h | 16 ++++++++++++++--
> 1 file changed, 14 insertions(+), 2 deletions(-)
>
> --- a/include/linux/bcd.h 2008-06-21 22:26:14.000000000 -0700
> +++ b/include/linux/bcd.h 2008-06-21 22:37:48.000000000 -0700
> @@ -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))

cu
Adrian

--

"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

2008-06-23 07:16:01

by David Brownell

[permalink] [raw]
Subject: Re: [patch 2.6.26-rc7] <linux/bcd.h> space reduction

On Sunday 22 June 2008, Adrian Bunk wrote:
> On Sun, Jun 22, 2008 at 07:54:36PM -0700, David Brownell wrote:
> > 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".


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

2008-06-24 21:56:49

by Adrian Bunk

[permalink] [raw]
Subject: Re: [patch 2.6.26-rc7] <linux/bcd.h> space reduction

On Mon, Jun 23, 2008 at 12:15:49AM -0700, David Brownell wrote:
> On Sunday 22 June 2008, Adrian Bunk wrote:
> > On Sun, Jun 22, 2008 at 07:54:36PM -0700, David Brownell wrote:
> > > 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".
>
>
> > ...
> >
> > 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.

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

Sounds good, but start with SHOUTING COMPATIBILITY #DEFINES FOR THE
EXISTING USERS and either clean them up later or let me do the cleanup.

> - Dave

cu
Adrian

--

"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

2008-06-30 00:41:40

by David Brownell

[permalink] [raw]
Subject: Re: [patch 2.6.26-rc7] <linux/bcd.h> space reduction

On Tuesday 24 June 2008, Adrian Bunk wrote:
> On Mon, Jun 23, 2008 at 12:15:49AM -0700, David Brownell wrote:
> > Where would that be ... lib/bcd.c, always linked?
>
> Sounds good.
>
> > 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.
>
> 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.

Signed-off-by: David Brownell <[email protected]>
---
include/linux/bcd.h | 9 +++++++--
lib/Makefile | 2 +-
lib/bcd.c | 14 ++++++++++++++
3 files changed, 22 insertions(+), 3 deletions(-)

--- a/include/linux/bcd.h 2008-06-28 22:21:37.000000000 -0700
+++ b/include/linux/bcd.h 2008-06-29 15:16:48.000000000 -0700
@@ -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))
--- a/lib/Makefile 2008-06-29 15:17:22.000000000 -0700
+++ b/lib/Makefile 2008-06-29 15:21:44.000000000 -0700
@@ -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)
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ b/lib/bcd.c 2008-06-29 15:27:31.000000000 -0700
@@ -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);

2008-06-30 18:21:25

by Adrian Bunk

[permalink] [raw]
Subject: Re: [patch 2.6.26-rc7] <linux/bcd.h> space reduction

On Sun, Jun 29, 2008 at 05:41:23PM -0700, David Brownell wrote:
> On Tuesday 24 June 2008, Adrian Bunk wrote:
> > On Mon, Jun 23, 2008 at 12:15:49AM -0700, David Brownell wrote:
> > > Where would that be ... lib/bcd.c, always linked?
> >
> > Sounds good.
> >
> > > 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.
> >
> > 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

--

"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