Float16.xs

filter: Statementstmt 56.4% 22 / 39 Branchbran 40.0% 8 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 50.8% 30 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Games/EternalLands/Binary/Float16.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include <stdint.h>
8  
9 /* This method is faster than the OpenEXR implementation (very often
10 * used, eg. in Ogre), with the additional benefit of rounding, inspired
11 * by James Tursa's half-precision code. */
12 6 static inline uint16_t _float_to_half(uint32_t x) {
13 6 uint16_t bits = (x >> 16) & 0x8000;
14 6 uint16_t m = (x >> 12) & 0x07ff;
15 6 unsigned int e = (x >> 23) & 0xff;
16 6 ▶ if (e < 103)
Branch: 1 branch
BranchTrueFalseTotal
if (e < 103) 0 6 6
17 0 return bits;
18 6 ▶ if (e > 142) {
Branch: 1 branch
BranchTrueFalseTotal
if (e > 142) { 0 6 6
19 0 bits |= 0x7c00u;
20 0 ▶ bits |= e == 255 && (x & 0x007fffffu);
Branch: 2 branches
BranchTrueFalseTotal
bits |= e == 255 && (x & 0x007fffffu); 0 0 0
bits |= e == 255 && (x & 0x007fffffu); 0 0 0
21 0 return bits;
22 }
23 6 ▶ if (e < 113) {
Branch: 1 branch
BranchTrueFalseTotal
if (e < 113) { 0 6 6
24 0 m |= 0x0800u;
25 0 bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
26 0 return bits;
27 }
28 6 bits |= ((e - 112) << 10) | (m >> 1);
29 6 bits += m & 1;
30 6 return bits;
31 }
32  
33 static int const shifttable[32] = {
34 23, 14, 22, 0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 20, 0,
35 15, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 17, 0, 18, 19, 0,
36 };
37 static uint32_t const shiftmagic = 0x07c4acddu;
38  
39 /* This algorithm is similar to the OpenEXR implementation, except it
40 * uses branchless code in the denormal path. This is slower than a
41 * table version, but will be more friendly to the cache for occasional
42 * uses. */
43 6 static inline uint32_t _half_to_float(uint16_t x) {
44 6 uint32_t s = (x & 0x8000u) << 16;
45 6 ▶ if ((x & 0x7fffu) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((x & 0x7fffu) == 0) 0 6 6
46 0 return (uint32_t)x << 16;
47 6 uint32_t e = x & 0x7c00u;
48 6 uint32_t m = x & 0x03ffu;
49 6 ▶ if (e == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (e == 0) { 0 6 6
50 0 uint32_t v = m | (m >> 1);
51 0 v |= v >> 2;
52 0 v |= v >> 4;
53 0 v |= v >> 8;
54 0 e = shifttable[(v * shiftmagic) >> 27];
55 0 return s | (((125 - e) << 23) + (m << e));
56 }
57 6 ▶ if (e == 0x7c00u) {
Branch: 1 branch
BranchTrueFalseTotal
if (e == 0x7c00u) { 0 6 6
58 0 ▶ if (m == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (m == 0) 0 0 0
59 0 return s | 0x7f800000u;
60 0 return s | 0x7fc00000u;
61 }
62 6 return s | (((e >> 10) + 112) << 23) | (m << 13);
63 }
64  
65 union fbits {
66 float f;
67 uint32_t x;
68 };
69  
70 MODULE = Games::EternalLands::Binary::Float16 PACKAGE = Games::EternalLands::Binary::Float16
71  
72 unsigned short
73 pack_float16(v)
74 float v
75 CODE:
76 union fbits u;
77 6 u.f = v;
78 6 RETVAL = _float_to_half(u.x);
79 OUTPUT:
80 RETVAL
81  
82 float
83 unpack_float16(v)
84 unsigned short v
85 CODE:
86 union fbits u;
87 6 u.x = _half_to_float(v);
88 6 ▶ RETVAL = u.f;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = u.f; 5 1 6
89 OUTPUT:
90 RETVAL
↑ Summary blib/lib/Games/EternalLands/Binary/Float16.pm »