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