]> git.sur5r.net Git - cc65/commitdiff
New shift module, comment fixes
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 11 Nov 2003 09:41:07 +0000 (09:41 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 11 Nov 2003 09:41:07 +0000 (09:41 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@2636 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/common/addrsize.c
src/common/addrsize.h
src/common/make/gcc.mak
src/common/make/watcom.mak
src/common/shift.c [new file with mode: 0644]
src/common/shift.h [new file with mode: 0644]

index 845cb0e75e04d3d1358860727b0861442dfc0be2..278f5745b2a392d7f8320e7125abfca3bcc10a19 100644 (file)
@@ -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                                                 */
index 86ed732e933a5be1be8c9c455863832a78cee6d2..62cc7fde15cb3a32282144c01443d8c663b41155 100644 (file)
@@ -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                                                 */
index d178f44bbb47429d63e0ec77e625f4a1a6b9bc5d..2bf80901a3f28219cbc6dccabd20eee55817d10c 100644 (file)
@@ -29,6 +29,7 @@ OBJS =        abend.o         \
         searchpath.o    \
         segdefs.o       \
         segnames.o      \
+        shift.o         \
        strbuf.o        \
         strpool.o       \
        strutil.o       \
index 72adf688720698440053d8dad68caca7125b324f..d33082cc81920e42628ba12ad466eef1b84858d6 100644 (file)
@@ -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 (file)
index 0000000..176a68c
--- /dev/null
@@ -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 <limits.h>
+
+/* 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 (file)
index 0000000..c3f3693
--- /dev/null
@@ -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 <limits.h>
+
+
+
+/*****************************************************************************/
+/*                                          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
+
+
+