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