// The OS/K Team licences this file to you under the MIT license. // See the LICENSE file in the project root for more information. #include "instrs.h" #include "arch_i.h" // // Unsigned arithmetic instructions // IMPL_START_2(add) { v1 += v2; } IMPL_OUT; IMPL_START_2(sub) { v1 -= v2; } IMPL_OUT; IMPL_START_1(mul) { // Adapted from www.codeproject.com/Tips/618570/UInt-Multiplication-Squaring ulong v2 = v1; v1 = ctx->r[RAX].val; ulong u1 = (v1 & 0xffffffff); ulong u2 = (v2 & 0xffffffff); ulong t = (u1 * u2); ulong w3 = (t & 0xffffffff); ulong k = (t >> 32); v1 >>= 32; t = (v1 * u2) + k; k = (t & 0xffffffff); ulong w1 = (t >> 32); v2 >>= 32; t = (u1 * v2) + k; k = (t >> 32); ctx->r[RDX].val = (v1 * v2) + w1 + k; ctx->r[RAX].val = (t << 32) + w3; } IMPL_END; IMPL_START_1(div) { ctx->r[RDX].val = ctx->r[RAX].val % v1; ctx->r[RAX].val = ctx->r[RAX].val / v1; } IMPL_END; IMPL_START_1(inc) { v1++; } IMPL_OUT; IMPL_START_1(dec) { v1--; } IMPL_OUT;