convert.c

filter: Statementstmt 100% 40 / 40 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 56 / 56 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Unitvec16.xs ↑ Summary
1 /*
2 * A Unit Vector to 16-bit word conversion algorithm
3 * based on the work of Rafael Baptista (rafael@oroboro.com)
4 * - Accuracy improved by Oleg D. (punkfloyd@rocketmail.com)
5 * - Adapted to perl XS by Cole Minor (coleminor@hush.ai)
6 *
7 * Baptista, Raphael (2000-12-08). "Higher Accuracy Quantized Normals". GameDev.Net LLC.
8 * http://www.gamedev.net/page/resources/_/technical/math-and-physics/higher-accuracy-quantized-normals-r1252]
9 * Retrieved 2012-01-06.
10 *
11 */
12 #include "constants.h"
13 #include "convert.h"
14  
15 #include "iltab.gen.c"
16  
17 829 unitvec16_t unitvec16_pack(float *n) {
18 float x, y, z, w;
19 unitvec16_t p;
20 unsigned xb, yb;
21  
22 829 p = 0;
23 829 x = n[0];
24 829 y = n[1];
25 829 z = n[2];
26 829 ▶ if (x < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (x < 0) { 324 505 829
27 324 p |= XSIGN_MASK;
28 324 x = -x;
29 }
30 829 ▶ if (y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (y < 0) { 324 505 829
31 324 p |= YSIGN_MASK;
32 324 y = -y;
33 }
34 829 ▶ if (z < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (z < 0) { 324 505 829
35 324 p |= ZSIGN_MASK;
36 324 z = -z;
37 }
38  
39 829 w = 126.0f / (x + y + z);
40 829 xb = x * w;
41 829 yb = y * w;
42  
43 829 ▶ if (xb >= 64) {
Branch: 1 branch
BranchTrueFalseTotal
if (xb >= 64) { 107 722 829
44 107 xb = 127 - xb;
45 107 yb = 127 - yb;
46 }
47  
48 829 p |= xb << 7;
49 829 p |= yb;
50 829 return p;
51 }
52  
53 829 void unitvec16_unpack(unitvec16_t p, float *n) {
54 unsigned xb, yb;
55 float w;
56  
57 829 xb = (p & TOP_MASK) >> 7;
58 829 yb = p & BOTTOM_MASK;
59  
60 829 ▶ if (xb + yb >= 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (xb + yb >= 127) { 106 723 829
61 106 xb = 127 - xb;
62 106 yb = 127 - yb;
63 }
64  
65 829 w = iltab[p & ~SIGN_MASK];
66 829 n[0] = w * (float) xb;
67 829 n[1] = w * (float) yb;
68 829 n[2] = w * (float) (126 - xb - yb);
69  
70 829 ▶ if (p & XSIGN_MASK)
Branch: 1 branch
BranchTrueFalseTotal
if (p & XSIGN_MASK) 324 505 829
71 324 n[0] = -n[0];
72 829 ▶ if (p & YSIGN_MASK)
Branch: 1 branch
BranchTrueFalseTotal
if (p & YSIGN_MASK) 324 505 829
73 324 n[1] = -n[1];
74 829 ▶ if (p & ZSIGN_MASK)
Branch: 1 branch
BranchTrueFalseTotal
if (p & ZSIGN_MASK) 325 504 829
75 325 n[2] = -n[2];
76 829 }
« Unitvec16.xs ↑ Summary