shithub: femtolisp

ref: e43dbe8cc89f1945b25b7fdbac45e8e61f8273c1
dir: /llt/dtypes.h/

View raw version
#ifndef __DTYPES_H_
#define __DTYPES_H_

/*
  This file defines sane integer types for our target platforms. This
  library only runs on machines with the following characteristics:

  - supports integer word sizes of 8, 16, 32, and 64 bits
  - uses unsigned and signed 2's complement representations
  - all pointer types are the same size
  - there is an integer type with the same size as a pointer

  Some features require:
  - IEEE 754 single- and double-precision floating point

  We assume the LP64 convention for 64-bit platforms.
*/


#if defined(__gnu_linux__)
#  define LINUX
#elif defined(__OpenBSD__)
#  define OPENBSD
#elif defined(__FreeBSD__)
#  define FREEBSD
#elif defined(__NetBSD__)
#  define NETBSD
#elif !defined(PLAN9)
#  error "unknown platform"
#endif

#if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) || defined(PLAN9)
#if defined(__x86_64__) || defined(__amd64__) || defined(__arm64__)
#  define __SIZEOF_POINTER__ 8
#else
#  define __SIZEOF_POINTER__ 4
#endif
#endif

#if !defined (BITS32) && !defined (BITS64)
#ifndef __SIZEOF_POINTER__
#  error "__SIZEOF_POINTER__ undefined"
#endif
#if( 8 == __SIZEOF_POINTER__ )
#  define BITS64
#elif( 4 == __SIZEOF_POINTER__ )
#  define BITS32
#else
#  error "this is one weird machine"
#endif
#endif


#if defined(PLAN9)
#define STDCALL
#define DLLEXPORT
#else
#  define STDCALL
#  define DLLEXPORT __attribute__ ((visibility("default")))
#endif

#if defined(PLAN9)
#  define __LITTLE_ENDIAN 1234
#  define __BIG_ENDIAN    4321
#  define __BYTE_ORDER    __LITTLE_ENDIAN
#else
#  include <endian.h>
#endif

#ifndef BYTE_ORDER
#  define LITTLE_ENDIAN   __LITTLE_ENDIAN
#  define BIG_ENDIAN      __BIG_ENDIAN
#  define BYTE_ORDER      __BYTE_ORDER
#endif

#ifdef PLAN9
#define __attribute__(...)
#else
#define USED(x) (void)(x)
#endif

#ifdef BOEHM_GC
// boehm GC allocator
#include <gc.h>
#define LLT_ALLOC(n) GC_MALLOC(n)
#define LLT_REALLOC(p,n) GC_REALLOC((p),(n))
#define LLT_FREE(x) ((void)(x))
#else
// standard allocator
#define LLT_ALLOC(n) malloc(n)
#define LLT_REALLOC(p,n) realloc((p),(n))
#define LLT_FREE(x) free(x)
#endif

typedef int bool_t;

#if defined(BITS64)
#define ULONG64
#endif

#ifdef PLAN9
typedef usize size_t;
#ifdef BITS64
typedef long long ssize_t;
#else
typedef long ssize_t;
#endif
#define STATIC_INLINE static
#define INLINE
#ifndef NULL
#define NULL nil
#endif
#else
# define STATIC_INLINE static inline
# define INLINE inline
#endif

#if defined(PLAN9)
typedef s8int int8_t;
typedef s16int int16_t;
typedef s32int int32_t;
typedef s64int int64_t;
typedef u8int uint8_t;
typedef u16int uint16_t;
typedef u32int uint32_t;
typedef u64int uint64_t;
typedef vlong off_t;
typedef intptr intptr_t;
typedef uintptr uintptr_t;
#else
#include <sys/types.h>
#include <stdint.h>
#endif

#ifdef BITS64
#define TOP_BIT 0x8000000000000000ULL
#define NBITS 64
typedef uint64_t uint_t;  // preferred int type on platform
typedef int64_t int_t;
typedef int64_t offset_t;
typedef uint64_t index_t;
#else
#define TOP_BIT 0x80000000UL
#define NBITS 32
typedef uint32_t uint_t;
typedef int32_t int_t;
typedef int32_t offset_t;
typedef uint32_t index_t;
#endif

#define LLT_ALIGN(x, sz) (((x) + (sz-1)) & (-sz))

// branch prediction annotations
#ifdef __GNUC__
#define __unlikely(x) __builtin_expect(!!(x), 0)
#define __likely(x)   __builtin_expect(!!(x), 1)
#else
#define __unlikely(x) (x)
#define __likely(x)   (x)
#endif

#ifndef PLAN9
#include <float.h>
#endif

#define DBL_MAXINT 9007199254740992LL
#define FLT_MAXINT 16777216
#define U64_MAX    18446744073709551615ULL
#define S64_MAX    9223372036854775807LL
#define S64_MIN    (-S64_MAX - 1LL)
#define BIT63      0x8000000000000000ULL
#define U32_MAX    4294967295UL
#define S32_MAX    2147483647L
#define S32_MIN    (-S32_MAX - 1L)
#define BIT31      0x80000000UL

#define LOG2_10          3.3219280948873626
#define rel_zero(a, b) (fabs((a)/(b)) < DBL_EPSILON)
#define sign_bit(r) ((*(uint64_t*)&(r)) & BIT63)
#define LABS(n) (((n)^((n)>>(NBITS-1))) - ((n)>>(NBITS-1)))
#define NBABS(n,nb) (((n)^((n)>>((nb)-1))) - ((n)>>((nb)-1)))
#define DFINITE(d) (((*(uint64_t*)&(d))&0x7ff0000000000000ULL)!=0x7ff0000000000000ULL)

extern double D_PNAN;
extern double D_NNAN;
extern double D_PINF;
extern double D_NINF;
extern float  F_PNAN;
extern float  F_NNAN;
extern float  F_PINF;
extern float  F_NINF;

#endif