forked from parallella/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_inv.c
44 lines (39 loc) · 913 Bytes
/
p_inv.c
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
#include <pal.h>
/**
*
* Element wise inversion (reciprocal) of elements in 'a'.
*
* @param a Pointer to input vector
*
* @param c Pointer to output vector
*
* @param n Size of 'a' and 'c' vector.
*
* @return None
*
*/
#if (P_FLOAT_TYPE == P_FLOAT_SINGLE)
# define INV_APPROX 0x7EEEEBB3
#else
/* https://www.pvk.ca/Blog/LowLevel/software-reciprocal.html */
# define INV_APPROX 0x7FDE623822FC16E6ULL
#endif
void PSYM(p_inv)(const PTYPE *a, PTYPE *c, int n)
{
int i;
PTYPE cur;
for (i = 0; i < n; i++) {
cur = *(a + i);
union {
PTYPE f;
PUTYPE x;
} u = {cur};
/* First approximation */
u.x = INV_APPROX - u.x;
/* Refine */
u.f = u.f * (PCONST(2.0) - u.f * cur);
u.f = u.f * (PCONST(2.0) - u.f * cur);
u.f = u.f * (PCONST(2.0) - u.f * cur);
*(c + i) = u.f;
}
}