blob: 9d09e2dc915b242c8e8a85b339f4ecb03ad1cee5 [file] [log] [blame]
Daniel Dunbar24768732009-10-27 17:48:46 +00001/* ===-- int_lib.h - configuration header for compiler-rt -----------------===
Edward O'Callaghandf720462009-08-05 01:47:29 +00002 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghandf720462009-08-05 01:47:29 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
Daniel Dunbar24768732009-10-27 17:48:46 +000010 * This file is a configuration header for compiler-rt.
Edward O'Callaghandf720462009-08-05 01:47:29 +000011 * This file is not part of the interface of this library.
12 *
13 * ===----------------------------------------------------------------------===
14 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000015
16#ifndef INT_LIB_H
17#define INT_LIB_H
18
Daniel Dunbarcc675f42011-11-15 18:56:21 +000019/* Assumption: Signed integral is 2's complement. */
20/* Assumption: Right shift of signed negative is arithmetic shift. */
21/* Assumption: Endianness is little or big (not mixed). */
22
Saleem Abdulrasool0d6094b2015-08-21 04:39:52 +000023#if defined(__ELF__)
Josh Gao772527c2015-08-21 02:51:17 +000024#define FNALIAS(alias_name, original_name) \
Eli Friedman0d586d02017-10-03 21:25:07 +000025 void alias_name() __attribute__((__alias__(#original_name)))
26#define COMPILER_RT_ALIAS(aliasee) __attribute__((__alias__(#aliasee)))
Saleem Abdulrasool0d6094b2015-08-21 04:39:52 +000027#else
28#define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")")
Eli Friedman0d586d02017-10-03 21:25:07 +000029#define COMPILER_RT_ALIAS(aliasee) _Pragma("GCC error(\"alias unsupported on this file format\")")
Saleem Abdulrasool0d6094b2015-08-21 04:39:52 +000030#endif
Josh Gao772527c2015-08-21 02:51:17 +000031
Daniel Dunbarb6f75f72011-11-15 18:34:44 +000032/* ABI macro definitions */
33
34#if __ARM_EABI__
Weiming Zhaod8ca7412017-03-29 03:36:46 +000035# ifdef COMPILER_RT_ARMHF_TARGET
36# define COMPILER_RT_ABI
37# else
Saleem Abdulrasool44c45712017-05-16 04:17:12 +000038# define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
Weiming Zhaod8ca7412017-03-29 03:36:46 +000039# endif
Daniel Dunbarb6f75f72011-11-15 18:34:44 +000040#else
Saleem Abdulrasool28e1b972016-04-20 17:43:40 +000041# define COMPILER_RT_ABI
Daniel Dunbarb6f75f72011-11-15 18:34:44 +000042#endif
43
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +000044#define AEABI_RTABI __attribute__((__pcs__("aapcs")))
45
Saleem Abdulrasoole6f96522015-10-06 04:33:05 +000046#ifdef _MSC_VER
Saleem Abdulrasoole4997152015-10-11 17:35:42 +000047#define ALWAYS_INLINE __forceinline
Saleem Abdulrasoole6f96522015-10-06 04:33:05 +000048#define NOINLINE __declspec(noinline)
49#define NORETURN __declspec(noreturn)
50#define UNUSED
51#else
Saleem Abdulrasoole4997152015-10-11 17:35:42 +000052#define ALWAYS_INLINE __attribute__((always_inline))
Saleem Abdulrasoole6f96522015-10-06 04:33:05 +000053#define NOINLINE __attribute__((noinline))
54#define NORETURN __attribute__((noreturn))
55#define UNUSED __attribute__((unused))
56#endif
57
Joerg Sonnenberger24f4a7d2013-12-03 16:19:14 +000058#if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE))
59/*
60 * Kernel and boot environment can't use normal headers,
61 * so use the equivalent system headers.
62 */
63# include <machine/limits.h>
64# include <sys/stdint.h>
65# include <sys/types.h>
66#else
Daniel Dunbar7205b232011-11-16 00:20:36 +000067/* Include the standard compiler builtin headers we use functionality from. */
Joerg Sonnenberger24f4a7d2013-12-03 16:19:14 +000068# include <limits.h>
69# include <stdint.h>
70# include <stdbool.h>
71# include <float.h>
72#endif
Daniel Dunbar7205b232011-11-16 00:20:36 +000073
Daniel Dunbarad4f9822011-11-15 18:56:13 +000074/* Include the commonly used internal type definitions. */
75#include "int_types.h"
Daniel Dunbarfd089992009-06-26 16:47:03 +000076
Daniel Dunbar2b88e032011-11-16 01:19:19 +000077/* Include internal utility function declarations. */
78#include "int_util.h"
79
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +000080COMPILER_RT_ABI si_int __paritysi2(si_int a);
81COMPILER_RT_ABI si_int __paritydi2(di_int a);
82
83COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
84COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
85COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
86
87COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
88COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
89#ifdef CRT_HAS_128BIT
Joerg Sonnenberger7e6a3142014-03-01 15:57:30 +000090COMPILER_RT_ABI si_int __clzti2(ti_int a);
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +000091COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
92#endif
93
Saleem Abdulrasool396e7942015-10-07 02:58:11 +000094/* Definitions for builtins unavailable on MSVC */
95#if defined(_MSC_VER) && !defined(__clang__)
96#include <intrin.h>
97
98uint32_t __inline __builtin_ctz(uint32_t value) {
Reid Kleckner1bd5e3e2016-08-01 18:39:27 +000099 unsigned long trailing_zero = 0;
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000100 if (_BitScanForward(&trailing_zero, value))
101 return trailing_zero;
102 return 32;
103}
104
105uint32_t __inline __builtin_clz(uint32_t value) {
Reid Kleckner1bd5e3e2016-08-01 18:39:27 +0000106 unsigned long leading_zero = 0;
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000107 if (_BitScanReverse(&leading_zero, value))
108 return 31 - leading_zero;
109 return 32;
110}
111
Saleem Abdulrasool9650c4a2015-10-10 17:57:37 +0000112#if defined(_M_ARM) || defined(_M_X64)
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000113uint32_t __inline __builtin_clzll(uint64_t value) {
Reid Kleckner1bd5e3e2016-08-01 18:39:27 +0000114 unsigned long leading_zero = 0;
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000115 if (_BitScanReverse64(&leading_zero, value))
116 return 63 - leading_zero;
117 return 64;
118}
Saleem Abdulrasool956365e2015-10-15 02:46:37 +0000119#else
120uint32_t __inline __builtin_clzll(uint64_t value) {
121 if (value == 0)
122 return 64;
123 uint32_t msh = (uint32_t)(value >> 32);
124 uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
125 if (msh != 0)
126 return __builtin_clz(msh);
127 return 32 + __builtin_clz(lsh);
128}
129#endif
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000130
131#define __builtin_clzl __builtin_clzll
Nico Weber6341a792015-12-22 17:22:25 +0000132#endif /* defined(_MSC_VER) && !defined(__clang__) */
Saleem Abdulrasool396e7942015-10-07 02:58:11 +0000133
Edward O'Callaghan7a6cb5f2009-08-05 19:57:20 +0000134#endif /* INT_LIB_H */