1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/in/arith.c
julianb0 a42786831f
vm
2019-07-04 20:33:49 +02:00

191 lines
2.9 KiB
C

// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <in/instrs.h>
//----------------------------------------------------------------------------//
IMPL_START_2(sgn)
{
v1 = (v2 == 0 ? 0 :
((long)v2 < 0 ? (ulong)-1L : 1));
}
IMPL_OUT;
IMPL_START_2(neg)
{
v1 = ~v2 + 1;
}
IMPL_OUT;
//----------------------------------------------------------------------------//
IMPL_START_1(inc)
{
v1++;
}
IMPL_OUT;
IMPL_START_1(dec)
{
v1--;
}
IMPL_OUT;
//----------------------------------------------------------------------------//
IMPL_START_2(add)
{
ALU_GET_SRCS();
v1 = src1 + src2;
}
IMPL_OUT;
IMPL_START_2(xadd)
{
ulong tmp = v1;
v1 += v2;
v2 = tmp;
}
IMPL_OUT_2;
IMPL_START_2(addf)
{
ALU_GET_SRCS();
COMPARE(src1, ~src2+1);
v1 = src1 + src2;
}
IMPL_OUT_ZSF;
//----------------------------------------------------------------------------//
IMPL_START_2(sub)
{
ALU_GET_SRCS();
v1 = src1 - src2;
}
IMPL_OUT;
IMPL_START_2(subf)
{
ALU_GET_SRCS();
COMPARE(src1, src2);
v1 = src1 - src2;
}
IMPL_OUT;
//
// i_subf but discards result
//
IMPL_START_2(cmp)
{
COMPARE(v1, v2);
}
IMPL_END;
//----------------------------------------------------------------------------//
IMPL_START_2(adc)
{
ALU_GET_SRCS();
v1 = src1 + src2 + !!(flg&CF);
}
IMPL_OUT;
IMPL_START_2(ado)
{
ALU_GET_SRCS();
v1 = src1 + src2 + !!(flg&OF);
}
IMPL_OUT;
IMPL_START_2(sbb)
{
ALU_GET_SRCS();
v1 = src1 - src2 - !!(flg&CF);
}
IMPL_OUT;
IMPL_START_2(sbo)
{
ALU_GET_SRCS();
v1 = src1 - src2 - !!(flg&OF);
}
IMPL_OUT;
//----------------------------------------------------------------------------//
//
// www.codeproject.com/Tips/618570/UInt-Multiplication-Squaring
//
static void multiply(ulong u, ulong v, ulong *hi, ulong *lo)
{
ulong u1 = (u & 0xffffffff);
ulong v1 = (v & 0xffffffff);
ulong t = (u1 * v1);
ulong w3 = (t & 0xffffffff);
ulong k = (t >> 32);
u >>= 32;
t = (u * v1) + k;
k = (t & 0xffffffff);
ulong w1 = (t >> 32);
v >>= 32;
t = (u1 * v) + k;
k = (t >> 32);
if (hi) *hi = (u * v) + w1 + k;
if (lo) *lo = (t << 32) + w3;
}
IMPL_START_2(mul)
{
ALU_GET_SRCS();
multiply(src1, src2, 0, &v1);
}
IMPL_OUT;
IMPL_START_2(mulhi)
{
ALU_GET_SRCS();
multiply(src1, src2, &v1, &v2);
}
IMPL_OUT_2;
IMPL_START_2(mulf)
{
ALU_GET_SRCS();
multiply(src1, src2, &v2, &v1);
if (v2 > 0) {
flg |= CF;
flg |= OF;
}
else {
flg &= ~CF;
flg &= ~OF;
}
}
IMPL_OUT;
//----------------------------------------------------------------------------//
IMPL_START_2(div)
{
ALU_GET_SRCS();
v1 = src1 / src2;
}
IMPL_OUT;
IMPL_START_2(rem)
{
ALU_GET_SRCS();
v1 = src1 % src2;
}
IMPL_OUT;
//----------------------------------------------------------------------------//