2023-08-12 16:30:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <Int64.h>
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
#ifdef INT64_NATIVE
|
|
|
|
#define Int64Sign(x) ((int) (((UInt64) (x)) >> 63))
|
|
|
|
#else
|
|
|
|
#define Int64Sign(x) ((int) ((x).i[1] >> 31))
|
|
|
|
#endif
|
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
size_t
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64Str(Int64 x, int base, char *out, size_t len)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
|
|
|
static const char symbols[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
size_t i = len - 1;
|
|
|
|
size_t j = 0;
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
int neg = Int64Sign(x);
|
2023-08-12 19:59:16 +00:00
|
|
|
|
|
|
|
Int64 base64 = Int64Create(0, base);
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (neg)
|
|
|
|
{
|
|
|
|
x = Int64Neg(x);
|
|
|
|
}
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
/* We only have symbols up to base 16 */
|
|
|
|
if (base < 2 || base > 16)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 mod = Int64Rem(x, base64);
|
|
|
|
Int32 low = Int64Low(mod);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
out[i] = symbols[low];
|
|
|
|
i--;
|
2023-08-12 19:59:16 +00:00
|
|
|
x = Int64Div(x, base64);
|
|
|
|
} while (Int64Gt(x, Int64Create(0, 0)));
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
/*
|
|
|
|
* Binary, octal, and hexadecimal are known to
|
|
|
|
* be bit representations. Everything else (notably
|
|
|
|
* decimal) should include the negative sign.
|
|
|
|
*/
|
|
|
|
if (base != 2 && base != 8 && base != 16)
|
|
|
|
{
|
|
|
|
if (neg)
|
|
|
|
{
|
|
|
|
out[i] = '-';
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
while (++i < len)
|
|
|
|
{
|
|
|
|
out[j++] = out[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
out[j] = '\0';
|
|
|
|
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
#ifndef INT64_NATIVE
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
/* No native 64-bit support, add our own */
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Create(UInt32 high, UInt32 low)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 x;
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
x.i[0] = low;
|
|
|
|
x.i[1] = high;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Add(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 z = Int64Create(0, 0);
|
2023-08-12 16:30:24 +00:00
|
|
|
int carry;
|
|
|
|
|
|
|
|
z.i[0] = x.i[0] + y.i[0];
|
|
|
|
carry = z.i[0] < x.i[0];
|
|
|
|
z.i[1] = x.i[1] + y.i[1] + carry;
|
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Sub(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64Add(x, Int64Neg(y));
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Mul(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 z = Int64Create(0, 0);
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
int xneg = Int64Sign(x);
|
|
|
|
int yneg = Int64Sign(y);
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (xneg)
|
|
|
|
{
|
|
|
|
x = Int64Neg(x);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (yneg)
|
|
|
|
{
|
|
|
|
y = Int64Neg(y);
|
|
|
|
}
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
/* while (y > 0) */
|
2023-08-12 19:59:16 +00:00
|
|
|
while (Int64Gt(y, Int64Create(0, 0)))
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
|
|
|
/* if (y & 1 != 0) */
|
2023-08-12 19:59:16 +00:00
|
|
|
if (Int64Neq(Int64And(y, Int64Create(0, 1)), Int64Create(0, 0)))
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
z = Int64Add(z, x);
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
x = Int64Sll(x, 1);
|
|
|
|
y = Int64Sra(y, 1);
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (xneg != yneg)
|
|
|
|
{
|
|
|
|
z = Int64Neg(z);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 q;
|
|
|
|
Int64 r;
|
|
|
|
} Int64Ldiv;
|
2023-08-12 16:30:24 +00:00
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
static Int64Ldiv
|
|
|
|
Int64LongDivision(Int64 n, Int64 d)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64Ldiv o;
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
int nneg = Int64Sign(n);
|
|
|
|
int dneg = Int64Sign(d);
|
2023-08-12 19:59:16 +00:00
|
|
|
|
|
|
|
o.q = Int64Create(0, 0);
|
|
|
|
o.r = Int64Create(0, 0);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
if (Int64Eq(d, Int64Create(0, 0)))
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
|
|
|
raise(SIGFPE);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (nneg)
|
|
|
|
{
|
|
|
|
n = Int64Neg(n);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (dneg)
|
|
|
|
{
|
|
|
|
d = Int64Neg(d);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
for (i = 63; i >= 0; i--)
|
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 bit = Int64And(Int64Sra(n, i), Int64Create(0, 1));
|
2023-08-12 23:02:06 +00:00
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
o.r = Int64Sll(o.r, 1);
|
|
|
|
o.r = Int64Or(o.r, bit);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
if (Int64Geq(o.r, d))
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
o.r = Int64Sub(o.r, d);
|
|
|
|
o.q = Int64Or(o.q, Int64Sll(Int64Create(0, 1), i));
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (nneg != dneg)
|
|
|
|
{
|
|
|
|
o.r = Int64Neg(o.r);
|
|
|
|
o.q = Int64Neg(o.q);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Div(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64LongDivision(x, y).q;
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Rem(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64LongDivision(x, y).r;
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Sll(Int64 x, int y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 z;
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
if (!y)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
z = Int64Create(0, 0);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
if (y < 32)
|
|
|
|
{
|
|
|
|
z.i[1] = (x.i[0] >> (32 - y)) | (x.i[1] << y);
|
|
|
|
z.i[0] = x.i[0] << y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z.i[1] = x.i[0] << (y - 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Sra(Int64 x, int y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64 z;
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
int neg = Int64Sign(x);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
if (!y)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
z = Int64Create(0, 0);
|
2023-08-12 16:30:24 +00:00
|
|
|
|
|
|
|
if (y < 32)
|
|
|
|
{
|
|
|
|
z.i[0] = (x.i[1] << (32 - y)) | (x.i[0] >> y);
|
|
|
|
z.i[1] = x.i[1] >> y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z.i[0] = x.i[1] >> (y - 32);
|
|
|
|
}
|
|
|
|
|
2023-08-12 23:02:06 +00:00
|
|
|
if (neg)
|
|
|
|
{
|
|
|
|
Int64 mask = Int64Create(0xFFFFFFFF, 0xFFFFFFFF);
|
|
|
|
|
|
|
|
z = Int64Or(Int64Sll(mask, (64 - y)), z);
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64And(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64Create(x.i[1] & y.i[1], x.i[0] & y.i[0]);
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Or(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64Create(x.i[1] | y.i[1], x.i[0] | y.i[0]);
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Xor(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64Create(x.i[1] ^ y.i[1], x.i[0] ^ y.i[0]);
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64
|
|
|
|
Int64Not(Int64 x)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 19:59:16 +00:00
|
|
|
return Int64Create(~(x.i[1]), ~(x.i[0]));
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64Eq(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
|
|
|
return x.i[0] == y.i[0] && x.i[1] == y.i[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64Lt(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 23:02:06 +00:00
|
|
|
int xneg = Int64Sign(x);
|
|
|
|
int yneg = Int64Sign(y);
|
|
|
|
|
|
|
|
if (xneg != yneg)
|
|
|
|
{
|
|
|
|
return xneg > yneg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (xneg)
|
|
|
|
{
|
|
|
|
/* Both negative */
|
|
|
|
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Both positive */
|
|
|
|
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-08-12 19:59:16 +00:00
|
|
|
Int64Gt(Int64 x, Int64 y)
|
2023-08-12 16:30:24 +00:00
|
|
|
{
|
2023-08-12 23:02:06 +00:00
|
|
|
int xneg = Int64Sign(x);
|
|
|
|
int yneg = Int64Sign(y);
|
|
|
|
|
|
|
|
if (xneg != yneg)
|
|
|
|
{
|
|
|
|
return xneg < yneg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (xneg)
|
|
|
|
{
|
|
|
|
/* Both negative */
|
|
|
|
return x.i[1] < y.i[1] || (x.i[1] == y.i[1] && x.i[0] < y.i[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Both positive */
|
|
|
|
return x.i[1] > y.i[1] || (x.i[1] == y.i[1] && x.i[0] > y.i[0]);
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 19:59:16 +00:00
|
|
|
|
2023-08-12 16:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|