MCPcopy
hub / github.com/compiler-explorer/compiler-explorer / getNumericToolTip

Function getNumericToolTip

static/utils.ts:108–160  ·  view source on GitHub ↗
(value: string, digitSeparator?: string)

Source from the content-addressed store, hash-verified

106}
107
108export function getNumericToolTip(value: string, digitSeparator?: string): string | null {
109 const formatNumber = (num: bigInt.BigInteger, base: number, chunkSize: number) => {
110 const numberString = num.toString(base).toUpperCase();
111 if (digitSeparator !== undefined) {
112 return addDigitSeparator(numberString, digitSeparator, chunkSize);
113 }
114 return numberString;
115 };
116 const numericValue = parseNumericValue(value);
117 if (numericValue === null) return null;
118
119 // PTX floats
120 const view = new DataView(new ArrayBuffer(8));
121 view.setBigUint64(0, BigInt(numericValue.toString()), true);
122 if (ptxFloat32.test(value)) return view.getFloat32(0, true).toPrecision(9) + 'f';
123 if (ptxFloat64.test(value)) return view.getFloat64(0, true).toPrecision(17);
124
125 // Decimal representation.
126 let result = formatNumber(numericValue, 10, 3);
127
128 // Hexadecimal representation.
129 if (numericValue.isNegative()) {
130 const masked = bigInt('ffffffffffffffff', 16).and(numericValue);
131 result += ' = 0x' + formatNumber(masked, 16, 4);
132 } else {
133 result += ' = 0x' + formatNumber(numericValue, 16, 4);
134 }
135
136 // Float32/64 representation.
137 view.setBigUint64(0, BigInt(numericValue.toString()), true);
138 if (numericValue.bitLength().lesserOrEquals(32)) result += ' = ' + view.getFloat32(0, true).toPrecision(9) + 'f';
139 // only subnormal doubles and zero may have upper 32 bits all 0, assume unlikely to be double
140 else result += ' = ' + view.getFloat64(0, true).toPrecision(17);
141
142 // Printable UTF-8 characters.
143 const bytes = numericValue.isNegative()
144 ? // bytes of negative number without sign extension
145 numericValue
146 .add(1)
147 .toArray(256)
148 .value.map(byte => byte ^ 0xff)
149 : numericValue.toArray(256).value;
150 // This assumes that `numericValue` is encoded as little-endian.
151 bytes.reverse();
152 const decoder = new TextDecoder('utf-8', {fatal: true});
153 try {
154 result += ' = ' + JSON.stringify(decoder.decode(Uint8Array.from(bytes)));
155 } catch {
156 // ignore `TypeError` when the number is not valid UTF-8
157 }
158
159 return result;
160}
161
162// zip two arrays up until min(a.length, b.length)
163export function* zip<T>(a: T[], b: T[]) {

Callers

nothing calls this directly

Calls 5

parseNumericValueFunction · 0.85
formatNumberFunction · 0.85
toStringMethod · 0.80
testMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected