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