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

191 lines
2.9 KiB
C
Raw Normal View History

2019-05-30 12:44:56 +02:00
// The OS/K Team licenses this file to you under the MIT license.
2019-05-29 16:57:22 +02:00
// See the LICENSE file in the project root for more information.
2019-06-05 12:53:09 +02:00
#include <in/instrs.h>
2019-05-29 16:57:22 +02:00
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-06-06 22:07:34 +02:00
2019-06-07 13:38:34 +02:00
IMPL_START_2(sgn)
{
2019-06-13 22:20:35 +02:00
v1 = (v2 == 0 ? 0 :
((long)v2 < 0 ? (ulong)-1L : 1));
2019-06-07 13:38:34 +02:00
}
2019-06-12 15:30:35 +02:00
IMPL_OUT;
2019-07-04 18:41:05 +02:00
IMPL_START_2(neg)
2019-06-12 15:30:35 +02:00
{
2019-07-04 18:41:05 +02:00
v1 = ~v2 + 1;
2019-06-12 15:30:35 +02:00
}
IMPL_OUT;
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-06-07 13:38:34 +02:00
2019-06-06 22:07:34 +02:00
IMPL_START_1(inc)
2019-06-12 15:30:35 +02:00
{
v1++;
}
IMPL_OUT;
2019-06-06 22:07:34 +02:00
IMPL_START_1(dec)
2019-06-12 15:30:35 +02:00
{
v1--;
}
IMPL_OUT;
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-05-29 16:57:22 +02:00
IMPL_START_2(add)
2019-06-12 15:30:35 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
v1 = src1 + src2;
2019-06-12 15:30:35 +02:00
}
IMPL_OUT;
2019-07-04 18:41:05 +02:00
IMPL_START_2(xadd)
{
ulong tmp = v1;
v1 += v2;
v2 = tmp;
}
IMPL_OUT_2;
2019-06-12 15:30:35 +02:00
IMPL_START_2(addf)
2019-05-29 16:57:22 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
COMPARE(src1, ~src2+1);
v1 = src1 + src2;
2019-06-16 13:06:41 +02:00
}
IMPL_OUT_ZSF;
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-06-16 13:06:41 +02:00
2019-05-29 16:57:22 +02:00
IMPL_START_2(sub)
2019-06-12 15:30:35 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
v1 = src1 - src2;
2019-06-12 15:30:35 +02:00
}
IMPL_OUT;
IMPL_START_2(subf)
2019-05-29 16:57:22 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
COMPARE(src1, src2);
v1 = src1 - src2;
2019-05-29 16:57:22 +02:00
}
2019-06-13 15:47:20 +02:00
IMPL_OUT;
2019-06-06 22:07:34 +02:00
//
2019-06-12 15:30:35 +02:00
// i_subf but discards result
2019-06-06 22:07:34 +02:00
//
IMPL_START_2(cmp)
{
2019-06-13 15:47:20 +02:00
COMPARE(v1, v2);
2019-06-06 22:07:34 +02:00
}
IMPL_END;
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-05-29 16:57:22 +02:00
2019-07-04 20:33:49 +02:00
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;
//----------------------------------------------------------------------------//
2019-06-07 13:38:34 +02:00
//
// 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);
2019-07-04 20:33:49 +02:00
if (hi) *hi = (u * v) + w1 + k;
if (lo) *lo = (t << 32) + w3;
2019-06-07 13:38:34 +02:00
}
2019-06-05 22:11:45 +02:00
IMPL_START_2(mul)
2019-06-12 15:30:35 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
multiply(src1, src2, 0, &v1);
2019-06-12 15:30:35 +02:00
}
IMPL_OUT;
2019-07-04 20:33:49 +02:00
IMPL_START_2(mulhi)
2019-06-12 15:30:35 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
multiply(src1, src2, &v1, &v2);
2019-06-12 15:30:35 +02:00
}
2019-07-04 18:41:05 +02:00
IMPL_OUT_2;
2019-06-12 15:30:35 +02:00
2019-07-04 20:33:49 +02:00
IMPL_START_2(mulf)
2019-05-29 16:57:22 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
multiply(src1, src2, &v2, &v1);
2019-05-29 16:57:22 +02:00
2019-07-04 20:33:49 +02:00
if (v2 > 0) {
2019-06-07 13:38:34 +02:00
flg |= CF;
flg |= OF;
}
2019-05-30 12:44:56 +02:00
2019-06-07 13:38:34 +02:00
else {
flg &= ~CF;
flg &= ~OF;
}
2019-05-29 16:57:22 +02:00
}
2019-07-04 20:33:49 +02:00
IMPL_OUT;
2019-05-29 16:57:22 +02:00
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-06-06 22:07:34 +02:00
2019-06-05 22:11:45 +02:00
IMPL_START_2(div)
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
v1 = src1 / src2;
2019-06-05 22:11:45 +02:00
}
IMPL_OUT;
2019-06-21 19:38:31 +02:00
IMPL_START_2(rem)
2019-06-05 22:11:45 +02:00
{
2019-07-04 20:33:49 +02:00
ALU_GET_SRCS();
v1 = src1 % src2;
2019-06-05 22:11:45 +02:00
}
IMPL_OUT;
2019-07-04 18:41:05 +02:00
//----------------------------------------------------------------------------//
2019-05-29 16:57:22 +02:00