2023-08-03 22:22:29 -04:00
|
|
|
package funkin.util.tools;
|
|
|
|
|
2024-01-10 00:20:00 -05:00
|
|
|
import haxe.Int64;
|
|
|
|
|
2023-08-03 22:22:29 -04:00
|
|
|
/**
|
2024-01-10 00:20:00 -05:00
|
|
|
* Why `haxe.Int64` doesn't have a built-in `toFloat` function is beyond me.
|
2023-08-03 22:22:29 -04:00
|
|
|
*/
|
|
|
|
class Int64Tools
|
|
|
|
{
|
2024-01-10 00:20:00 -05:00
|
|
|
private inline static var MAX_32_PRECISION:Float = 4294967296.0;
|
|
|
|
|
|
|
|
public static function fromFloat(f:Float):Int64
|
|
|
|
{
|
|
|
|
var h = Std.int(f / MAX_32_PRECISION);
|
|
|
|
var l = Std.int(f);
|
|
|
|
return Int64.make(h, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function toFloat(i:Int64):Float
|
|
|
|
{
|
2024-02-05 19:46:11 -05:00
|
|
|
var f:Float = i.low;
|
2024-01-10 00:20:00 -05:00
|
|
|
if (f < 0) f += MAX_32_PRECISION;
|
2024-02-05 19:46:11 -05:00
|
|
|
return (i.high * MAX_32_PRECISION + f);
|
2024-01-10 00:20:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function isToIntSafe(i:Int64):Bool
|
|
|
|
{
|
|
|
|
return i.high != i.low >> 31;
|
|
|
|
}
|
2023-08-03 22:22:29 -04:00
|
|
|
|
2024-01-10 00:20:00 -05:00
|
|
|
public static function toIntSafe(i:Int64):Int
|
2023-08-03 22:22:29 -04:00
|
|
|
{
|
2024-01-10 00:20:00 -05:00
|
|
|
try
|
2023-08-03 22:22:29 -04:00
|
|
|
{
|
2024-01-10 00:20:00 -05:00
|
|
|
return Int64.toInt(i);
|
2023-08-03 22:22:29 -04:00
|
|
|
}
|
2024-01-10 00:20:00 -05:00
|
|
|
catch (e:Dynamic)
|
2023-08-03 22:22:29 -04:00
|
|
|
{
|
2024-01-10 00:20:00 -05:00
|
|
|
throw 'Could not represent value "${Int64.toStr(i)}" as an integer.';
|
2023-08-03 22:22:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|