Skip to content

Commit d9f5231

Browse files
committed
Make decoding reusable for LLDB and GDB
1 parent 0b9db3a commit d9f5231

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

extra/decimal_printer.py

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# decimal_printer.py
1+
# Copyright 2025 Matt Borland
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# https://www.boost.org/LICENSE_1_0.txt
24

5+
from detail.decode_decimal32 import decode_decimal32
36
import lldb
47

58
def decimal32_summary(valobj, internal_dict):
@@ -11,56 +14,8 @@ def decimal32_summary(valobj, internal_dict):
1114
try:
1215
val = valobj.GetNonSyntheticValue()
1316
bits = val.GetChildMemberWithName("bits_").GetValueAsUnsigned()
17+
return decode_decimal32(bits)
1418

15-
sign = bits & 2147483648 != 0
16-
isnan = False
17-
18-
if bits & 2013265920 == 2013265920:
19-
20-
if bits & 2113929216 == 2113929216:
21-
result = "-SNAN" if sign else "SNAN"
22-
isnan = True
23-
elif bits & 2080374784 == 2080374784:
24-
result = "-SNAN" if sign else "QNAN"
25-
isnan = True
26-
elif bits & 2080374784 == 2013265920:
27-
result = "-INF" if sign else "INF"
28-
else:
29-
raise ValueError("Unknown Finite Value")
30-
31-
if isnan:
32-
payload = bits & 8388607
33-
if payload > 0:
34-
result += '(' + str(payload) + ')'
35-
36-
else:
37-
# See decimal32_t::to_components()
38-
d32_comb_11_mask = 1610612736
39-
if bits & d32_comb_11_mask == d32_comb_11_mask:
40-
implied_bit = 8388608
41-
significand = implied_bit | (bits & 2097151)
42-
exp = (bits & 534773760) >> 21
43-
else:
44-
significand = bits & 8388607
45-
exp = (bits & 2139095040) >> 23
46-
47-
exp -= 101 # Bias Value
48-
49-
if significand == 0:
50-
result = "0.0e+0"
51-
else:
52-
n_digits = len(str(abs(significand)))
53-
54-
# If there is no fractional component we want to remove the decimal point and zero
55-
if n_digits > 1:
56-
normalized = significand / (10 ** (n_digits - 1))
57-
total_exp = exp + n_digits - 1
58-
else:
59-
normalized = significand
60-
total_exp = exp
61-
result = f"{'-' if sign else ''}{normalized:.{n_digits - 1}f}e{total_exp:+}"
62-
63-
return result
6419
except Exception as e:
6520
return f"<invalid decimal32_t: {e}>"
6621

extra/detail/decode_decimal32.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2025 Matt Borland
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# https://www.boost.org/LICENSE_1_0.txt
4+
5+
def decode_decimal32(bits):
6+
sign = bits & 2147483648 != 0
7+
isnan = False
8+
9+
if bits & 2013265920 == 2013265920:
10+
11+
if bits & 2113929216 == 2113929216:
12+
result = "-SNAN" if sign else "SNAN"
13+
isnan = True
14+
elif bits & 2080374784 == 2080374784:
15+
result = "-SNAN" if sign else "QNAN"
16+
isnan = True
17+
elif bits & 2080374784 == 2013265920:
18+
result = "-INF" if sign else "INF"
19+
else:
20+
raise ValueError("Unknown Finite Value")
21+
22+
if isnan:
23+
payload = bits & 8388607
24+
if payload > 0:
25+
result += '(' + str(payload) + ')'
26+
27+
else:
28+
# See decimal32_t::to_components()
29+
d32_comb_11_mask = 1610612736
30+
if bits & d32_comb_11_mask == d32_comb_11_mask:
31+
implied_bit = 8388608
32+
significand = implied_bit | (bits & 2097151)
33+
exp = (bits & 534773760) >> 21
34+
else:
35+
significand = bits & 8388607
36+
exp = (bits & 2139095040) >> 23
37+
38+
exp -= 101 # Bias Value
39+
40+
if significand == 0:
41+
result = "0.0e+0"
42+
else:
43+
n_digits = len(str(abs(significand)))
44+
45+
# If there is no fractional component we want to remove the decimal point and zero
46+
if n_digits > 1:
47+
normalized = significand / (10 ** (n_digits - 1))
48+
total_exp = exp + n_digits - 1
49+
else:
50+
normalized = significand
51+
total_exp = exp
52+
result = f"{'-' if sign else ''}{normalized:.{n_digits - 1}f}e{total_exp:+}"
53+
54+
return result

0 commit comments

Comments
 (0)