From: cuz Date: Tue, 11 Nov 2003 09:41:07 +0000 (+0000) Subject: New shift module, comment fixes X-Git-Tag: V2.12.0~1164 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=7e9b389428207882b499c4a7303fb2ae428b5c96;p=cc65 New shift module, comment fixes git-svn-id: svn://svn.cc65.org/cc65/trunk@2636 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/common/addrsize.c b/src/common/addrsize.c index 845cb0e75..278f5745b 100644 --- a/src/common/addrsize.c +++ b/src/common/addrsize.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2003 Ullrich von Bassewitz */ +/* (C) 2003 Ullrich von Bassewitz */ /* Römerstraße 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ diff --git a/src/common/addrsize.h b/src/common/addrsize.h index 86ed732e9..62cc7fde1 100644 --- a/src/common/addrsize.h +++ b/src/common/addrsize.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2003 Ullrich von Bassewitz */ +/* (C) 2003 Ullrich von Bassewitz */ /* Römerstraße 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ diff --git a/src/common/make/gcc.mak b/src/common/make/gcc.mak index d178f44bb..2bf80901a 100644 --- a/src/common/make/gcc.mak +++ b/src/common/make/gcc.mak @@ -29,6 +29,7 @@ OBJS = abend.o \ searchpath.o \ segdefs.o \ segnames.o \ + shift.o \ strbuf.o \ strpool.o \ strutil.o \ diff --git a/src/common/make/watcom.mak b/src/common/make/watcom.mak index 72adf6887..d33082cc8 100644 --- a/src/common/make/watcom.mak +++ b/src/common/make/watcom.mak @@ -74,6 +74,7 @@ OBJS = abend.obj \ searchpath.obj \ segdefs.obj \ segnames.obj \ + shift.obj \ strbuf.obj \ strpool.obj \ strutil.obj \ diff --git a/src/common/shift.c b/src/common/shift.c new file mode 100644 index 000000000..176a68c30 --- /dev/null +++ b/src/common/shift.c @@ -0,0 +1,130 @@ +/*****************************************************************************/ +/* */ +/* shift.c */ +/* */ +/* Safe shift routines */ +/* */ +/* */ +/* */ +/* (C) 2003 Ullrich von Bassewitz */ +/* Römerstraße 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +/* According to the C standard, shifting a data type by the number of bits it + * has causes undefined behaviour. So + * + * unsigned long l = 1; + * unsigned u =32; + * l <<= u; + * + * maybe illegal. The functions in this module behave safely in this respect, + * and they use proper casting to distinguish signed from unsigned shifts. + * They are not a general purpose replacement for the shift operator! + */ + + + +#include + +/* common */ +#include "shift.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +long asl_l (long l, unsigned count) +/* Arithmetic shift left l by count. */ +{ + while (1) { + if (count >= CHAR_BIT * sizeof (l)) { + l <<= (CHAR_BIT * sizeof (l) - 1); + count -= (CHAR_BIT * sizeof (l) - 1); + } else { + l <<= count; + break; + } + } + return l; +} + + + +long asr_l (long l, unsigned count) +/* Arithmetic shift right l by count */ +{ + while (1) { + if (count >= CHAR_BIT * sizeof (l)) { + l >>= (CHAR_BIT * sizeof (l) - 1); + count -= (CHAR_BIT * sizeof (l) - 1); + } else { + l >>= count; + break; + } + } + return l; +} + + + +unsigned long shl_l (unsigned long l, unsigned count) +/* Logical shift left l by count */ +{ + while (1) { + if (count >= CHAR_BIT * sizeof (l)) { + l <<= (CHAR_BIT * sizeof (l) - 1); + count -= (CHAR_BIT * sizeof (l) - 1); + } else { + l <<= count; + break; + } + } + return l; +} + + + +unsigned long shr_l (unsigned long l, unsigned count) +/* Logical shift right l by count */ +{ + while (1) { + if (count >= CHAR_BIT * sizeof (l)) { + l >>= (CHAR_BIT * sizeof (l) - 1); + count -= (CHAR_BIT * sizeof (l) - 1); + } else { + l >>= count; + break; + } + } + return l; +} + + + diff --git a/src/common/shift.h b/src/common/shift.h new file mode 100644 index 000000000..c3f36930d --- /dev/null +++ b/src/common/shift.h @@ -0,0 +1,84 @@ +/*****************************************************************************/ +/* */ +/* shift.h */ +/* */ +/* Safe shift routines */ +/* */ +/* */ +/* */ +/* (C) 2003 Ullrich von Bassewitz */ +/* Römerstraße 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +/* According to the C standard, shifting a data type by the number of bits it + * has causes undefined behaviour. So + * + * unsigned long l = 1; + * unsigned u =32; + * l <<= u; + * + * maybe illegal. The functions in this module behave safely in this respect, + * and they use proper casting to distinguish signed from unsigned shifts. + * They are not a general purpose replacement for the shift operator! + */ + + + +#ifndef SHIFT_H +#define SHIFT_H + + + +#include + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +long asl_l (long l, unsigned count); +/* Arithmetic shift left l by count. */ + +long asr_l (long l, unsigned count); +/* Arithmetic shift right l by count */ + +unsigned long shl_l (unsigned long l, unsigned count); +/* Logical shift left l by count */ + +unsigned long shr_l (unsigned long l, unsigned count); +/* Logical shift right l by count */ + + + +/* End of shift.h */ + +#endif + + +