2006-04-13 20:47:06 +00:00
|
|
|
// This file has been modified from Ken Silverman's original release
|
2012-03-12 04:47:04 +00:00
|
|
|
// by Jonathon Fowler (jf@jonof.id.au)
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#ifndef pragmas_h_
|
|
|
|
#define pragmas_h_
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-11-26 04:39:23 +00:00
|
|
|
#ifdef __cplusplus
|
2006-07-02 17:33:49 +00:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2009-09-09 07:19:14 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#define EDUKE32_GENERATE_PRAGMAS \
|
|
|
|
EDUKE32_SCALER_PRAGMA(1) EDUKE32_SCALER_PRAGMA(2) EDUKE32_SCALER_PRAGMA(3) EDUKE32_SCALER_PRAGMA(4) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(5) EDUKE32_SCALER_PRAGMA(6) EDUKE32_SCALER_PRAGMA(7) EDUKE32_SCALER_PRAGMA(8) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(9) EDUKE32_SCALER_PRAGMA(10) EDUKE32_SCALER_PRAGMA(11) EDUKE32_SCALER_PRAGMA(12) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(13) EDUKE32_SCALER_PRAGMA(14) EDUKE32_SCALER_PRAGMA(15) EDUKE32_SCALER_PRAGMA(16) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(17) EDUKE32_SCALER_PRAGMA(18) EDUKE32_SCALER_PRAGMA(19) EDUKE32_SCALER_PRAGMA(20) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(21) EDUKE32_SCALER_PRAGMA(22) EDUKE32_SCALER_PRAGMA(23) EDUKE32_SCALER_PRAGMA(24) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(25) EDUKE32_SCALER_PRAGMA(26) EDUKE32_SCALER_PRAGMA(27) EDUKE32_SCALER_PRAGMA(28) \
|
|
|
|
EDUKE32_SCALER_PRAGMA(29) EDUKE32_SCALER_PRAGMA(30) EDUKE32_SCALER_PRAGMA(31)
|
2014-09-30 04:04:53 +00:00
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
extern int32_t dmval;
|
2014-10-25 03:30:38 +00:00
|
|
|
#if !defined(NOASM) && defined __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
extern int32_t reciptable[2048], fpuasm;
|
|
|
|
#if !defined(NOASM) && defined __cplusplus
|
2014-11-26 04:39:23 +00:00
|
|
|
}
|
2014-10-25 03:30:38 +00:00
|
|
|
#endif
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2009-09-09 07:19:14 +00:00
|
|
|
// break the C version of divscale out from the others
|
|
|
|
// because asm version overflows in drawmapview()
|
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#define qw(x) ((int64_t)(x)) // quadword cast
|
|
|
|
#define dw(x) ((int32_t)(x)) // doubleword cast
|
|
|
|
#define wo(x) ((int16_t)(x)) // word cast
|
|
|
|
#define by(x) ((uint8_t)(x)) // byte cast
|
2009-09-09 07:19:14 +00:00
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
#define LIBDIVIDE_ALWAYS
|
|
|
|
#define DIVTABLESIZE 16384
|
2014-08-23 10:28:17 +00:00
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
extern libdivide_s64pad_t divtable64[DIVTABLESIZE];
|
|
|
|
extern libdivide_s32pad_t divtable32[DIVTABLESIZE];
|
2015-09-27 21:18:12 +00:00
|
|
|
extern void initdivtables(void);
|
2014-10-25 03:29:21 +00:00
|
|
|
|
|
|
|
#if defined(__arm__) || defined(LIBDIVIDE_ALWAYS)
|
|
|
|
static inline uint32_t divideu32(uint32_t n, uint32_t d)
|
2012-05-01 12:37:32 +00:00
|
|
|
{
|
2014-10-25 03:29:21 +00:00
|
|
|
static libdivide_u32_t udiv;
|
|
|
|
static uint32_t lastd;
|
|
|
|
|
|
|
|
if (d == lastd)
|
|
|
|
goto skip;
|
|
|
|
|
2016-06-21 00:32:37 +00:00
|
|
|
udiv = libdivide_u32_gen((lastd = d));
|
2014-10-25 03:29:21 +00:00
|
|
|
skip:
|
|
|
|
return libdivide_u32_do(n, &udiv);
|
2012-05-01 12:37:32 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
static inline int32_t tabledivide64(int64_t n, int32_t d)
|
|
|
|
{
|
|
|
|
static libdivide_s64_t sdiv;
|
|
|
|
static int32_t lastd;
|
2015-04-18 21:37:07 +00:00
|
|
|
libdivide_s64_t const * const dptr = ((unsigned)d < DIVTABLESIZE) ? (libdivide_s64_t *)&divtable64[d] : &sdiv;
|
2012-05-01 12:37:32 +00:00
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
if (d == lastd || dptr != &sdiv)
|
|
|
|
goto skip;
|
|
|
|
|
2016-06-21 00:32:37 +00:00
|
|
|
sdiv = libdivide_s64_gen((lastd = d));
|
2014-10-25 03:29:21 +00:00
|
|
|
skip:
|
|
|
|
return libdivide_s64_do(n, dptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t tabledivide32(int32_t n, int32_t d)
|
|
|
|
{
|
|
|
|
static libdivide_s32_t sdiv;
|
|
|
|
static int32_t lastd;
|
2015-04-18 21:37:07 +00:00
|
|
|
libdivide_s32_t const * const dptr = ((unsigned)d < DIVTABLESIZE) ? (libdivide_s32_t *)&divtable32[d] : &sdiv;
|
2014-10-25 03:29:21 +00:00
|
|
|
|
|
|
|
if (d == lastd || dptr != &sdiv)
|
|
|
|
goto skip;
|
|
|
|
|
2016-06-21 00:32:37 +00:00
|
|
|
sdiv = libdivide_s32_gen((lastd = d));
|
2014-10-25 03:29:21 +00:00
|
|
|
skip:
|
|
|
|
return libdivide_s32_do(n, dptr);
|
|
|
|
}
|
2012-05-01 12:37:32 +00:00
|
|
|
#else
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE uint32_t divideu32(uint32_t n, uint32_t d) { return n / d; }
|
2012-05-01 12:37:32 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
static inline int32_t tabledivide64(int64_t n, int32_t d)
|
|
|
|
{
|
|
|
|
return ((unsigned)d < DIVTABLESIZE) ? libdivide_s64_do(n, (libdivide_s64_t *)&divtable64[d]) : n / d;
|
|
|
|
}
|
2009-09-09 07:19:14 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
static inline int32_t tabledivide32(int32_t n, int32_t d)
|
|
|
|
{
|
|
|
|
return ((unsigned)d < DIVTABLESIZE) ? libdivide_s32_do(n, (libdivide_s32_t *)&divtable32[d]) : n / d;
|
|
|
|
}
|
2012-05-01 12:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
extern uint32_t divideu32_noinline(uint32_t n, uint32_t d);
|
|
|
|
extern int32_t tabledivide32_noinline(int32_t n, int32_t d);
|
|
|
|
extern int32_t tabledivide64_noinline(int64_t n, int32_t d);
|
2009-09-09 07:19:14 +00:00
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
#ifdef GEKKO
|
|
|
|
#include <math.h>
|
2014-11-02 05:35:22 +00:00
|
|
|
static inline int32_t divscale(int32_t eax, int32_t ebx, int32_t ecx) { return tabledivide64(ldexp(eax, ecx), ebx); }
|
2014-10-25 03:29:21 +00:00
|
|
|
#else
|
|
|
|
static inline int32_t divscale(int32_t eax, int32_t ebx, int32_t ecx)
|
|
|
|
{
|
|
|
|
const int64_t numer = qw(eax) << by(ecx);
|
|
|
|
return dw(tabledivide64(numer, ebx));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#define EDUKE32_SCALER_PRAGMA(a) \
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE int32_t divscale##a(int32_t eax, int32_t ebx) { return divscale(eax, ebx, a); }
|
2014-10-29 17:03:29 +00:00
|
|
|
EDUKE32_GENERATE_PRAGMAS EDUKE32_SCALER_PRAGMA(32)
|
|
|
|
#undef EDUKE32_SCALER_PRAGMA
|
2014-10-25 03:29:21 +00:00
|
|
|
|
|
|
|
static inline int32_t scale(int32_t eax, int32_t edx, int32_t ecx)
|
|
|
|
{
|
|
|
|
const int64_t numer = qw(eax) * edx;
|
|
|
|
return dw(tabledivide64(numer, ecx));
|
|
|
|
}
|
2009-09-09 07:19:14 +00:00
|
|
|
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swapptr(void *a, void *b)
|
2015-07-08 03:33:43 +00:00
|
|
|
{
|
2015-07-17 00:12:40 +00:00
|
|
|
intptr_t const t = *(intptr_t*) a;
|
2015-07-08 03:33:43 +00:00
|
|
|
*(intptr_t*) a = *(intptr_t*) b;
|
|
|
|
*(intptr_t*) b = t;
|
|
|
|
}
|
|
|
|
|
2016-05-23 04:46:18 +00:00
|
|
|
FORCE_INLINE int32_t sqr(int32_t a) { return a * a; }
|
|
|
|
|
2012-05-01 12:37:32 +00:00
|
|
|
#if defined(__GNUC__) && defined(GEKKO)
|
|
|
|
|
|
|
|
// GCC Inline Assembler version (PowerPC)
|
2014-09-30 04:04:53 +00:00
|
|
|
#include "pragmas_ppc.h"
|
2012-05-01 12:37:32 +00:00
|
|
|
|
|
|
|
#elif defined(__GNUC__) && defined(__i386__) && !defined(NOASM)
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2012-05-01 12:37:32 +00:00
|
|
|
// GCC Inline Assembler version (x86)
|
2014-09-30 04:04:53 +00:00
|
|
|
#include "pragmas_x86_gcc.h"
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#elif defined(_MSC_VER) && !defined(NOASM) // __GNUC__
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
// Microsoft C inline assembler
|
2014-09-30 04:04:53 +00:00
|
|
|
#include "pragmas_x86_msvc.h"
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#elif defined(__arm__) // _MSC_VER
|
2014-08-23 10:28:17 +00:00
|
|
|
|
2014-09-30 04:04:53 +00:00
|
|
|
// GCC Inline Assembler version (ARM)
|
|
|
|
#include "pragmas_arm.h"
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-09-30 04:04:53 +00:00
|
|
|
#else
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-07-01 01:40:18 +00:00
|
|
|
//
|
|
|
|
// Generic C
|
|
|
|
//
|
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
#define EDUKE32_SCALER_PRAGMA(a) \
|
2017-01-23 11:21:18 +00:00
|
|
|
FORCE_INLINE int32_t mulscale##a(int32_t eax, int32_t edx) { return dw((qw(eax) * edx) >> by(a)); } \
|
2016-06-21 00:32:37 +00:00
|
|
|
FORCE_INLINE int32_t dmulscale##a(int32_t eax, int32_t edx, int32_t esi, int32_t edi) \
|
2014-11-02 05:35:22 +00:00
|
|
|
{ \
|
2017-01-23 11:21:18 +00:00
|
|
|
return dw(((qw(eax) * edx) + (qw(esi) * edi)) >> by(a)); \
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 17:03:29 +00:00
|
|
|
EDUKE32_GENERATE_PRAGMAS EDUKE32_SCALER_PRAGMA(32)
|
2014-09-30 04:04:53 +00:00
|
|
|
|
2014-10-29 17:03:29 +00:00
|
|
|
#undef EDUKE32_SCALER_PRAGMA
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swapchar(void *a, void *b)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
char const t = *(char *)b;
|
|
|
|
*(char *)b = *(char *)a;
|
|
|
|
*(char *)a = t;
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swapchar2(void *a, void *b, int32_t s)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
|
|
|
swapchar(a, b);
|
|
|
|
swapchar((char *)a + 1, (char *)b + s);
|
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swapshort(void *a, void *b)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
int16_t const t = *(int16_t *)b;
|
|
|
|
*(int16_t *)b = *(int16_t *)a;
|
|
|
|
*(int16_t *)a = t;
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swaplong(void *a, void *b)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
int32_t const t = *(int32_t *)b;
|
|
|
|
*(int32_t *)b = *(int32_t *)a;
|
|
|
|
*(int32_t *)a = t;
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swapfloat(void *a, void *b)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
float const t = *(float *)b;
|
|
|
|
*(float *)b = *(float *)a;
|
|
|
|
*(float *)a = t;
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
2016-05-13 22:15:25 +00:00
|
|
|
FORCE_INLINE void swapdouble(void *a, void *b)
|
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
double const t = *(double *)b;
|
|
|
|
*(double *)b = *(double *)a;
|
|
|
|
*(double *)a = t;
|
2016-05-13 22:15:25 +00:00
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE void swap64bit(void *a, void *b)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2017-01-23 11:21:18 +00:00
|
|
|
uint64_t const t = *(uint64_t *)b;
|
|
|
|
*(uint64_t *)b = *(uint64_t *)a;
|
|
|
|
*(uint64_t *)a = t;
|
2014-11-02 05:35:22 +00:00
|
|
|
}
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2017-01-23 11:21:18 +00:00
|
|
|
FORCE_INLINE char readpixel(void *s) { return *(char *)s; }
|
|
|
|
FORCE_INLINE void drawpixel(void *s, char a) { *(char *)s = a; }
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE int32_t klabs(int32_t a)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
2016-05-13 22:15:25 +00:00
|
|
|
const uint32_t m = a >> (sizeof(uint32_t) * CHAR_BIT - 1);
|
2014-11-02 05:35:22 +00:00
|
|
|
return (a ^ m) - m;
|
|
|
|
}
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE int32_t ksgn(int32_t a) { return (a > 0) - (a < 0); }
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2015-08-02 08:50:27 +00:00
|
|
|
FORCE_INLINE int32_t mulscale(int32_t eax, int32_t edx, int32_t ecx) { return dw((qw(eax) * edx) >> by(ecx)); }
|
|
|
|
FORCE_INLINE int32_t dmulscale(int32_t eax, int32_t edx, int32_t esi, int32_t edi, int32_t ecx)
|
2014-11-02 05:35:22 +00:00
|
|
|
{
|
|
|
|
return dw(((qw(eax) * edx) + (qw(esi) * edi)) >> by(ecx));
|
|
|
|
}
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
void qinterpolatedown16(intptr_t bufptr, int32_t num, int32_t val, int32_t add);
|
|
|
|
void qinterpolatedown16short(intptr_t bufptr, int32_t num, int32_t val, int32_t add);
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2014-11-02 05:35:22 +00:00
|
|
|
void clearbuf(void *d, int32_t c, int32_t a);
|
|
|
|
void copybuf(const void *s, void *d, int32_t c);
|
|
|
|
void swapbuf4(void *a, void *b, int32_t c);
|
2006-07-01 01:40:18 +00:00
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
void clearbufbyte(void *D, int32_t c, int32_t a);
|
2012-02-18 22:14:45 +00:00
|
|
|
void copybufbyte(const void *S, void *D, int32_t c);
|
|
|
|
void copybufreverse(const void *S, void *D, int32_t c);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-10-25 03:30:38 +00:00
|
|
|
static inline int32_t krecipasm(int32_t i)
|
|
|
|
{
|
|
|
|
// Ken did this
|
2016-01-11 05:05:38 +00:00
|
|
|
float const f = (float const)i;
|
|
|
|
i = *(int32_t const *)&f;
|
2014-11-02 05:35:22 +00:00
|
|
|
return ((reciptable[(i >> 12) & 2047] >> (((i - 0x3f800000) >> 23) & 31)) ^ (i >> 31));
|
2014-10-25 03:30:38 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-30 04:04:53 +00:00
|
|
|
#undef qw
|
|
|
|
#undef dw
|
|
|
|
#undef wo
|
|
|
|
#undef by
|
|
|
|
|
2014-03-05 21:12:59 +00:00
|
|
|
static inline void swapbufreverse(void *s, void *d, int32_t c)
|
|
|
|
{
|
2014-11-02 05:35:22 +00:00
|
|
|
uint8_t *src = (uint8_t *)s, *dst = (uint8_t *)d;
|
2015-02-17 16:23:04 +00:00
|
|
|
Bassert(c >= 4);
|
|
|
|
|
2014-10-25 03:29:21 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
swapchar(dst, src);
|
2014-11-02 05:35:22 +00:00
|
|
|
swapchar(dst + 1, src - 1);
|
|
|
|
swapchar(dst + 2, src - 2);
|
|
|
|
swapchar(dst + 3, src - 3);
|
2014-10-25 03:29:21 +00:00
|
|
|
dst += 4, src -= 4;
|
2015-02-17 16:23:04 +00:00
|
|
|
} while ((c -= 4) > 4);
|
|
|
|
|
|
|
|
while (c--)
|
|
|
|
swapchar(dst++, src--);
|
2014-03-05 21:12:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 04:39:23 +00:00
|
|
|
#ifdef __cplusplus
|
2006-07-02 17:33:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#endif // pragmas_h_
|