Skip to content

Commit

Permalink
Let bin/hex hint display as 2's complement and full length
Browse files Browse the repository at this point in the history
Also allow bin display for Int64
  • Loading branch information
yuxiaomao committed Aug 9, 2024
1 parent bf44e13 commit 1ba8071
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion hld/Eval.hx
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@ class Eval {
}
case VInt64(i):
switch( v.hint ) {
case HHex: Value.int64Hex(i);
case HBin: Value.int64Str(i, 2);
case HHex: Value.int64Str(i, 16);
default: "" + i;
}
case VFloat(i):
Expand Down
37 changes: 20 additions & 17 deletions hld/Value.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,31 @@ enum Hint {

static final INTBASE = "0123456789ABCDEF";
public static function intStr( value : Int, base : Int ) : String {
if( base < 2 || base > INTBASE.length )
throw "Unsupported int base";
var prefix = base == 2 ? "0b" : base == 16 ? "0x" : "";
if( base == 10 || value == 0 )
return prefix + value;
var s = "";
var abs = value >= 0 ? value : -value;
while( abs > 0 ) {
s = INTBASE.charAt(abs % base) + s;
abs = Std.int(abs / base);
}
return (value < 0 ? "-" : "") + prefix + s;
return int64Str(value, base, true);
}

public static function int64Hex( value : haxe.Int64 ) : String {
public static function int64Str( value : haxe.Int64, base : Int, is32bit : Bool = false ) : String {
if( base != 2 && base != 16 )
throw "Unsupported int base " + base;
var prefix = base == 2 ? "0b" : "0x";
if( value == haxe.Int64.make(0,0) )
return prefix + "0";
var mask = base - 1;
var shift = base == 2 ? 1 : 4;
var maxlen = base == 2 ? (is32bit ? 32 : 64) : (is32bit ? 8 : 16);
var s = "";
var abs = value >= haxe.Int64.make(0,0) ? value : -value;
var positive = value >= haxe.Int64.make(0,0);
var abs = positive ? value : -(value+1); // 2's complement
while( abs > 0 ) {
s = INTBASE.charAt(abs.low & 15) + s;
abs = abs >> 4;
var cur = positive ? (abs.low & mask) : (mask - abs.low & mask);
s = INTBASE.charAt(cur) + s;
abs = abs >> shift;
}
if( s.length < maxlen ) {
var lchar = positive ? "0" : (base == 2 ? "1" : "F");
s = StringTools.lpad(s, lchar, maxlen);
}
return (value < 0 ? "-" : "") + "0x" + s;
return prefix + s;
}

public static function intEnumFlags( value : Int, eproto : format.hl.Data.EnumPrototype ) : String {
Expand Down

0 comments on commit 1ba8071

Please sign in to comment.