This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge pull request #79 from neilb/taintsupport
[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 License,
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 License; 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_try 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 ?F:!try
27 : Checking 32bit alignedness
28 $cat <<EOM
29
30 Checking to see whether you can access character data unalignedly...
31 EOM
32 case "$d_u32align" in
33 '')   $cat >try.c <<EOCP
34 #include <stdio.h>
35 #$i_stdlib I_STDLIB
36 #ifdef I_STDLIB
37 #include <stdlib.h>
38 #endif
39 #define U32 $u32type
40 #define BYTEORDER 0x$byteorder
41 #define U8 $u8type
42 #include <signal.h>
43 #ifdef SIGBUS
44 $signal_t bletch(int s) { exit(4); }
45 #endif
46 int main() {
47 #if BYTEORDER == 0x1234 || BYTEORDER == 0x4321
48     volatile U8 buf[8];
49     volatile U32 *up;
50     int i;
51
52     if (sizeof(U32) != 4) {
53         printf("sizeof(U32) is not 4, but %d\n", sizeof(U32));
54         exit(1);
55     }
56
57     fflush(stdout);
58
59 #ifdef SIGBUS
60     signal(SIGBUS, bletch);
61 #endif
62
63     buf[0] = 0;
64     buf[1] = 0;
65     buf[2] = 0;
66     buf[3] = 1;
67     buf[4] = 0;
68     buf[5] = 0;
69     buf[6] = 0;
70     buf[7] = 1;
71
72     for (i = 0; i < 4; i++) {
73         up = (U32*)(buf + i);
74         if (! ((*up == 1 << (8*i)) ||   /* big-endian */
75                (*up == 1 << (8*(3-i)))  /* little-endian */
76               )
77            )
78         {
79             printf("read failed (%x)\n", *up);
80             exit(2);
81         }
82     }
83
84     /* write test */
85     for (i = 0; i < 4; i++) {
86         up = (U32*)(buf + i);
87         *up = 0xBeef;
88         if (*up != 0xBeef) {
89             printf("write failed (%x)\n", *up);
90             exit(3);
91         }
92     }
93
94     exit(0);
95 #else
96     printf("1\n");
97     exit(1);
98 #endif
99     return 0;
100 }
101 EOCP
102 set try
103 if eval $compile_ok; then
104         echo "(Testing for character data alignment may crash the test.  That's okay.)" >&4
105         $run ./try 2>&1 >/dev/null
106         case "$?" in
107         0)      cat >&4 <<EOM
108 You can access character data pretty unalignedly.
109 EOM
110                 d_u32align="$undef"
111                 ;;
112         *)      cat >&4 <<EOM
113 It seems that you must access character data in an aligned manner.
114 EOM
115                 d_u32align="$define"
116                 ;;
117         esac
118 else
119         rp='Can you access character data at unaligned addresses?'
120         dflt='n'
121         . ./myread
122         case "$ans" in
123         [yY]*)  d_u32align="$undef"  ;;
124         *)      d_u32align="$define" ;;
125         esac
126 fi
127 $rm_try
128 ;;
129 esac
130