drd: Add a consistency check
[valgrind.git] / coregrind / m_compiler.c
blob3be4549444ba3e562e2c8602da09c37be3c3e729
1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Compiler specific stuff. m_compiler.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2014-2014 Florian Krohm
12 florian@eich-krohm.de
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
29 The GNU General Public License is contained in the file COPYING.
32 /* Currently, this file provides definitions for builtins that not all
33 compilers or compiler versions provide.
35 Missing builtins are rare. Therefore, no attempt has been made to
36 provide efficient implementations.
39 #include "config.h"
40 #include "pub_core_basics.h"
41 #include "pub_core_libcbase.h"
43 #ifndef HAVE_BUILTIN_POPCOUT
45 /* From the GCC documentation:
46 Returns the number of 1-bits in x. */
48 UInt
49 __builtin_popcount(UInt x)
51 UInt i, count = 0;
53 for (i = 0; i < 32; ++i) {
54 count += x & 1;
55 x >>= 1;
57 return count;
60 UInt
61 __builtin_popcountll(ULong x)
63 UInt i, count = 0;
65 for (i = 0; i < 64; ++i) {
66 count += x & 1;
67 x >>= 1;
69 return count;
71 #endif
73 #ifndef HAVE_BUILTIN_CLZ
75 /* From the GCC documentation:
76 Returns the number of leading 0-bits in x, starting at the most
77 significant position. If x is 0, the result is undefined. */
79 UInt
80 __builtin_clz(UInt x)
82 UInt count = 32;
83 UInt y;
85 y = x >> 16; if (y != 0) { count -= 16; x = y; }
86 y = x >> 8; if (y != 0) { count -= 8; x = y; }
87 y = x >> 4; if (y != 0) { count -= 4; x = y; }
88 y = x >> 2; if (y != 0) { count -= 2; x = y; }
89 y = x >> 1; if (y != 0) return count - 2;
90 return count - x;
93 UInt
94 __builtin_clzll(ULong x)
96 UInt count = 64;
97 ULong y;
99 y = x >> 32; if (y != 0) { count -= 32; x = y; }
100 y = x >> 16; if (y != 0) { count -= 16; x = y; }
101 y = x >> 8; if (y != 0) { count -= 8; x = y; }
102 y = x >> 4; if (y != 0) { count -= 4; x = y; }
103 y = x >> 2; if (y != 0) { count -= 2; x = y; }
104 y = x >> 1; if (y != 0) return count - 2;
105 return count - x;
107 #endif
109 #ifndef HAVE_BUILTIN_CTZ
111 /* From the GCC documentation:
112 Returns the number of trailing 0-bits in x, starting at the least
113 significant bit position. If x is 0, the result is undefined. */
115 UInt
116 __builtin_ctz(UInt x)
118 UInt i, count = 0;
120 for (i = 0; i < 32; ++i) {
121 if (x & 1) break;
122 ++count;
123 x >>= 1;
125 return count;
128 UInt
129 __builtin_ctzll(ULong x)
131 UInt i, count = 0;
133 for (i = 0; i < 64; ++i) {
134 if (x & 1) break;
135 ++count;
136 x >>= 1;
138 return count;
140 #endif
143 #ifdef __INTEL_COMPILER
145 /* Provide certain functions Intel's ICC compiler expects to be defined. */
147 void *
148 _intel_fast_memcpy(void *dest, const void *src, SizeT sz)
150 return VG_(memcpy)( dest, src, sz );
153 void *
154 _intel_fast_memset(void *dest, int value, SizeT num)
156 return VG_(memset)( dest, value, num );
158 #endif
160 /*--------------------------------------------------------------------*/
161 /*--- end ---*/
162 /*--------------------------------------------------------------------*/