2014-12-03 08:39:06 +00:00
|
|
|
/* Copyright (c) 2013-2014 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-10-12 01:18:47 +00:00
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
2014-11-22 08:33:41 +00:00
|
|
|
#include <inttypes.h>
|
2014-10-12 01:18:47 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-06-07 04:08:13 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#ifdef _WIN64
|
|
|
|
typedef int64_t off_t;
|
|
|
|
typedef int64_t ssize_t;
|
|
|
|
#else
|
|
|
|
typedef int32_t off_t;
|
|
|
|
typedef int32_t ssize_t;
|
|
|
|
#endif
|
|
|
|
#define restrict __restrict
|
|
|
|
#define SSIZE_MAX ((ssize_t) SIZE_MAX)
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#define ftruncate _chsize
|
|
|
|
#else
|
2015-03-23 09:47:10 +00:00
|
|
|
#include <strings.h>
|
2014-10-12 01:18:47 +00:00
|
|
|
#include <unistd.h>
|
2015-06-07 04:08:13 +00:00
|
|
|
#endif
|
2015-02-10 10:46:12 +00:00
|
|
|
|
2015-05-15 15:37:08 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2014-10-12 01:18:47 +00:00
|
|
|
#define UNUSED(V) (void)(V)
|
|
|
|
|
2015-04-21 09:27:54 +00:00
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.141592654f
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 05:50:44 +00:00
|
|
|
#if defined(__PPC__) || defined(__POWERPC__)
|
|
|
|
#define LOAD_32LE(DEST, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
|
|
|
asm("lwbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_16LE(DEST, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
|
|
|
asm("lhbrx %0, %1, %2" : "=r"(DEST) : "b"(_ptr), "r"(_addr)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_32LE(SRC, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
|
|
|
asm("stwbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STORE_16LE(SRC, ADDR, ARR) { \
|
|
|
|
uint32_t _addr = (ADDR); \
|
|
|
|
void* _ptr = (ARR); \
|
|
|
|
asm("sthbrx %0, %1, %2" : : "r"(SRC), "b"(_ptr), "r"(_addr)); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define LOAD_32LE(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
|
|
|
|
#define LOAD_16LE(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
|
|
|
|
#define STORE_32LE(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
|
|
|
|
#define STORE_16LE(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
|
|
|
|
#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
|
|
|
|
#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
|
|
|
|
#define INS_BITS(SRC, START, END, BITS) (CLEAR_BITS(SRC, START, END) | (((BITS) << (START)) & MAKE_MASK(START, END)))
|
|
|
|
#define CLEAR_BITS(SRC, START, END) ((SRC) & ~MAKE_MASK(START, END))
|
|
|
|
#define FILL_BITS(SRC, START, END) ((SRC) | MAKE_MASK(START, END))
|
|
|
|
|
2015-06-07 04:08:13 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define ATTRIBUTE_UNUSED
|
|
|
|
#define ATTRIBUTE_FORMAT(X, Y, Z)
|
|
|
|
#else
|
|
|
|
#define ATTRIBUTE_UNUSED __attribute__((unused))
|
|
|
|
#define ATTRIBUTE_FORMAT(X, Y, Z) __attribute__((format(X, Y, Z)))
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 05:50:44 +00:00
|
|
|
#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
|
|
|
|
|
|
|
|
#define DECL_BITS(TYPE, FIELD, START, SIZE) \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return CHECK_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return EXT_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Clear ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return CLEAR_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Fill ## FIELD (TYPE src) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return FILL_BITS(src, (START), (START) + (SIZE)); \
|
|
|
|
} \
|
2015-06-07 04:08:13 +00:00
|
|
|
ATTRIBUTE_UNUSED static inline TYPE TYPE ## Set ## FIELD (TYPE src, TYPE bits) { \
|
2015-05-29 05:50:44 +00:00
|
|
|
return INS_BITS(src, (START), (START) + (SIZE), bits); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
|
|
|
|
|
2015-06-08 04:20:54 +00:00
|
|
|
#ifndef _MSC_VER
|
2015-05-29 05:50:44 +00:00
|
|
|
#define LIKELY(X) __builtin_expect(!!(X), 1)
|
|
|
|
#define UNLIKELY(X) __builtin_expect(!!(X), 0)
|
2015-06-07 04:08:13 +00:00
|
|
|
#else
|
|
|
|
#define LIKELY(X) (!!(X))
|
|
|
|
#define UNLIKELY(X) (!!(X))
|
|
|
|
#endif
|
2015-05-29 05:50:44 +00:00
|
|
|
|
|
|
|
#define ROR(I, ROTATE) ((((uint32_t) (I)) >> ROTATE) | ((uint32_t) (I) << ((-ROTATE) & 31)))
|
|
|
|
|
2014-10-12 01:18:47 +00:00
|
|
|
#endif
|