1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Compiler specific stuff. m_compiler.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2014-2014 Florian Krohm
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
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.
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. */
49 __builtin_popcount(UInt x
)
53 for (i
= 0; i
< 32; ++i
) {
61 __builtin_popcountll(ULong x
)
65 for (i
= 0; i
< 64; ++i
) {
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. */
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;
94 __builtin_clzll(ULong x
)
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;
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. */
116 __builtin_ctz(UInt x
)
120 for (i
= 0; i
< 32; ++i
) {
129 __builtin_ctzll(ULong x
)
133 for (i
= 0; i
< 64; ++i
) {
143 #ifdef __INTEL_COMPILER
145 /* Provide certain functions Intel's ICC compiler expects to be defined. */
148 _intel_fast_memcpy(void *dest
, const void *src
, SizeT sz
)
150 return VG_(memcpy
)( dest
, src
, sz
);
154 _intel_fast_memset(void *dest
, int value
, SizeT num
)
156 return VG_(memset
)( dest
, value
, num
);
160 /*--------------------------------------------------------------------*/
162 /*--------------------------------------------------------------------*/