* gcc.dg/duff-1.c: New test.
* gcc.dg/duff-2.c: New test.
* gcc.dg/duff-3.c: New test.
From-SVN: r59240
+2002-11-19 Jason Thorpe <thorpej@wasabisystems.com>
+
+ * gcc.dg/duff-1.c: New test.
+ * gcc.dg/duff-2.c: New test.
+ * gcc.dg/duff-3.c: New test.
+
2002-11-18 Neil Booth <neil@daikokuya.co.uk>
* gcc.dg/cpp/_Pragma5.c: New test.
--- /dev/null
+/* Duff's device is legal C; test to make sure the compiler
+ doesn't complain about it.
+
+ Jason Thorpe <thorpej@wasabisystems.com>
+ Derived from PR 3846. */
+
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+extern void exit (int);
+
+typedef __SIZE_TYPE__ size_t;
+extern int memcmp (const void *, const void *, size_t);
+
+void
+duffcpy (char *dst, const char *src, unsigned long size)
+{
+ switch (size & 3)
+ {
+ for (;;)
+ {
+ *dst++ = *src++;
+ case 3:
+ *dst++ = *src++;
+ case 2:
+ *dst++ = *src++;
+ case 1:
+ *dst++ = *src++;
+ case 0:
+ if (size <= 3)
+ break;
+ size -= 4;
+ }
+ }
+}
+
+const char testpat[] = "The quick brown fox jumped over the lazy dog.";
+
+int
+main()
+{
+ char buf[64];
+
+ duffcpy (buf, testpat, sizeof (testpat));
+ if (memcmp (buf, testpat, sizeof (testpat)) != 0)
+ abort ();
+
+ exit (0);
+}
--- /dev/null
+/* Duff's device is legal C; test to make sure the compiler
+ doesn't complain about it.
+
+ Jason Thorpe <thorpej@wasabisystems.com>
+ Derived from the BSD Telnet Kerberos 4 checksum routine.
+ See also PR 5230. */
+
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+extern void exit (int);
+
+#if __INT_MAX__ >= 2147483647
+/* At least 32-bit integers. */
+typedef int type32;
+#else
+typedef long type32;
+#endif
+
+type32
+cksum (const unsigned char *src, unsigned long size)
+{
+ type32 ck = 0;
+
+ switch (size & 3)
+ {
+ while (size > 0)
+ {
+ case 0:
+ ck ^= (type32)*src++ << 24;
+ --size;
+ case 3:
+ ck ^= (type32)*src++ << 16;
+ --size;
+ case 2:
+ ck ^= (type32)*src++ << 8;
+ --size;
+ case 1:
+ ck ^= (type32)*src++;
+ --size;
+ }
+ }
+
+ return ck;
+}
+
+const char testpat[] = "The quick brown fox jumped over the lazy dog.";
+
+int
+main()
+{
+ type32 ck;
+
+ ck = cksum ((const unsigned char *) testpat, sizeof (testpat));
+ if (ck != 925902908)
+ abort ();
+
+ exit (0);
+}
--- /dev/null
+/* Duff's device is legal C; test to make sure the compiler
+ doesn't complain about it.
+
+ Jason Thorpe <thorpej@wasabisystems.com>
+ Derived from Tom Duff's original usenet message about the device. */
+
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+extern void abort (void);
+extern void exit (int);
+
+typedef __SIZE_TYPE__ size_t;
+extern int memcmp (const void *, const void *, size_t);
+
+void
+duffcpy (char *dst, const char *src, unsigned long size)
+{
+ unsigned long n = (size + 7) / 8;
+
+ switch (size % 8)
+ {
+ case 0: do { *dst++ = *src++;
+ case 7: *dst++ = *src++;
+ case 6: *dst++ = *src++;
+ case 5: *dst++ = *src++;
+ case 4: *dst++ = *src++;
+ case 3: *dst++ = *src++;
+ case 2: *dst++ = *src++;
+ case 1: *dst++ = *src++;
+ } while (--n > 0);
+ }
+}
+
+const char testpat[] = "The quick brown fox jumped over the lazy dog.";
+
+int
+main()
+{
+ char buf[64];
+
+ duffcpy (buf, testpat, sizeof (testpat));
+ if (memcmp (buf, testpat, sizeof (testpat)) != 0)
+ abort ();
+
+ exit (0);
+}