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-2015 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"
42 #include "pub_core_libcassert.h"
43 #include "pub_core_debuglog.h"
45 #ifndef HAVE_BUILTIN_POPCOUT
47 /* From the GCC documentation:
48 Returns the number of 1-bits in x. */
51 __builtin_popcount(UInt x
)
55 for (i
= 0; i
< 32; ++i
) {
63 __builtin_popcountll(ULong x
)
67 for (i
= 0; i
< 64; ++i
) {
75 #ifndef HAVE_BUILTIN_CLZ
77 /* From the GCC documentation:
78 Returns the number of leading 0-bits in x, starting at the most
79 significant position. If x is 0, the result is undefined. */
87 y
= x
>> 16; if (y
!= 0) { count
-= 16; x
= y
; }
88 y
= x
>> 8; if (y
!= 0) { count
-= 8; x
= y
; }
89 y
= x
>> 4; if (y
!= 0) { count
-= 4; x
= y
; }
90 y
= x
>> 2; if (y
!= 0) { count
-= 2; x
= y
; }
91 y
= x
>> 1; if (y
!= 0) return count
- 2;
96 __builtin_clzll(ULong x
)
101 y
= x
>> 32; if (y
!= 0) { count
-= 32; x
= y
; }
102 y
= x
>> 16; if (y
!= 0) { count
-= 16; x
= y
; }
103 y
= x
>> 8; if (y
!= 0) { count
-= 8; x
= y
; }
104 y
= x
>> 4; if (y
!= 0) { count
-= 4; x
= y
; }
105 y
= x
>> 2; if (y
!= 0) { count
-= 2; x
= y
; }
106 y
= x
>> 1; if (y
!= 0) return count
- 2;
111 #ifndef HAVE_BUILTIN_CTZ
113 /* From the GCC documentation:
114 Returns the number of trailing 0-bits in x, starting at the least
115 significant bit position. If x is 0, the result is undefined. */
118 __builtin_ctz(UInt x
)
122 for (i
= 0; i
< 32; ++i
) {
131 __builtin_ctzll(ULong x
)
135 for (i
= 0; i
< 64; ++i
) {
145 #ifdef __INTEL_COMPILER
147 /* Provide certain functions Intel's ICC compiler expects to be defined. */
150 __intel_memcpy(void *dest
, const void *src
, SizeT sz
)
152 return VG_(memcpy
)( dest
, src
, sz
);
156 __intel_mic_avx512f_memcpy(void *dest
, const void *src
, SizeT sz
)
158 return VG_(memcpy
)( dest
, src
, sz
);
162 __intel_new_memcpy(void *dest
, const void *src
, SizeT sz
)
164 return VG_(memcpy
)( dest
, src
, sz
);
168 __intel_ssse3_memcpy(void *dest
, const void *src
, SizeT sz
)
170 return VG_(memcpy
)( dest
, src
, sz
);
174 __intel_ssse3_rep_memcpy(void *dest
, const void *src
, SizeT sz
)
176 return VG_(memcpy
)( dest
, src
, sz
);
180 _intel_fast_memcpy(void *dest
, const void *src
, SizeT sz
)
182 return VG_(memcpy
)( dest
, src
, sz
);
186 __intel_lrb_memcpy(void *dest
, const void *src
, SizeT sz
)
188 return VG_(memcpy
)( dest
, src
, sz
);
192 __intel_memset(void *dest
, int value
, SizeT num
)
194 return VG_(memset
)( dest
, value
, num
);
198 __intel_new_memset(void *dest
, int value
, SizeT num
)
200 return VG_(memset
)( dest
, value
, num
);
204 __intel_mic_avx512f_memset(void *dest
, int value
, SizeT num
)
206 return VG_(memset
)( dest
, value
, num
);
210 __intel_lrb_memset(void *dest
, int value
, SizeT num
)
212 return VG_(memset
)( dest
, value
, num
);
216 _intel_fast_memset(void *dest
, int value
, SizeT num
)
218 return VG_(memset
)( dest
, value
, num
);
224 /*====================================================================*/
225 /*=== gcc -fsanitize=undefined helper function support ===*/
226 /*====================================================================*/
228 void __ubsan_handle_type_mismatch ( void );
229 void __ubsan_handle_type_mismatch ( void )
231 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
235 void __ubsan_handle_mul_overflow ( void );
236 void __ubsan_handle_mul_overflow ( void )
238 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
242 void __ubsan_handle_add_overflow ( void );
243 void __ubsan_handle_add_overflow ( void )
245 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
249 void __ubsan_handle_sub_overflow ( void );
250 void __ubsan_handle_sub_overflow ( void )
252 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
256 void __ubsan_handle_divrem_overflow ( void );
257 void __ubsan_handle_divrem_overflow ( void )
259 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
263 void __ubsan_handle_negate_overflow ( void );
264 void __ubsan_handle_negate_overflow ( void )
266 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
270 void __ubsan_handle_out_of_bounds ( void );
271 void __ubsan_handle_out_of_bounds ( void )
273 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
277 void __ubsan_handle_shift_out_of_bounds ( void );
278 void __ubsan_handle_shift_out_of_bounds ( void )
280 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
284 void __ubsan_handle_vla_bound_not_positive ( void );
285 void __ubsan_handle_vla_bound_not_positive ( void )
287 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
291 void __ubsan_handle_nonnull_arg ( void );
292 void __ubsan_handle_nonnull_arg ( void )
294 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
298 /*--------------------------------------------------------------------*/
300 /*--------------------------------------------------------------------*/