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/alu.c
2019-08-21 16:57:32 +02:00

213 lines
5.3 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>
#define _NEED_ARCH_I
#include <ob/arch_i.h>
//----------------------------------------------------------------------------//
IMPL_START(not, 1) { SRCP(p1); *r1 = ~p1->val; return 1; }
IMPL_START(not, 2) { SRCP(p2); *r1 = ~p2->val; return 1; }
IMPL_START(or, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val | p2->val; return 1; }
IMPL_START(or, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val | p3->val; return 1; }
IMPL_START(and, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val & p2->val; return 1; }
IMPL_START(and, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val & p3->val; return 1; }
IMPL_START(xor, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val ^ p2->val; return 1; }
IMPL_START(xor, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val ^ p3->val; return 1; }
//----------------------------------------------------------------------------//
IMPL_START(shl, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val << p2->val; return 1; }
IMPL_START(shl, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val << p3->val; return 1; }
IMPL_START(shr, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val >> p2->val; return 1; }
IMPL_START(shr, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val >> p3->val; return 1; }
IMPL_START(sar, 2) {
SRCP(p1); SRCP(p2);
*r1 = (ulong)((long)p1->val >> (long)p2->val);
return 1;
}
IMPL_START(sar, 3) {
SRCP(p2); SRCP(p3);
*r1 = (ulong)((long)p2->val >> (long)p3->val);
return 1;
}
//----------------------------------------------------------------------------//
IMPL_START(neg, 1) { SRCP(p1); *r1 = ~p1->val + 1; return 1; }
IMPL_START(neg, 2) { SRCP(p2); *r1 = ~p2->val + 1; return 1; }
IMPL_START(negv, 1) {
SRCP(p1);
if (p1->val == (1ul<<63))
_except(E_OVF, "Ineffective NEG operation (NEGV)");
*r1 = ~p1->val + 1;
return 1;
}
IMPL_START(negv, 2) {
SRCP(p2);
if (p2->val == (1ul<<63))
_except(E_OVF, "Ineffective NEG operation (NEGV)");
*r1 = ~p2->val + 1;
return 1;
}
//----------------------------------------------------------------------------//
IMPL_START(inc, 1) { SRCP(p1); *r1 = p1->val + 1; return 1; }
IMPL_START(add, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val + p2->val; return 1; }
IMPL_START(add, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val + p3->val; return 1; }
IMPL_START(addv, 2) {
SRCP(p1); SRCP(p2);
ulong rs = p1->val + p2->val;
if (((p1->val ^ rs) & (p2->val ^ rs)) >> 63)
_except(E_OVF, "Signed addition overflow (ADDV#2)");
*r1 = rs;
return 1;
}
IMPL_START(addv, 3) {
SRCP(p2); SRCP(p3);
ulong rs = p2->val + p3->val;
if (((p2->val ^ rs) & (p3->val ^ rs)) >> 63)
_except(E_OVF, "Signed addition overflow (ADDV#3)");
*r1 = rs;
return 1;
}
//----------------------------------------------------------------------------//
IMPL_START(dec, 1) { SRCP(p1); *r1 = p1->val - 1; return 1; }
IMPL_START(sub, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val - p2->val; return 1; }
IMPL_START(sub, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val - p3->val; return 1; }
IMPL_START(subv, 2) {
SRCP(p1); SRCP(p2);
ulong rs = p1->val - p2->val;
if (((p1->val ^ rs) & (p1->val ^ p2->val)) >> 63)
_except(E_OVF, "Signed substraction underflow (SUBV#2)");
*r1 = rs;
return 1;
}
IMPL_START(subv, 3) {
SRCP(p1); SRCP(p2);
ulong rs = p2->val - p3->val;
if (((p2->val ^ rs) & (p2->val ^ p3->val)) >> 63)
_except(E_OVF, "Signed substraction underflow (SUBV#3)");
*r1 = rs;
return 1;
}
//----------------------------------------------------------------------------//
IMPL_START(mul, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val * p2->val; return 1; }
IMPL_START(mul, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val * p3->val; return 1; }
IMPL_START(rem, 2) { SRCP(p1); SRCP(p2); *r1 = p1->val % p2->val; return 1; }
IMPL_START(rem, 3) { SRCP(p2); SRCP(p3); *r1 = p2->val % p3->val; return 1; }
IMPL_START(div, 2) {
SRCP(p1); SRCP(p2);
if (!p2->val) _except(E_DIV, "DIV by 0");
*r1 = p1->val / p2->val;
return 1;
}
IMPL_START(div, 3) {
SRCP(p2); SRCP(p3);
if (!p3->val) _except(E_DIV, "DIV by 0");
*r1 = p2->val / p3->val;
return 1;
}
IMPL_START(idiv, 2) {
SRCP(p1); SRCP(p2);
if (!p3->val) _except(E_DIV, "IDIV by 0");
*r1 = (ulong)((long)p1->val/(long)p2->val);
return 1;
}
IMPL_START(idiv, 3) {
SRCP(p2); SRCP(p3);
if (!p3->val) _except(E_DIV, "IDIV by 0");
*r1 = (ulong)((long)p2->val/(long)p3->val);
return 1;
}
//----------------------------------------------------------------------------//
static void __unsigned_multiply128(ulong u, ulong v, ulong *hi, ulong *lo)
{
__uint128_t r = (__uint128_t)u * (__uint128_t)v;
*hi = r >> 64;
*lo = r;
}
IMPL_START(mulhi, 3)
{
SRCP(p2);
SRCP(p3);
__unsigned_multiply128(p2->val, p3->val, r1, r2);
return 2;
}
//----------------------------------------------------------------------------//
static void __signed_multiply128(ulong u, ulong v, ulong *hi, ulong *lo)
{
__int128_t r = (__int128_t)(long)u * (__int128_t)(long)v;
*hi = r >> 64;
*lo = r;
}
IMPL_START(imulhi, 3)
{
SRCP(p2);
SRCP(p3);
__signed_multiply128(p2->val, p3->val, r1, r2);
return 2;
}
//----------------------------------------------------------------------------//