Skip to content

Instantly share code, notes, and snippets.

@Ludophonic
Last active April 5, 2022 15:53
Show Gist options
  • Save Ludophonic/9043238 to your computer and use it in GitHub Desktop.
Save Ludophonic/9043238 to your computer and use it in GitHub Desktop.
Xcode / LLDB script for formatting half-precision floats in the debugger.
union FP16
{
uint16 u;
struct
{
uint16 Mantissa : 10;
uint16 Exponent : 5;
uint16 Sign : 1;
};
};
struct half
{
// Constructors, operators, etc... here
FP16 fp16;
};
import lldb
# FP16 summary
def summary_FP16( valobj, internal_dict ):
anon = valobj.GetChildAtIndex(1)
m = anon.GetChildAtIndex(0).GetValueAsUnsigned()
e = anon.GetChildAtIndex(1).GetValueAsUnsigned()
s = anon.GetChildAtIndex(2).GetValueAsUnsigned()
if e == 0:
summary = "" if s == 0 else "-"
summary += str( 2.0**(-14.0) * (m/1024.0) )
elif e == 31:
if m == 0:
summary = "+Inf" if s == 0 else "-Inf"
else:
summary = "NaN"
else:
summary = str( (-1.0)**s * 2.0**(e-15.0) * (1.0+m/1024.0) )
return summary
def __lldb_init_module( debugger, dict ):
debugger.HandleCommand('type summary add FP16 -w Half -F Half.summary_FP16')
debugger.HandleCommand('type summary add half -w Half --summary-string "${var.fp16}"')
#
# Copy any referenced python scripts to ~/.lldb/
# Copy this file to ~/.lldbinit and set its execute bit
#
# load up python script for half precision float
command script import ~/.lldb/Half.py
# enable summary formats for Half
type category enable Half
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment