MCPcopy Create free account
hub / github.com/ElementsProject/lightning / u64_to_fp16

Function u64_to_fp16

common/fp16.c:6–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <common/fp16.h>
5
6fp16_t u64_to_fp16(u64 val, bool round_up)
7{
8 u16 mantissa_bits, mantissa, exponent;
9
10 if (val == 0)
11 return 0;
12
13 /* How many bits do we need to represent mantissa? */
14 mantissa_bits = bitops_hs64(val) + 1;
15
16 /* We only have 11 bits, so if we need more, we will round. */
17 if (mantissa_bits > 11) {
18 exponent = mantissa_bits - 11;
19 mantissa = (val >> exponent);
20 /* If we're losing bits here, we're rounding down */
21 if (round_up && (val & ((1ULL << exponent)-1))) {
22 mantissa++;
23 if (mantissa == (1 << 11)) {
24 mantissa >>= 1;
25 exponent++;
26 }
27 }
28 /* huge number? Make it max. */
29 if (exponent >= 32) {
30 exponent = 31;
31 mantissa = (1 << 11)-1;
32 }
33 } else {
34 exponent = 0;
35 mantissa = val;
36 }
37
38 assert((mantissa >> 11) == 0);
39 return (exponent << 11) | mantissa;
40}
41
42bool amount_msat_less_fp16(struct amount_msat amt, fp16_t fp)
43{

Callers 6

fill_from_updateFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85
get_capacitiesFunction · 0.85
mainFunction · 0.85

Calls 1

bitops_hs64Function · 0.85

Tested by 2

mainFunction · 0.68
mainFunction · 0.68