NetBSD/sys/lib/libkern/bcd.c
kleink 31930d4de5 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.
2006-03-11 15:40:07 +00:00

26 lines
487 B
C

/* $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));
}