This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ANSI-C headers in test snippets to please g++ (and the rest)
[metaconfig.git] / U / perl / d_u32align.U
1 ?RCS: $Id$
2 ?RCS:
3 ?RCS: Copyright (c) 2001, Jarkko Hietaniemi
4 ?RCS: 
5 ?RCS: You may redistribute only under the terms of the Artistic Licence,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic Licence; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
10 ?RCS:
11 ?MAKE:d_u32align: Compile cat rm Oldconfig run i_stdlib \
12         byteorder u32type Myread Setvar u8type signal_t
13 ?MAKE:  -pick add $@ %<
14 ?S:d_u32align:
15 ?S:     This variable tells whether you must access character data
16 ?S:     through U32-aligned pointers.
17 ?S:.
18 ?C:U32_ALIGNMENT_REQUIRED:
19 ?C:     This symbol, if defined, indicates that you must access
20 ?C:     character data through U32-aligned pointers.
21 ?C:.
22 ?H:?%<:#ifndef U32_ALIGNMENT_REQUIRED
23 ?H:?%<:#$d_u32align U32_ALIGNMENT_REQUIRED      /**/
24 ?H:?%<:#endif
25 ?H:.
26 $cat <<EOM
27
28 Checking to see whether you can access character data unalignedly...
29 EOM
30 case "$d_u32align" in
31 '')   $cat >try.c <<EOCP
32 #include <stdio.h>
33 #$i_stdlib I_STDLIB
34 #ifdef I_STDLIB
35 #include <stdlib.h>
36 #endif
37 #define U32 $u32type
38 #define BYTEORDER 0x$byteorder
39 #define U8 $u8type
40 #include <signal.h>
41 #ifdef SIGBUS
42 $signal_t bletch(int s) { exit(4); }
43 #endif
44 int main() {
45 #if BYTEORDER == 0x1234 || BYTEORDER == 0x4321
46     U8 buf[8];
47     U32 *up;
48     int i;
49
50     if (sizeof(U32) != 4) {
51         printf("sizeof(U32) is not 4, but %d\n", sizeof(U32));
52         exit(1);
53     }
54
55     fflush(stdout);
56
57 #ifdef SIGBUS
58     signal(SIGBUS, bletch);
59 #endif
60
61     buf[0] = 0;
62     buf[1] = 0;
63     buf[2] = 0;
64     buf[3] = 1;
65     buf[4] = 0;
66     buf[5] = 0;
67     buf[6] = 0;
68     buf[7] = 1;
69
70     for (i = 0; i < 4; i++) {
71         up = (U32*)(buf + i);
72         if (! ((*up == 1 << (8*i)) ||   /* big-endian */
73                (*up == 1 << (8*(3-i)))  /* little-endian */
74               )
75            )
76         {
77             printf("read failed (%x)\n", *up);
78             exit(2);
79         }
80     }
81
82     /* write test */
83     for (i = 0; i < 4; i++) {
84         up = (U32*)(buf + i);
85         *up = 0xBeef;
86         if (*up != 0xBeef) {
87             printf("write failed (%x)\n", *up);
88             exit(3);
89         }
90     }
91
92     exit(0);
93 #else
94     printf("1\n");
95     exit(1);
96 #endif
97     return 0;
98 }
99 EOCP
100 set try
101 if eval $compile_ok; then
102         echo "(Testing for character data alignment may crash the test.  That's okay.)" >&4
103         $run ./try 2>&1 >/dev/null
104         case "$?" in
105         0)      cat >&4 <<EOM
106 You can access character data pretty unalignedly.
107 EOM
108                 d_u32align="$undef"
109                 ;;
110         *)      cat >&4 <<EOM
111 It seems that you must access character data in an aligned manner.
112 EOM
113                 d_u32align="$define"
114                 ;;
115         esac
116 else
117         rp='Can you access character data at unaligned addresses?'
118         dflt='n'
119         . ./myread
120         case "$ans" in
121         [yY]*)  d_u32align="$undef"  ;;
122         *)      d_u32align="$define" ;;
123         esac
124 fi
125 $rm -f core core.try.* try.core
126 ;;
127 esac
128