File: CheckAlignment.c

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (62 lines) | stat: -rw-r--r-- 1,977 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
                      /*******************************************************
                       * Create ascii pattern to match in order to find
                       *         alignment at compile time
                       *******************************************************/

// Compile this program and match
// the produced executable against:
// INT64_ALIGNMENT=<code>
// VOIDP_ALIGNMENT=<code>
// DOUBLE_ALIGNMENT=<code>
//
// to get the alignment used by the (cross)compiler.
//
// <code> is the alignment as an ascii digit.

#include <stdint.h>

#if  defined(__GNUC__) || defined(__clang__)
#define ALIGNOF(type) (__alignof(type) + 48) // Ascii '1' for 1, '4' for 4, '8' for 8
#else
#define ALIGNOF(type) (sizeof(type) + 48) // Safe fallback
#endif

int prevent_optimization(unsigned char*p, int size) {
   unsigned char *d;
   int i;

   //Prevent optimizer from eliminating the constants in main()
   unsigned char dummy[24];			/* var is not allowed */
   d = dummy;
   for (i = 0; i < size; ++i) {
     *d++ = *p++;
   }
   return dummy[size-1];
}


#define int64_pat_sz  18
#define voidp_pat_sz  18
#define double_pat_sz  19
int main() {

   static const unsigned char int64_alignment[int64_pat_sz] = {
         'I', 'N','T','6','4','_','A','L','I','G','N','M','E','N','T','=',
         ALIGNOF(int64_t), 0x0
   };

   static const unsigned char voidp_alignment[voidp_pat_sz] = {
         'V', 'O','I','D','P','_','A','L','I','G','N','M','E','N','T','=',
         ALIGNOF(void*), 0x0
   };

   static const unsigned char double_alignment[double_pat_sz] = {
         'D', 'O','U','B','L','E','_','A','L','I','G','N','M','E','N','T','=',
         ALIGNOF(double), 0x0
   };

   //Not used, prevent optimization
   return prevent_optimization((unsigned char*)int64_alignment, int64_pat_sz) +
          prevent_optimization((unsigned char*)voidp_alignment, voidp_pat_sz) +
          prevent_optimization((unsigned char*)double_alignment, double_pat_sz);
}