blob: 7a72de480676475c02b826aa56a6cfab720f7ced [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 not part of the interface of this library.
10//
11// This file defines various standard types, most importantly a number of unions
12// used to access parts of larger types.
13//
14//===----------------------------------------------------------------------===//
dp-arm75b60572017-05-04 12:12:06 +010015
16#ifndef INT_TYPES_H
17#define INT_TYPES_H
18
19#include "int_endianness.h"
20
Daniel Boulby318e7a52022-10-21 20:20:52 +010021// si_int is defined in Linux sysroot's asm-generic/siginfo.h
dp-arm75b60572017-05-04 12:12:06 +010022#ifdef si_int
23#undef si_int
24#endif
Daniel Boulby318e7a52022-10-21 20:20:52 +010025typedef int32_t si_int;
26typedef uint32_t su_int;
27#if UINT_MAX == 0xFFFFFFFF
28#define clzsi __builtin_clz
29#define ctzsi __builtin_ctz
30#elif ULONG_MAX == 0xFFFFFFFF
31#define clzsi __builtin_clzl
32#define ctzsi __builtin_ctzl
33#else
34#error could not determine appropriate clzsi macro for this system
35#endif
dp-arm75b60572017-05-04 12:12:06 +010036
Daniel Boulby318e7a52022-10-21 20:20:52 +010037typedef int64_t di_int;
38typedef uint64_t du_int;
dp-arm75b60572017-05-04 12:12:06 +010039
Daniel Boulby318e7a52022-10-21 20:20:52 +010040typedef union {
41 di_int all;
42 struct {
dp-arm75b60572017-05-04 12:12:06 +010043#if _YUGA_LITTLE_ENDIAN
Daniel Boulby318e7a52022-10-21 20:20:52 +010044 su_int low;
45 si_int high;
dp-arm75b60572017-05-04 12:12:06 +010046#else
Daniel Boulby318e7a52022-10-21 20:20:52 +010047 si_int high;
48 su_int low;
49#endif // _YUGA_LITTLE_ENDIAN
50 } s;
dp-arm75b60572017-05-04 12:12:06 +010051} dwords;
52
Daniel Boulby318e7a52022-10-21 20:20:52 +010053typedef union {
54 du_int all;
55 struct {
dp-arm75b60572017-05-04 12:12:06 +010056#if _YUGA_LITTLE_ENDIAN
Daniel Boulby318e7a52022-10-21 20:20:52 +010057 su_int low;
58 su_int high;
dp-arm75b60572017-05-04 12:12:06 +010059#else
Daniel Boulby318e7a52022-10-21 20:20:52 +010060 su_int high;
61 su_int low;
62#endif // _YUGA_LITTLE_ENDIAN
63 } s;
dp-arm75b60572017-05-04 12:12:06 +010064} udwords;
65
Daniel Boulby318e7a52022-10-21 20:20:52 +010066#if defined(__LP64__) || defined(__wasm__) || defined(__mips64) || \
67 defined(__riscv) || defined(_WIN64)
dp-arm75b60572017-05-04 12:12:06 +010068#define CRT_HAS_128BIT
69#endif
70
Daniel Boulby318e7a52022-10-21 20:20:52 +010071// MSVC doesn't have a working 128bit integer type. Users should really compile
72// compiler-rt with clang, but if they happen to be doing a standalone build for
73// asan or something else, disable the 128 bit parts so things sort of work.
74#if defined(_MSC_VER) && !defined(__clang__)
75#undef CRT_HAS_128BIT
76#endif
77
dp-arm75b60572017-05-04 12:12:06 +010078#ifdef CRT_HAS_128BIT
Daniel Boulby318e7a52022-10-21 20:20:52 +010079typedef int ti_int __attribute__((mode(TI)));
80typedef unsigned tu_int __attribute__((mode(TI)));
dp-arm75b60572017-05-04 12:12:06 +010081
Daniel Boulby318e7a52022-10-21 20:20:52 +010082typedef union {
83 ti_int all;
84 struct {
dp-arm75b60572017-05-04 12:12:06 +010085#if _YUGA_LITTLE_ENDIAN
Daniel Boulby318e7a52022-10-21 20:20:52 +010086 du_int low;
87 di_int high;
dp-arm75b60572017-05-04 12:12:06 +010088#else
Daniel Boulby318e7a52022-10-21 20:20:52 +010089 di_int high;
90 du_int low;
91#endif // _YUGA_LITTLE_ENDIAN
92 } s;
dp-arm75b60572017-05-04 12:12:06 +010093} twords;
94
Daniel Boulby318e7a52022-10-21 20:20:52 +010095typedef union {
96 tu_int all;
97 struct {
dp-arm75b60572017-05-04 12:12:06 +010098#if _YUGA_LITTLE_ENDIAN
Daniel Boulby318e7a52022-10-21 20:20:52 +010099 du_int low;
100 du_int high;
dp-arm75b60572017-05-04 12:12:06 +0100101#else
Daniel Boulby318e7a52022-10-21 20:20:52 +0100102 du_int high;
103 du_int low;
104#endif // _YUGA_LITTLE_ENDIAN
105 } s;
dp-arm75b60572017-05-04 12:12:06 +0100106} utwords;
107
108static __inline ti_int make_ti(di_int h, di_int l) {
Daniel Boulby318e7a52022-10-21 20:20:52 +0100109 twords r;
110 r.s.high = h;
111 r.s.low = l;
112 return r.all;
dp-arm75b60572017-05-04 12:12:06 +0100113}
114
115static __inline tu_int make_tu(du_int h, du_int l) {
Daniel Boulby318e7a52022-10-21 20:20:52 +0100116 utwords r;
117 r.s.high = h;
118 r.s.low = l;
119 return r.all;
dp-arm75b60572017-05-04 12:12:06 +0100120}
121
Daniel Boulby318e7a52022-10-21 20:20:52 +0100122#endif // CRT_HAS_128BIT
dp-arm75b60572017-05-04 12:12:06 +0100123
Daniel Boulby318e7a52022-10-21 20:20:52 +0100124// FreeBSD's boot environment does not support using floating-point and poisons
125// the float and double keywords.
126#if defined(__FreeBSD__) && defined(_STANDALONE)
127#define CRT_HAS_FLOATING_POINT 0
128#else
129#define CRT_HAS_FLOATING_POINT 1
130#endif
131
132#if CRT_HAS_FLOATING_POINT
133typedef union {
134 su_int u;
135 float f;
dp-arm75b60572017-05-04 12:12:06 +0100136} float_bits;
137
Daniel Boulby318e7a52022-10-21 20:20:52 +0100138typedef union {
139 udwords u;
140 double f;
dp-arm75b60572017-05-04 12:12:06 +0100141} double_bits;
Daniel Boulby318e7a52022-10-21 20:20:52 +0100142#endif
dp-arm75b60572017-05-04 12:12:06 +0100143
Daniel Boulby318e7a52022-10-21 20:20:52 +0100144typedef struct {
dp-arm75b60572017-05-04 12:12:06 +0100145#if _YUGA_LITTLE_ENDIAN
Daniel Boulby318e7a52022-10-21 20:20:52 +0100146 udwords low;
147 udwords high;
dp-arm75b60572017-05-04 12:12:06 +0100148#else
Daniel Boulby318e7a52022-10-21 20:20:52 +0100149 udwords high;
150 udwords low;
151#endif // _YUGA_LITTLE_ENDIAN
dp-arm75b60572017-05-04 12:12:06 +0100152} uqwords;
153
Daniel Boulby318e7a52022-10-21 20:20:52 +0100154// Check if the target supports 80 bit extended precision long doubles.
155// Notably, on x86 Windows, MSVC only provides a 64-bit long double, but GCC
156// still makes it 80 bits. Clang will match whatever compiler it is trying to
157// be compatible with. On 32-bit x86 Android, long double is 64 bits, while on
158// x86_64 Android, long double is 128 bits.
159#if (defined(__i386__) || defined(__x86_64__)) && \
160 !(defined(_MSC_VER) || defined(__ANDROID__))
161#define HAS_80_BIT_LONG_DOUBLE 1
162#elif defined(__m68k__) || defined(__ia64__)
163#define HAS_80_BIT_LONG_DOUBLE 1
164#else
165#define HAS_80_BIT_LONG_DOUBLE 0
166#endif
167
168#if CRT_HAS_FLOATING_POINT
169typedef union {
170 uqwords u;
171 long double f;
dp-arm75b60572017-05-04 12:12:06 +0100172} long_double_bits;
173
174#if __STDC_VERSION__ >= 199901L
175typedef float _Complex Fcomplex;
176typedef double _Complex Dcomplex;
177typedef long double _Complex Lcomplex;
178
179#define COMPLEX_REAL(x) __real__(x)
180#define COMPLEX_IMAGINARY(x) __imag__(x)
181#else
Daniel Boulby318e7a52022-10-21 20:20:52 +0100182typedef struct {
183 float real, imaginary;
184} Fcomplex;
dp-arm75b60572017-05-04 12:12:06 +0100185
Daniel Boulby318e7a52022-10-21 20:20:52 +0100186typedef struct {
187 double real, imaginary;
188} Dcomplex;
dp-arm75b60572017-05-04 12:12:06 +0100189
Daniel Boulby318e7a52022-10-21 20:20:52 +0100190typedef struct {
191 long double real, imaginary;
192} Lcomplex;
dp-arm75b60572017-05-04 12:12:06 +0100193
194#define COMPLEX_REAL(x) (x).real
195#define COMPLEX_IMAGINARY(x) (x).imaginary
196#endif
Daniel Boulby318e7a52022-10-21 20:20:52 +0100197#endif
198#endif // INT_TYPES_H