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