1 /*
2  * align32 - determine if 32 bit accesses must be aligned
3  *
4  * Copyright (C) 1999,2021  Landon Curt Noll
5  *
6  * Calc is open software; you can redistribute it and/or modify it under
7  * the terms of the version 2.1 of the GNU Lesser General Public License
8  * as published by the Free Software Foundation.
9  *
10  * Calc is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU Lesser General
13  * Public License for more details.
14  *
15  * A copy of version 2.1 of the GNU Lesser General Public License is
16  * distributed with calc under the filename COPYING-LGPL.  You should have
17  * received a copy with calc; if not, write to Free Software Foundation, Inc.
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  *
20  * Under source code control:	1995/11/23 05:18:06
21  * File existed as early as:	1995
22  *
23  * chongo <was here> /\oo/\	http://www.isthe.com/chongo/
24  * Share and enjoy!  :-)	http://www.isthe.com/chongo/tech/comp/calc/
25  */
26 
27 
28 #include <stdio.h>
29 #include <signal.h>
30 #include "longbits.h"
31 
32 #include "have_unistd.h"
33 #if defined(HAVE_UNISTD_H)
34 #include <unistd.h>
35 #endif
36 
37 
38 #include "banned.h"	/* include after system header <> includes */
39 
40 
41 static void buserr(void);	/* catch alignment errors */
42 
43 
44 int
main(void)45 main(void)
46 {
47 	char byte[2*sizeof(USB32)];	/* mis-alignment buffer */
48 	USB32 *p;			/* mis-alignment pointer */
49 	int i;
50 
51 #if defined(MUST_ALIGN32)
52 	/* force alignment */
53 	printf("#define MUST_ALIGN32\t%c* forced to align 32 bit values *%c\n",
54 	   '/', '/');
55 #else
56 	/* setup to catch alignment bus errors */
57 	signal(SIGBUS, buserr);
58 	signal(SIGSEGV, buserr);  /* some systems will generate SEGV instead! */
59 
60 	/* mis-align our long fetches */
61 	for (i=0; i < sizeof(USB32); ++i) {
62 		p = (USB32 *)(byte+i);
63 		*p = i;
64 		*p += 1;
65 	}
66 
67 	/* if we got here, then we can mis-align longs */
68 	printf("#undef MUST_ALIGN32\t%c* can mis-align 32 bit values *%c\n",
69 	   '/', '/');
70 
71 #endif
72 	/* exit(0); */
73 	return 0;
74 }
75 
76 
77 /*
78  * buserr - catch an alignment error
79  *
80  * given:
81  *	arg		to keep ANSI C happy
82  */
83 /*ARGSUSED*/
84 static void
buserr(int arg)85 buserr(int arg)
86 {
87 	/* alignment is required */
88 	printf("#define MUST_ALIGN32\t%c* must align 32 bit values *%c\n",
89 	  '/', '/');
90 	exit(0);
91 }
92