1 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */
2 /*======================================================================
3 Copyright (C) 2011 Walter Doekes <walter+tthsum@wjd.nu>
4 This file is part of tthsum.
5 
6 tthsum is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 tthsum is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with tthsum.  If not, see <http://www.gnu.org/licenses/>.
18 ======================================================================*/
19 #include "types.h"
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25 
26 
27 /* The tiger_bp function uses unaligned 64 bit memory access on little
28  * endian platforms. Tests on my x86_64 incur a 3.5% performance penalty
29  * when we force aligned access. However, some platforms need alignment
30  * to function properly (like the ARM).
31  *
32  * I tested doing 1024 byte reads on memory aligned such that it was
33  * properly aligned for the tiger_bp function, but that caused the
34  * system time to double: 6% performance penalty. */
conf_alignment()35 static void conf_alignment() {
36     /* Because it's hard to continue execution after a SIGBUS, we do
37      * the misalignment check from a child process: this way both a
38      * SIGBUS and a value mismatch will add the -DREQUIRE_ALIGNMENT
39      * preprocessor flag. */
40     pid_t pid = fork();
41     /* child */
42     if (pid == 0) {
43 	uint64_t buf64[3];
44 	uint8_t *p;
45 	memcpy(buf64, "AAAAAAA" "BBBBBBBB" "CCCCCCCC", 24); /* 7+8+8+1 */
46 	p = (uint8_t*)&buf64[0] + 7;
47 	if (*((uint64_t*)p) != _ULL(0x4242424242424242))
48 	    _exit(1);
49 	_exit(0);
50     /* parent */
51     } else if (pid > 0) {
52 	int status;
53 	waitpid(pid, &status, 0);
54 	if (status != 0)
55 	    printf("-DREQUIRE_ALIGNMENT ");
56     /* error */
57     } else {
58 	perror("fork");
59     }
60 }
61 
62 
main()63 int main() {
64     conf_alignment();
65     return 0;
66 }
67