blob: 272f9d9dad7b6c0f3a1aaa500c5777b228146f09 [file] [log] [blame]
Daniel Dunbar4467b652009-10-27 17:48:46 +00001/* ===-- int_lib.h - configuration header for compiler-rt -----------------===
Edward O'Callaghan0e4ad9c2009-08-05 01:47:29 +00002 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-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'Callaghan0e4ad9c2009-08-05 01:47:29 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
Daniel Dunbar4467b652009-10-27 17:48:46 +000010 * This file is a configuration header for compiler-rt.
Edward O'Callaghan0e4ad9c2009-08-05 01:47:29 +000011 * This file is not part of the interface of this library.
12 *
13 * ===----------------------------------------------------------------------===
14 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015
16#ifndef INT_LIB_H
17#define INT_LIB_H
18
Daniel Dunbar0a3e3502011-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 Abdulrasoold3a8aba2015-08-21 04:39:52 +000023#if defined(__ELF__)
Josh Gaoc8525162015-08-21 02:51:17 +000024#define FNALIAS(alias_name, original_name) \
25 void alias_name() __attribute__((alias(#original_name)))
Saleem Abdulrasoold3a8aba2015-08-21 04:39:52 +000026#else
27#define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")")
28#endif
Josh Gaoc8525162015-08-21 02:51:17 +000029
Daniel Dunbar0ae9d252011-11-15 18:34:44 +000030/* ABI macro definitions */
31
32#if __ARM_EABI__
33# define ARM_EABI_FNALIAS(aeabi_name, name) \
34 void __aeabi_##aeabi_name() __attribute__((alias("__" #name)));
Saleem Abdulrasool8554b6b2014-09-06 21:33:55 +000035# define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
Daniel Dunbar0ae9d252011-11-15 18:34:44 +000036#else
37# define ARM_EABI_FNALIAS(aeabi_name, name)
Saleem Abdulrasool08b610e2015-10-06 04:33:05 +000038# if defined(__arm__) && defined(_WIN32) && (!defined(_MSC_VER) || defined(__clang__))
Saleem Abdulrasool52417d02015-05-22 21:47:24 +000039# define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
40# else
41# define COMPILER_RT_ABI
42# endif
Daniel Dunbar0ae9d252011-11-15 18:34:44 +000043#endif
44
Saleem Abdulrasool08b610e2015-10-06 04:33:05 +000045#ifdef _MSC_VER
Saleem Abdulrasool4a5943a2015-10-11 17:35:42 +000046#define ALWAYS_INLINE __forceinline
Saleem Abdulrasool08b610e2015-10-06 04:33:05 +000047#define NOINLINE __declspec(noinline)
48#define NORETURN __declspec(noreturn)
49#define UNUSED
50#else
Saleem Abdulrasool4a5943a2015-10-11 17:35:42 +000051#define ALWAYS_INLINE __attribute__((always_inline))
Saleem Abdulrasool08b610e2015-10-06 04:33:05 +000052#define NOINLINE __attribute__((noinline))
53#define NORETURN __attribute__((noreturn))
54#define UNUSED __attribute__((unused))
55#endif
56
Joerg Sonnenberger09226bc2013-12-03 16:19:14 +000057#if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE))
58/*
59 * Kernel and boot environment can't use normal headers,
60 * so use the equivalent system headers.
61 */
62# include <machine/limits.h>
63# include <sys/stdint.h>
64# include <sys/types.h>
65#else
Daniel Dunbar2bf93402011-11-16 00:20:36 +000066/* Include the standard compiler builtin headers we use functionality from. */
Joerg Sonnenberger09226bc2013-12-03 16:19:14 +000067# include <limits.h>
68# include <stdint.h>
69# include <stdbool.h>
70# include <float.h>
71#endif
Daniel Dunbar2bf93402011-11-16 00:20:36 +000072
Daniel Dunbar396a72f2011-11-15 18:56:13 +000073/* Include the commonly used internal type definitions. */
74#include "int_types.h"
Daniel Dunbarb3a69012009-06-26 16:47:03 +000075
Daniel Dunbar401f6932011-11-16 01:19:19 +000076/* Include internal utility function declarations. */
77#include "int_util.h"
78
Joerg Sonnenbergerbfbb8bb2014-03-01 15:30:50 +000079COMPILER_RT_ABI si_int __paritysi2(si_int a);
80COMPILER_RT_ABI si_int __paritydi2(di_int a);
81
82COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
83COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
84COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
85
86COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
87COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
88#ifdef CRT_HAS_128BIT
Joerg Sonnenbergerae87cca2014-03-01 15:57:30 +000089COMPILER_RT_ABI si_int __clzti2(ti_int a);
Joerg Sonnenbergerbfbb8bb2014-03-01 15:30:50 +000090COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
91#endif
92
Saleem Abdulrasool52f9fb12015-10-07 02:58:11 +000093/* Definitions for builtins unavailable on MSVC */
94#if defined(_MSC_VER) && !defined(__clang__)
95#include <intrin.h>
96
97uint32_t __inline __builtin_ctz(uint32_t value) {
98 uint32_t trailing_zero = 0;
99 if (_BitScanForward(&trailing_zero, value))
100 return trailing_zero;
101 return 32;
102}
103
104uint32_t __inline __builtin_clz(uint32_t value) {
105 uint32_t leading_zero = 0;
106 if (_BitScanReverse(&leading_zero, value))
107 return 31 - leading_zero;
108 return 32;
109}
110
Saleem Abdulrasoolcdcbb4a2015-10-10 17:57:37 +0000111#if defined(_M_ARM) || defined(_M_X64)
Saleem Abdulrasool52f9fb12015-10-07 02:58:11 +0000112uint32_t __inline __builtin_clzll(uint64_t value) {
113 uint32_t leading_zero = 0;
114 if (_BitScanReverse64(&leading_zero, value))
115 return 63 - leading_zero;
116 return 64;
117}
Saleem Abdulrasoolb6cbcef2015-10-15 02:46:37 +0000118#else
119uint32_t __inline __builtin_clzll(uint64_t value) {
120 if (value == 0)
121 return 64;
122 uint32_t msh = (uint32_t)(value >> 32);
123 uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
124 if (msh != 0)
125 return __builtin_clz(msh);
126 return 32 + __builtin_clz(lsh);
127}
128#endif
Saleem Abdulrasool52f9fb12015-10-07 02:58:11 +0000129
130#define __builtin_clzl __builtin_clzll
Saleem Abdulrasoolb6cbcef2015-10-15 02:46:37 +0000131#endif // defined(_MSC_VER) && !defined(__clang__)
Saleem Abdulrasool52f9fb12015-10-07 02:58:11 +0000132
Edward O'Callaghan0898ee92009-08-05 19:57:20 +0000133#endif /* INT_LIB_H */