blob: 04ea2d910574bcfa52e18422948beac071441eea [file] [log] [blame]
Daniel Boulby318e7a52022-10-21 20:20:52 +01001//===-- int_lib.h - configuration header for compiler-rt -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a configuration header for compiler-rt.
10// This file is not part of the interface of this library.
11//
12//===----------------------------------------------------------------------===//
dp-arme3cc8382017-05-04 12:15:35 +010013
dp-arm75b60572017-05-04 12:12:06 +010014#ifndef INT_LIB_H
15#define INT_LIB_H
16
Daniel Boulby318e7a52022-10-21 20:20:52 +010017// Assumption: Signed integral is 2's complement.
18// Assumption: Right shift of signed negative is arithmetic shift.
19// Assumption: Endianness is little or big (not mixed).
dp-arm75b60572017-05-04 12:12:06 +010020
Daniel Boulby318e7a52022-10-21 20:20:52 +010021// ABI macro definitions
dp-arm75b60572017-05-04 12:12:06 +010022
23#if __ARM_EABI__
Daniel Boulby318e7a52022-10-21 20:20:52 +010024#ifdef COMPILER_RT_ARMHF_TARGET
25#define COMPILER_RT_ABI
26#else
27#define COMPILER_RT_ABI __attribute__((__pcs__("aapcs")))
28#endif
dp-arm75b60572017-05-04 12:12:06 +010029#else
Daniel Boulby318e7a52022-10-21 20:20:52 +010030#define COMPILER_RT_ABI
dp-arm75b60572017-05-04 12:12:06 +010031#endif
32
Sandrine Bailleuxcefb51f2018-10-31 13:35:01 +010033#define AEABI_RTABI __attribute__((__pcs__("aapcs")))
34
35#if defined(_MSC_VER) && !defined(__clang__)
dp-arm75b60572017-05-04 12:12:06 +010036#define ALWAYS_INLINE __forceinline
37#define NOINLINE __declspec(noinline)
38#define NORETURN __declspec(noreturn)
39#define UNUSED
40#else
41#define ALWAYS_INLINE __attribute__((always_inline))
42#define NOINLINE __attribute__((noinline))
43#define NORETURN __attribute__((noreturn))
44#define UNUSED __attribute__((unused))
45#endif
46
Daniel Boulby318e7a52022-10-21 20:20:52 +010047#define STR(a) #a
48#define XSTR(a) STR(a)
49#define SYMBOL_NAME(name) XSTR(__USER_LABEL_PREFIX__) #name
50
51#if defined(__ELF__) || defined(__MINGW32__) || defined(__wasm__) || \
Manish Pandey9320bd52023-11-06 16:45:14 +000052 defined(_AIX) || defined(__CYGWIN__)
Daniel Boulby318e7a52022-10-21 20:20:52 +010053#define COMPILER_RT_ALIAS(name, aliasname) \
54 COMPILER_RT_ABI __typeof(name) aliasname __attribute__((__alias__(#name)));
55#elif defined(__APPLE__)
56#if defined(VISIBILITY_HIDDEN)
57#define COMPILER_RT_ALIAS_VISIBILITY(name) \
58 __asm__(".private_extern " SYMBOL_NAME(name));
59#else
60#define COMPILER_RT_ALIAS_VISIBILITY(name)
61#endif
62#define COMPILER_RT_ALIAS(name, aliasname) \
63 __asm__(".globl " SYMBOL_NAME(aliasname)); \
64 COMPILER_RT_ALIAS_VISIBILITY(aliasname) \
65 __asm__(SYMBOL_NAME(aliasname) " = " SYMBOL_NAME(name)); \
66 COMPILER_RT_ABI __typeof(name) aliasname;
67#elif defined(_WIN32)
68#define COMPILER_RT_ALIAS(name, aliasname)
69#else
70#error Unsupported target
71#endif
72
73#if (defined(__FreeBSD__) || defined(__NetBSD__)) && \
74 (defined(_KERNEL) || defined(_STANDALONE))
75//
76// Kernel and boot environment can't use normal headers,
77// so use the equivalent system headers.
78// NB: FreeBSD (and OpenBSD) deprecate machine/limits.h in
79// favour of sys/limits.h, so prefer the former, but fall
80// back on the latter if not available since NetBSD only has
81// the latter.
82//
83#if defined(__has_include) && __has_include(<sys/limits.h>)
84#include <sys/limits.h>
85#else
86#include <machine/limits.h>
87#endif
88#include <sys/stdint.h>
89#include <sys/types.h>
90#else
91// Include the standard compiler builtin headers we use functionality from.
92#include <float.h>
93#include <limits.h>
94#include <stdbool.h>
95#include <stdint.h>
96#endif
dp-arm75b60572017-05-04 12:12:06 +010097
Daniel Boulby318e7a52022-10-21 20:20:52 +010098// Include the commonly used internal type definitions.
dp-arm75b60572017-05-04 12:12:06 +010099#include "int_types.h"
100
Daniel Boulby318e7a52022-10-21 20:20:52 +0100101// Include internal utility function declarations.
102#include "int_util.h"
103
104COMPILER_RT_ABI int __paritysi2(si_int a);
105COMPILER_RT_ABI int __paritydi2(di_int a);
dp-arm75b60572017-05-04 12:12:06 +0100106
107COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
108COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
109COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
110
Daniel Boulby318e7a52022-10-21 20:20:52 +0100111COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int *rem);
112COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int *rem);
dp-arm75b60572017-05-04 12:12:06 +0100113#ifdef CRT_HAS_128BIT
Daniel Boulby318e7a52022-10-21 20:20:52 +0100114COMPILER_RT_ABI int __clzti2(ti_int a);
115COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int *rem);
dp-arm75b60572017-05-04 12:12:06 +0100116#endif
117
Daniel Boulby318e7a52022-10-21 20:20:52 +0100118// Definitions for builtins unavailable on MSVC
dp-arm75b60572017-05-04 12:12:06 +0100119#if defined(_MSC_VER) && !defined(__clang__)
120#include <intrin.h>
121
Daniel Boulby318e7a52022-10-21 20:20:52 +0100122int __inline __builtin_ctz(uint32_t value) {
dp-arm75b60572017-05-04 12:12:06 +0100123 unsigned long trailing_zero = 0;
124 if (_BitScanForward(&trailing_zero, value))
125 return trailing_zero;
126 return 32;
127}
128
Daniel Boulby318e7a52022-10-21 20:20:52 +0100129int __inline __builtin_clz(uint32_t value) {
dp-arm75b60572017-05-04 12:12:06 +0100130 unsigned long leading_zero = 0;
131 if (_BitScanReverse(&leading_zero, value))
132 return 31 - leading_zero;
133 return 32;
134}
135
136#if defined(_M_ARM) || defined(_M_X64)
Daniel Boulby318e7a52022-10-21 20:20:52 +0100137int __inline __builtin_clzll(uint64_t value) {
dp-arm75b60572017-05-04 12:12:06 +0100138 unsigned long leading_zero = 0;
139 if (_BitScanReverse64(&leading_zero, value))
140 return 63 - leading_zero;
141 return 64;
142}
143#else
Daniel Boulby318e7a52022-10-21 20:20:52 +0100144int __inline __builtin_clzll(uint64_t value) {
dp-arm75b60572017-05-04 12:12:06 +0100145 if (value == 0)
146 return 64;
147 uint32_t msh = (uint32_t)(value >> 32);
148 uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
149 if (msh != 0)
150 return __builtin_clz(msh);
151 return 32 + __builtin_clz(lsh);
152}
153#endif
154
155#define __builtin_clzl __builtin_clzll
Daniel Boulby318e7a52022-10-21 20:20:52 +0100156
157bool __inline __builtin_sadd_overflow(int x, int y, int *result) {
158 if ((x < 0) != (y < 0)) {
159 *result = x + y;
160 return false;
161 }
162 int tmp = (unsigned int)x + (unsigned int)y;
163 if ((tmp < 0) != (x < 0))
164 return true;
165 *result = tmp;
166 return false;
167}
168
169#endif // defined(_MSC_VER) && !defined(__clang__)
dp-arm75b60572017-05-04 12:12:06 +0100170
Daniel Boulby318e7a52022-10-21 20:20:52 +0100171#endif // INT_LIB_H