This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix .gitignore: only ignore 'perl' in root of repo, not U/perl subdir
[metaconfig.git] / U / perl / d_u32align.U
CommitLineData
d85168a5
JH
1?RCS: $Id$
2?RCS:
3?RCS: Copyright (c) 2001, Jarkko Hietaniemi
0065f4af 4?RCS:
65a32477 5?RCS: You may redistribute only under the terms of the Artistic License,
d85168a5
JH
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
65a32477 8?RCS: that same Artistic License; a copy of which may be found at the root
d85168a5
JH
9?RCS: of the source tree for dist 3.0.
10?RCS:
2cb64bf6 11?MAKE:d_u32align: Compile cat rm_try Oldconfig run i_stdlib \
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:.
98813c8a
JH
22?H:?%<:#ifndef U32_ALIGNMENT_REQUIRED
23?H:?%<:#$d_u32align U32_ALIGNMENT_REQUIRED /**/
24?H:?%<:#endif
d85168a5 25?H:.
0065f4af 26?F:!try
04c34a22 27: Checking 32bit alignedness
d85168a5
JH
28$cat <<EOM
29
30Checking to see whether you can access character data unalignedly...
31EOM
a0d5d9d4
JH
32case "$d_u32align" in
33'') $cat >try.c <<EOCP
d85168a5 34#include <stdio.h>
3b148aa9
JH
35#$i_stdlib I_STDLIB
36#ifdef I_STDLIB
37#include <stdlib.h>
38#endif
d85168a5 39#define U32 $u32type
99457da0
JH
40#define BYTEORDER 0x$byteorder
41#define U8 $u8type
9589ed74
JH
42#include <signal.h>
43#ifdef SIGBUS
dc952a08 44$signal_t bletch(int s) { exit(4); }
9589ed74 45#endif
d85168a5
JH
46int main() {
47#if BYTEORDER == 0x1234 || BYTEORDER == 0x4321
5ccb2b6b
MB
48 volatile U8 buf[8];
49 volatile U32 *up;
d85168a5
JH
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
9589ed74
JH
59#ifdef SIGBUS
60 signal(SIGBUS, bletch);
61#endif
62
6a2a4e4f
JH
63 buf[0] = 0;
64 buf[1] = 0;
65 buf[2] = 0;
66 buf[3] = 1;
750213d3 67 buf[4] = 0;
6a2a4e4f
JH
68 buf[5] = 0;
69 buf[6] = 0;
750213d3 70 buf[7] = 1;
6a2a4e4f 71
d85168a5
JH
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}
101EOCP
102set try
103if eval $compile_ok; then
99457da0 104 echo "(Testing for character data alignment may crash the test. That's okay.)" >&4
0f00356b 105 $run ./try 2>&1 >/dev/null
d85168a5
JH
106 case "$?" in
107 0) cat >&4 <<EOM
108You can access character data pretty unalignedly.
109EOM
110 d_u32align="$undef"
111 ;;
112 *) cat >&4 <<EOM
113It seems that you must access character data in an aligned manner.
114EOM
115 d_u32align="$define"
116 ;;
117 esac
d85168a5
JH
118else
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
126fi
2cb64bf6 127$rm_try
a0d5d9d4
JH
128;;
129esac
d85168a5 130