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-2017 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, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 /* Currently, this file provides definitions for builtins that not all
31 compilers or compiler versions provide.
33 Missing builtins are rare. Therefore, no attempt has been made to
34 provide efficient implementations.
38 #include "pub_core_basics.h"
39 #include "pub_core_libcbase.h"
40 #include "pub_core_libcassert.h"
41 #include "pub_core_debuglog.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_memcpy(void *dest
, const void *src
, SizeT sz
)
150 return VG_(memcpy
)( dest
, src
, sz
);
154 __intel_mic_avx512f_memcpy(void *dest
, const void *src
, SizeT sz
)
156 return VG_(memcpy
)( dest
, src
, sz
);
160 __intel_new_memcpy(void *dest
, const void *src
, SizeT sz
)
162 return VG_(memcpy
)( dest
, src
, sz
);
166 __intel_ssse3_memcpy(void *dest
, const void *src
, SizeT sz
)
168 return VG_(memcpy
)( dest
, src
, sz
);
172 __intel_ssse3_rep_memcpy(void *dest
, const void *src
, SizeT sz
)
174 return VG_(memcpy
)( dest
, src
, sz
);
178 _intel_fast_memcpy(void *dest
, const void *src
, SizeT sz
)
180 return VG_(memcpy
)( dest
, src
, sz
);
184 __intel_lrb_memcpy(void *dest
, const void *src
, SizeT sz
)
186 return VG_(memcpy
)( dest
, src
, sz
);
190 __intel_memset(void *dest
, int value
, SizeT num
)
192 return VG_(memset
)( dest
, value
, num
);
196 __intel_new_memset(void *dest
, int value
, SizeT num
)
198 return VG_(memset
)( dest
, value
, num
);
202 __intel_mic_avx512f_memset(void *dest
, int value
, SizeT num
)
204 return VG_(memset
)( dest
, value
, num
);
208 __intel_lrb_memset(void *dest
, int value
, SizeT num
)
210 return VG_(memset
)( dest
, value
, num
);
214 _intel_fast_memset(void *dest
, int value
, SizeT num
)
216 return VG_(memset
)( dest
, value
, num
);
222 /*====================================================================*/
223 /*=== gcc -fsanitize=undefined helper function support ===*/
224 /*====================================================================*/
226 void __ubsan_handle_type_mismatch ( void );
227 void __ubsan_handle_type_mismatch ( void )
229 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
233 void __ubsan_handle_type_mismatch_v1 ( void );
234 void __ubsan_handle_type_mismatch_v1 ( void )
236 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
240 void __ubsan_handle_mul_overflow ( void );
241 void __ubsan_handle_mul_overflow ( void )
243 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
247 void __ubsan_handle_add_overflow ( void );
248 void __ubsan_handle_add_overflow ( void )
250 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
254 void __ubsan_handle_sub_overflow ( void );
255 void __ubsan_handle_sub_overflow ( void )
257 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
261 void __ubsan_handle_divrem_overflow ( void );
262 void __ubsan_handle_divrem_overflow ( void )
264 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
268 void __ubsan_handle_negate_overflow ( void );
269 void __ubsan_handle_negate_overflow ( void )
271 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
275 void __ubsan_handle_pointer_overflow ( void );
276 void __ubsan_handle_pointer_overflow ( void )
278 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
282 void __ubsan_handle_out_of_bounds ( void );
283 void __ubsan_handle_out_of_bounds ( void )
285 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
289 void __ubsan_handle_shift_out_of_bounds ( void );
290 void __ubsan_handle_shift_out_of_bounds ( void )
292 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
296 void __ubsan_handle_vla_bound_not_positive ( void );
297 void __ubsan_handle_vla_bound_not_positive ( void )
299 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
303 void __ubsan_handle_nonnull_arg ( void );
304 void __ubsan_handle_nonnull_arg ( void )
306 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
310 void __ubsan_handle_invalid_builtin ( void );
311 void __ubsan_handle_invalid_builtin ( void )
313 VG_(debugLog
)(0, "main:ubsan", "In %s", __func__
);
317 /*--------------------------------------------------------------------*/
319 /*--------------------------------------------------------------------*/