1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/* Copyright (C) 2010-2020 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (vector_2.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __LIBRETRO_SDK_GFX_MATH_VECTOR_2_H__
#define __LIBRETRO_SDK_GFX_MATH_VECTOR_2_H__
#include <stdint.h>
#include <math.h>
#include <retro_common_api.h>
#include <retro_inline.h>
RETRO_BEGIN_DECLS
typedef float vec2_t[2];
#define vec2_dot(a, b) ((a[0] * b[0]) + (a[1] * b[1]))
#define vec2_cross(a, b) ((a[0]*b[1]) - (a[1]*b[0]))
#define vec2_add(dst, src) \
dst[0] += src[0]; \
dst[1] += src[1]
#define vec2_subtract(dst, src) \
dst[0] -= src[0]; \
dst[1] -= src[1]
#define vec2_copy(dst, src) \
dst[0] = src[0]; \
dst[1] = src[1]
static INLINE float overflow(void)
{
unsigned i;
volatile float f = 1e10;
for (i = 0; i < 10; ++i)
f *= f;
return f;
}
static INLINE int16_t tofloat16(float f)
{
union uif32
{
float f;
uint32_t i;
};
int i, s, e, m;
union uif32 Entry;
Entry.f = f;
i = (int)Entry.i;
s = (i >> 16) & 0x00008000;
e = ((i >> 23) & 0x000000ff) - (127 - 15);
m = i & 0x007fffff;
if (e <= 0)
{
if (e < -10)
return (int16_t)(s);
m = (m | 0x00800000) >> (1 - e);
if (m & 0x00001000)
m += 0x00002000;
return (int16_t)(s | (m >> 13));
}
if (e == 0xff - (127 - 15))
{
if (m == 0)
return (int16_t)(s | 0x7c00);
m >>= 13;
return (int16_t)(s | 0x7c00 | m | (m == 0));
}
if (m & 0x00001000)
{
m += 0x00002000;
if (m & 0x00800000)
{
m = 0;
e += 1;
}
}
if (e > 30)
{
overflow();
return (int16_t)(s | 0x7c00);
}
return (int16_t)(s | (e << 10) | (m >> 13));
}
static INLINE unsigned int vec2_packHalf2x16(float vec0, float vec1)
{
union
{
int16_t in[2];
unsigned int out;
} u;
u.in[0] = tofloat16(vec0);
u.in[1] = tofloat16(vec1);
return u.out;
}
RETRO_END_DECLS
#endif
|