1 /* ===-- int_lib.h - configuration header for compiler-rt  -----------------===
   2  *
   3  *                     The LLVM Compiler Infrastructure
   4  *
   5  * This file is dual licensed under the MIT and the University of Illinois Open
   6  * Source Licenses. See LICENSE.TXT for details.
   7  *
   8  * ===----------------------------------------------------------------------===
   9  *
  10  * This file is a configuration header for compiler-rt.
  11  * This file is not part of the interface of this library.
  12  *
  13  * ===----------------------------------------------------------------------===
  14  */
  15 
  16 #ifndef INT_LIB_H
  17 #define INT_LIB_H
  18 
  19 /* 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 
  23 #if defined(__ELF__)
  24 #define FNALIAS(alias_name, original_name) \
  25   void alias_name() __attribute__((alias(#original_name)))
  26 #else
  27 #define FNALIAS(alias, name) _Pragma("GCC error(\"alias unsupported on this file format\")")
  28 #endif
  29 
  30 /* ABI macro definitions */
  31 
  32 #if __ARM_EABI__
  33 # define ARM_EABI_FNALIAS(aeabi_name, name)         \
  34   void __aeabi_##aeabi_name() __attribute__((alias("__" #name)));
  35 # define COMPILER_RT_ABI __attribute__((pcs("aapcs")))
  36 #else
  37 # define ARM_EABI_FNALIAS(aeabi_name, name)
  38 # define COMPILER_RT_ABI
  39 #endif
  40 
  41 #ifdef _MSC_VER
  42 #define ALWAYS_INLINE __forceinline
  43 #define NOINLINE __declspec(noinline)
  44 #define NORETURN __declspec(noreturn)
  45 #define UNUSED
  46 #else
  47 #define ALWAYS_INLINE __attribute__((always_inline))
  48 #define NOINLINE __attribute__((noinline))
  49 #define NORETURN __attribute__((noreturn))
  50 #define UNUSED __attribute__((unused))
  51 #endif
  52 
  53 #if defined(__NetBSD__) && (defined(_KERNEL) || defined(_STANDALONE))
  54 /*
  55  * Kernel and boot environment can't use normal headers,
  56  * so use the equivalent system headers.
  57  */
  58 #  include <machine/limits.h>
  59 #  include <sys/stdint.h>
  60 #  include <sys/types.h>
  61 #else
  62 /* Include the standard compiler builtin headers we use functionality from. */
  63 #  include <limits.h>
  64 #  include <stdint.h>
  65 #  include <stdbool.h>
  66 #endif
  67 
  68 /* Include the commonly used internal type definitions. */
  69 #include "int_types.h"
  70 
  71 /* Include internal utility function declarations. */
  72 #include "int_util.h"
  73 
  74 COMPILER_RT_ABI si_int __paritysi2(si_int a);
  75 COMPILER_RT_ABI si_int __paritydi2(di_int a);
  76 
  77 COMPILER_RT_ABI di_int __divdi3(di_int a, di_int b);
  78 COMPILER_RT_ABI si_int __divsi3(si_int a, si_int b);
  79 COMPILER_RT_ABI su_int __udivsi3(su_int n, su_int d);
  80 
  81 COMPILER_RT_ABI su_int __udivmodsi4(su_int a, su_int b, su_int* rem);
  82 COMPILER_RT_ABI du_int __udivmoddi4(du_int a, du_int b, du_int* rem);
  83 #ifdef CRT_HAS_128BIT
  84 COMPILER_RT_ABI si_int __clzti2(ti_int a);
  85 COMPILER_RT_ABI tu_int __udivmodti4(tu_int a, tu_int b, tu_int* rem);
  86 #endif
  87 
  88 /* Definitions for builtins unavailable on MSVC */
  89 #if defined(_MSC_VER) && !defined(__clang__)
  90 #include <intrin.h>
  91 
  92 uint32_t __inline __builtin_ctz(uint32_t value) {
  93   unsigned long trailing_zero = 0;
  94   if (_BitScanForward(&trailing_zero, value))
  95     return trailing_zero;
  96   return 32;
  97 }
  98 
  99 uint32_t __inline __builtin_clz(uint32_t value) {
 100   unsigned long leading_zero = 0;
 101   if (_BitScanReverse(&leading_zero, value))
 102     return 31 - leading_zero;
 103   return 32;
 104 }
 105 
 106 #if defined(_M_ARM) || defined(_M_X64)
 107 uint32_t __inline __builtin_clzll(uint64_t value) {
 108   unsigned long leading_zero = 0;
 109   if (_BitScanReverse64(&leading_zero, value))
 110     return 63 - leading_zero;
 111   return 64;
 112 }
 113 #else
 114 uint32_t __inline __builtin_clzll(uint64_t value) {
 115   if (value == 0)
 116     return 64;
 117   uint32_t msh = (uint32_t)(value >> 32);
 118   uint32_t lsh = (uint32_t)(value & 0xFFFFFFFF);
 119   if (msh != 0)
 120     return __builtin_clz(msh);
 121   return 32 + __builtin_clz(lsh);
 122 }
 123 #endif
 124 
 125 #define __builtin_clzl __builtin_clzll
 126 #endif /* defined(_MSC_VER) && !defined(__clang__) */
 127 
 128 #endif /* INT_LIB_H */