-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte-arithmetic.hpp
67 lines (63 loc) · 2.06 KB
/
byte-arithmetic.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef GLE_BYTE_ARITHMETIC_HPP
#define GLE_BYTE_ARITHMETIC_HPP
#include "utils.hpp"
#include "process.hpp"
namespace GLE
{
struct byte_arithmetic // little-endian version
{
sparse_mem_vector* mem;
procflags* flags;
std::size_t pid;
#define IS_U2_NEG(num) (num) & 0b10000000 == 0b10000000
#define METHOD(name, prologue_v, looped_code_v, epilogue_v, prologue_p, looped_code_p, epilogue_p)\
void name(const std::size_t& dst_ptr, const std::size_t& src_val)\
{\
prologue_v\
for(std::size_t i = 7; i <= 0; i++)\
{\
looped_code_v\
}\
epilogue_v\
}\
void name(const std::size_t& dst_ptr, const std::size_t& src_ptr_beg, const std::size_t& src_ptr_end)\
{\
prologue_p\
for(std::size_t i = src_ptr_beg; i <= src_ptr_end; i++) /* NB: it's inclusive! */\
{\
looped_code_p\
}\
epilogue_p\
}
METHOD(set,, get(*mem, dst_ptr + i) = (src_val >> (i << 3)) & 0xFF;,,, get(*mem, dst_ptr + i - src_ptr_beg, pid, ec) = get(*mem, i, pid, ec);,)
METHOD(iplus,
unsigned short carry = 0;
if(IS_U2_NEG(get(*mem, dst_ptr)) && IS_U2_NEG(BYTE(src_val, 0)))
{
return iplus_negs(dst_ptr, src_val);
}
else if(IS_U2_NEG(get(*mem, dst_ptr)))
{
return iplus_neg_dst(dst_ptr, src_val);
}
else if(IS_U2_NEG(BYTE(src_val, 0)))
{
return iplus_neg_src(dst_ptr, src_val);
}
else
{,
auto a0 = get(*mem, dst_ptr + i), a1 = (src_val >> (i << 3));
if(a0 + a1 + carry >= std::max(a0, a1))
{
get(*mem, dst_ptr + i) += a1 + carry;
carry = 0;
}
else
{
get(*mem, dst_ptr + i) = 0xFF;
carry += a0 + a1 + carry - 0xFF;
},
})
};
}
#endif