mirror of
https://github.com/Q3Rally-Team/q3rally.git
synced 2024-11-29 23:22:55 +00:00
0d5fb492cd
Fix GCC 6 misleading-indentation warning add SECURITY.md OpenGL2: Restore adding fixed ambient light when HDR is enabled Few LCC memory fixes. fix a few potential buffer overwrite in Game VM Enable compiler optimization on all macOS architectures Don't allow qagame module to create "botlib.log" at ANY filesystem location Make FS_BuildOSPath for botlib.log consistent with typical usage tiny readme thing Remove extra plus sign from Huff_Compress() Fix VMs being able to change CVAR_PROTECTED cvars Don't register fs_game cvar everywhere just to get the value Don't let VMs change engine latch cvars immediately Fix fs_game '..' reading outside of home and base path Fix VMs forcing engine latch cvar to update to latched value Revert my recent cvar latch changes Revert "Don't let VMs change engine latch cvars immediately" Partially revert "Fix fs_game '..' reading outside of home and base path" Revert "Fix VMs forcing engine latch cvar to update to latched value" Fix exploit to bypass filename restrictions on Windows Changes to systemd q3a.service Fix Q_vsnprintf for mingw-w64 Fix timelimit causing an infinite map ending loop Fix invalid access to cluster 0 in AAS_AreaRouteToGoalArea() Fix negative frag/capturelimit causing an infinite map end loop OpenGL2: Fix dark lightmap on shader in mpteam6 Make FS_InvalidGameDir() consider subdirectories invalid [qcommon] Remove dead serialization code [qcommon] Make several zone variables and functions static. Fix MAC_OS_X_VERSION_MIN_REQUIRED for macOS 10.10 and later Increase q3_ui .arena filename list buffer size to 4096 bytes OpenGL2: Fix crash when BSP has deluxe maps and vertex lit surfaces Support Unicode characters greater than 0xFF in cl_consoleKeys Fix macOS app bundle with space in name OpenGL1: Use glGenTextures instead of hardcoded values Remove CON_FlushIn function and where STDIN needs flushing, use tcflush POSIX function Update libogg from 1.3.2 to 1.3.3 Rename (already updated) libogg-1.3.2 to libogg-1.3.3 Update libvorbis from 1.3.5 to 1.3.6 * Fix CVE-2018-5146 - out-of-bounds write on codebook decoding. * Fix CVE-2017-14632 - free() on unitialized data * Fix CVE-2017-14633 - out-of-bounds read Rename (already updated) libvorbis-1.3.5 to libvorbis-1.3.6 Update opus from 1.1.4 to 1.2.1 Rename (already updated) opus-1.1.4 to opus-1.2.1 Update opusfile from 0.8 to 0.9 Rename (already updated) opusfile-0.8 to opusfile-0.9 First swing at a CONTRIBUTING.md Allow loading system OpenAL library on macOS again Remove duplicate setting of FREETYPE_CFLAGS in Makefile Fix exploit to reset player by sending wrong serverId Fix "Going to CS_ZOMBIE for [clientname]" developer message Fix MSG_Read*String*() functions not being able to read last byte from message
290 lines
7.8 KiB
C
290 lines
7.8 KiB
C
/* Copyright (c) 2002-2008 Jean-Marc Valin
|
|
Copyright (c) 2007-2008 CSIRO
|
|
Copyright (c) 2007-2009 Xiph.Org Foundation
|
|
Written by Jean-Marc Valin */
|
|
/**
|
|
@file mathops.h
|
|
@brief Various math functions
|
|
*/
|
|
/*
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions
|
|
are met:
|
|
|
|
- Redistributions of source code must retain the above copyright
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
- Redistributions in binary form must reproduce the above copyright
|
|
notice, this list of conditions and the following disclaimer in the
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef MATHOPS_H
|
|
#define MATHOPS_H
|
|
|
|
#include "arch.h"
|
|
#include "entcode.h"
|
|
#include "os_support.h"
|
|
|
|
#define PI 3.141592653f
|
|
|
|
/* Multiplies two 16-bit fractional values. Bit-exactness of this macro is important */
|
|
#define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>15)
|
|
|
|
unsigned isqrt32(opus_uint32 _val);
|
|
|
|
/* CELT doesn't need it for fixed-point, by analysis.c does. */
|
|
#if !defined(FIXED_POINT) || defined(ANALYSIS_C)
|
|
#define cA 0.43157974f
|
|
#define cB 0.67848403f
|
|
#define cC 0.08595542f
|
|
#define cE ((float)PI/2)
|
|
static OPUS_INLINE float fast_atan2f(float y, float x) {
|
|
float x2, y2;
|
|
x2 = x*x;
|
|
y2 = y*y;
|
|
/* For very small values, we don't care about the answer, so
|
|
we can just return 0. */
|
|
if (x2 + y2 < 1e-18f)
|
|
{
|
|
return 0;
|
|
}
|
|
if(x2<y2){
|
|
float den = (y2 + cB*x2) * (y2 + cC*x2);
|
|
return -x*y*(y2 + cA*x2) / den + (y<0 ? -cE : cE);
|
|
}else{
|
|
float den = (x2 + cB*y2) * (x2 + cC*y2);
|
|
return x*y*(x2 + cA*y2) / den + (y<0 ? -cE : cE) - (x*y<0 ? -cE : cE);
|
|
}
|
|
}
|
|
#undef cA
|
|
#undef cB
|
|
#undef cC
|
|
#undef cD
|
|
#endif
|
|
|
|
|
|
#ifndef OVERRIDE_CELT_MAXABS16
|
|
static OPUS_INLINE opus_val32 celt_maxabs16(const opus_val16 *x, int len)
|
|
{
|
|
int i;
|
|
opus_val16 maxval = 0;
|
|
opus_val16 minval = 0;
|
|
for (i=0;i<len;i++)
|
|
{
|
|
maxval = MAX16(maxval, x[i]);
|
|
minval = MIN16(minval, x[i]);
|
|
}
|
|
return MAX32(EXTEND32(maxval),-EXTEND32(minval));
|
|
}
|
|
#endif
|
|
|
|
#ifndef OVERRIDE_CELT_MAXABS32
|
|
#ifdef FIXED_POINT
|
|
static OPUS_INLINE opus_val32 celt_maxabs32(const opus_val32 *x, int len)
|
|
{
|
|
int i;
|
|
opus_val32 maxval = 0;
|
|
opus_val32 minval = 0;
|
|
for (i=0;i<len;i++)
|
|
{
|
|
maxval = MAX32(maxval, x[i]);
|
|
minval = MIN32(minval, x[i]);
|
|
}
|
|
return MAX32(maxval, -minval);
|
|
}
|
|
#else
|
|
#define celt_maxabs32(x,len) celt_maxabs16(x,len)
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#ifndef FIXED_POINT
|
|
|
|
#define celt_sqrt(x) ((float)sqrt(x))
|
|
#define celt_rsqrt(x) (1.f/celt_sqrt(x))
|
|
#define celt_rsqrt_norm(x) (celt_rsqrt(x))
|
|
#define celt_cos_norm(x) ((float)cos((.5f*PI)*(x)))
|
|
#define celt_rcp(x) (1.f/(x))
|
|
#define celt_div(a,b) ((a)/(b))
|
|
#define frac_div32(a,b) ((float)(a)/(b))
|
|
|
|
#ifdef FLOAT_APPROX
|
|
|
|
/* Note: This assumes radix-2 floating point with the exponent at bits 23..30 and an offset of 127
|
|
denorm, +/- inf and NaN are *not* handled */
|
|
|
|
/** Base-2 log approximation (log2(x)). */
|
|
static OPUS_INLINE float celt_log2(float x)
|
|
{
|
|
int integer;
|
|
float frac;
|
|
union {
|
|
float f;
|
|
opus_uint32 i;
|
|
} in;
|
|
in.f = x;
|
|
integer = (in.i>>23)-127;
|
|
in.i -= integer<<23;
|
|
frac = in.f - 1.5f;
|
|
frac = -0.41445418f + frac*(0.95909232f
|
|
+ frac*(-0.33951290f + frac*0.16541097f));
|
|
return 1+integer+frac;
|
|
}
|
|
|
|
/** Base-2 exponential approximation (2^x). */
|
|
static OPUS_INLINE float celt_exp2(float x)
|
|
{
|
|
int integer;
|
|
float frac;
|
|
union {
|
|
float f;
|
|
opus_uint32 i;
|
|
} res;
|
|
integer = floor(x);
|
|
if (integer < -50)
|
|
return 0;
|
|
frac = x-integer;
|
|
/* K0 = 1, K1 = log(2), K2 = 3-4*log(2), K3 = 3*log(2) - 2 */
|
|
res.f = 0.99992522f + frac * (0.69583354f
|
|
+ frac * (0.22606716f + 0.078024523f*frac));
|
|
res.i = (res.i + (integer<<23)) & 0x7fffffff;
|
|
return res.f;
|
|
}
|
|
|
|
#else
|
|
#define celt_log2(x) ((float)(1.442695040888963387*log(x)))
|
|
#define celt_exp2(x) ((float)exp(0.6931471805599453094*(x)))
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
#include "os_support.h"
|
|
|
|
#ifndef OVERRIDE_CELT_ILOG2
|
|
/** Integer log in base2. Undefined for zero and negative numbers */
|
|
static OPUS_INLINE opus_int16 celt_ilog2(opus_int32 x)
|
|
{
|
|
celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers");
|
|
return EC_ILOG(x)-1;
|
|
}
|
|
#endif
|
|
|
|
|
|
/** Integer log in base2. Defined for zero, but not for negative numbers */
|
|
static OPUS_INLINE opus_int16 celt_zlog2(opus_val32 x)
|
|
{
|
|
return x <= 0 ? 0 : celt_ilog2(x);
|
|
}
|
|
|
|
opus_val16 celt_rsqrt_norm(opus_val32 x);
|
|
|
|
opus_val32 celt_sqrt(opus_val32 x);
|
|
|
|
opus_val16 celt_cos_norm(opus_val32 x);
|
|
|
|
/** Base-2 logarithm approximation (log2(x)). (Q14 input, Q10 output) */
|
|
static OPUS_INLINE opus_val16 celt_log2(opus_val32 x)
|
|
{
|
|
int i;
|
|
opus_val16 n, frac;
|
|
/* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605,
|
|
0.15530808010959576, -0.08556153059057618 */
|
|
static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545, -1401};
|
|
if (x==0)
|
|
return -32767;
|
|
i = celt_ilog2(x);
|
|
n = VSHR32(x,i-15)-32768-16384;
|
|
frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4]))))))));
|
|
return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT);
|
|
}
|
|
|
|
/*
|
|
K0 = 1
|
|
K1 = log(2)
|
|
K2 = 3-4*log(2)
|
|
K3 = 3*log(2) - 2
|
|
*/
|
|
#define D0 16383
|
|
#define D1 22804
|
|
#define D2 14819
|
|
#define D3 10204
|
|
|
|
static OPUS_INLINE opus_val32 celt_exp2_frac(opus_val16 x)
|
|
{
|
|
opus_val16 frac;
|
|
frac = SHL16(x, 4);
|
|
return ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac))))));
|
|
}
|
|
/** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */
|
|
static OPUS_INLINE opus_val32 celt_exp2(opus_val16 x)
|
|
{
|
|
int integer;
|
|
opus_val16 frac;
|
|
integer = SHR16(x,10);
|
|
if (integer>14)
|
|
return 0x7f000000;
|
|
else if (integer < -15)
|
|
return 0;
|
|
frac = celt_exp2_frac(x-SHL16(integer,10));
|
|
return VSHR32(EXTEND32(frac), -integer-2);
|
|
}
|
|
|
|
opus_val32 celt_rcp(opus_val32 x);
|
|
|
|
#define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b))
|
|
|
|
opus_val32 frac_div32(opus_val32 a, opus_val32 b);
|
|
|
|
#define M1 32767
|
|
#define M2 -21
|
|
#define M3 -11943
|
|
#define M4 4936
|
|
|
|
/* Atan approximation using a 4th order polynomial. Input is in Q15 format
|
|
and normalized by pi/4. Output is in Q15 format */
|
|
static OPUS_INLINE opus_val16 celt_atan01(opus_val16 x)
|
|
{
|
|
return MULT16_16_P15(x, ADD32(M1, MULT16_16_P15(x, ADD32(M2, MULT16_16_P15(x, ADD32(M3, MULT16_16_P15(M4, x)))))));
|
|
}
|
|
|
|
#undef M1
|
|
#undef M2
|
|
#undef M3
|
|
#undef M4
|
|
|
|
/* atan2() approximation valid for positive input values */
|
|
static OPUS_INLINE opus_val16 celt_atan2p(opus_val16 y, opus_val16 x)
|
|
{
|
|
if (y < x)
|
|
{
|
|
opus_val32 arg;
|
|
arg = celt_div(SHL32(EXTEND32(y),15),x);
|
|
if (arg >= 32767)
|
|
arg = 32767;
|
|
return SHR16(celt_atan01(EXTRACT16(arg)),1);
|
|
} else {
|
|
opus_val32 arg;
|
|
arg = celt_div(SHL32(EXTEND32(x),15),y);
|
|
if (arg >= 32767)
|
|
arg = 32767;
|
|
return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1);
|
|
}
|
|
}
|
|
|
|
#endif /* FIXED_POINT */
|
|
#endif /* MATHOPS_H */
|