Provide BCD<->binary conversion in libkern and turn <dev/clock_subr.h>'s

FROMBCD()/TOBCD() macros into wrappers around it, resulting in both
smaller code footprint and elimination of possible issues due to
multiple evaluation of macro arguments.

Suggested by Simon Burge and Anders Gavare on tech-kern.
This commit is contained in:
kleink 2006-03-11 15:40:07 +00:00
parent c66cd10ee8
commit 31930d4de5
6 changed files with 40 additions and 45 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock.c,v 1.7 2005/12/24 20:06:47 perry Exp $ */
/* $NetBSD: clock.c,v 1.8 2006/03/11 15:40:07 kleink Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -121,7 +121,7 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.7 2005/12/24 20:06:47 perry Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.8 2006/03/11 15:40:07 kleink Exp $");
/* #define CLOCKDEBUG */
/* #define CLOCK_PARANOIA */
@ -184,8 +184,6 @@ void sysbeep __P((int, int));
void rtcinit __P((void));
int rtcget __P((mc_todregs *));
void rtcput __P((mc_todregs *));
int bcdtobin __P((int));
int bintobcd __P((int));
static inline int gettick_broken_latch __P((void));
@ -647,22 +645,6 @@ rtcput(regs)
MC146818_PUTTOD(NULL, regs); /* XXX softc */
}
int
bcdtobin(n)
int n;
{
return (((n >> 4) & 0x0f) * 10 + (n & 0x0f));
}
int
bintobcd(n)
int n;
{
return ((u_char)(((n / 10) << 4) & 0xf0) | ((n % 10) & 0x0f));
}
static int timeset;
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock.c,v 1.90 2006/02/19 14:59:22 thorpej Exp $ */
/* $NetBSD: clock.c,v 1.91 2006/03/11 15:40:07 kleink Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -121,7 +121,7 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.90 2006/02/19 14:59:22 thorpej Exp $");
__KERNEL_RCSID(0, "$NetBSD: clock.c,v 1.91 2006/03/11 15:40:07 kleink Exp $");
/* #define CLOCKDEBUG */
/* #define CLOCK_PARANOIA */
@ -183,8 +183,6 @@ void sysbeep(int, int);
void rtcinit(void);
int rtcget(mc_todregs *);
void rtcput(mc_todregs *);
int bcdtobin(int);
int bintobcd(int);
static int cmoscheck(void);
@ -652,20 +650,6 @@ rtcput(mc_todregs *regs)
MC146818_PUTTOD(NULL, regs); /* XXX softc */
}
int
bcdtobin(int n)
{
return (((n >> 4) & 0x0f) * 10 + (n & 0x0f));
}
int
bintobcd(int n)
{
return ((u_char)(((n / 10) << 4) & 0xf0) | ((n % 10) & 0x0f));
}
static int timeset;
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: clock_subr.h,v 1.13 2005/12/11 12:20:53 christos Exp $ */
/* $NetBSD: clock_subr.h,v 1.14 2006/03/11 15:40:07 kleink Exp $ */
/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
@ -56,10 +56,10 @@ time_t clock_ymdhms_to_secs(struct clock_ymdhms *);
void clock_secs_to_ymdhms(time_t, struct clock_ymdhms *);
/*
* BCD to decimal and decimal to BCD.
* BCD to binary and binary to BCD.
*/
#define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
#define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
#define FROMBCD(x) bcdtobin((x))
#define TOBCD(x) bintobcd((x))
/* Some handy constants. */
#define SECDAY (24 * 60 * 60)

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.77 2005/12/20 19:35:26 christos Exp $
# $NetBSD: Makefile,v 1.78 2006/03/11 15:40:07 kleink Exp $
LIB= kern
NOPIC= # defined
@ -43,7 +43,7 @@ SRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \
# Other stuff
SRCS+= __cmsg_alignbytes.c inet_addr.c intoa.c md4c.c md5c.c sha1.c pmatch.c
SRCS+= _que.c arc4random.c mcount.c
SRCS+= _que.c arc4random.c bcd.c mcount.c
SRCS+= strstr.c strlcpy.c strlcat.c

25
sys/lib/libkern/bcd.c Normal file
View File

@ -0,0 +1,25 @@
/* $NetBSD: bcd.c,v 1.1 2006/03/11 15:40:07 kleink Exp $ */
/*
* Convert a single byte between (unsigned) packed bcd and binary.
* Public domain.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0,"$NetBSD: bcd.c,v 1.1 2006/03/11 15:40:07 kleink Exp $");
#include <lib/libkern/libkern.h>
unsigned int
bcdtobin(unsigned int bcd)
{
return (((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f));
}
unsigned int
bintobcd(unsigned int bin)
{
return ((((bin / 10) << 4) & 0xf0) | (bin % 10));
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: libkern.h,v 1.57 2006/03/08 08:26:51 dyoung Exp $ */
/* $NetBSD: libkern.h,v 1.58 2006/03/11 15:40:07 kleink Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -290,6 +290,10 @@ int ffs __P((int));
void __assert __P((const char *, const char *, int, const char *))
__attribute__((__noreturn__));
unsigned int
bcdtobin __P((unsigned int));
unsigned int
bintobcd __P((unsigned int));
u_int32_t
inet_addr __P((const char *));
struct in_addr;