| 1 | // Copyright 2010-2022 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include "mathop.h" |
| 6 | #include "branch.h" |
| 7 | #include "process.h" |
| 8 | #include "stack.h" |
| 9 | #include "types.h" |
| 10 | #include "util.h" |
| 11 | #include "zterp.h" |
| 12 | |
| 13 | void zinc() |
| 14 | { |
| 15 | store_variable(zargs[0], variable(zargs[0]) + 1); |
| 16 | } |
| 17 | |
| 18 | void zdec() |
| 19 | { |
| 20 | store_variable(zargs[0], variable(zargs[0]) - 1); |
| 21 | } |
| 22 | |
| 23 | void znot() |
| 24 | { |
| 25 | store(~zargs[0]); |
| 26 | } |
| 27 | |
| 28 | void zdec_chk() |
| 29 | { |
| 30 | auto newval = variable(zargs[0]) - 1; |
| 31 | store_variable(zargs[0], newval); |
| 32 | |
| 33 | branch_if(as_signed(newval) < as_signed(zargs[1])); |
| 34 | } |
| 35 | |
| 36 | void zinc_chk() |
| 37 | { |
| 38 | auto newval = variable(zargs[0]) + 1; |
| 39 | store_variable(zargs[0], newval); |
| 40 | |
| 41 | branch_if(as_signed(newval) > as_signed(zargs[1])); |
| 42 | } |
| 43 | |
| 44 | void ztest() |
| 45 | { |
| 46 | branch_if((zargs[0] & zargs[1]) == zargs[1]); |
| 47 | } |
| 48 | |
| 49 | void zor() |
| 50 | { |
| 51 | store(zargs[0] | zargs[1]); |
| 52 | } |
| 53 | |
| 54 | void zand() |
| 55 | { |
| 56 | store(zargs[0] & zargs[1]); |
| 57 | } |
| 58 | |
| 59 | void zadd() |
| 60 | { |
| 61 | store(zargs[0] + zargs[1]); |
| 62 | } |
| 63 | |
| 64 | void zsub() |
| 65 | { |
| 66 | store(zargs[0] - zargs[1]); |
| 67 | } |
| 68 | |
| 69 | void zmul() |
| 70 | { |
| 71 | store(static_cast<uint32_t>(zargs[0]) * static_cast<uint32_t>(zargs[1])); |
| 72 | } |
| 73 | |
| 74 | void zdiv() |
| 75 | { |
| 76 | ZASSERT(zargs[1] != 0, "divide by zero"); |
| 77 | store(as_signed(zargs[0]) / as_signed(zargs[1])); |
| 78 | } |
| 79 | |
| 80 | void zmod() |
| 81 | { |
| 82 | ZASSERT(zargs[1] != 0, "divide by zero"); |
| 83 | store(as_signed(zargs[0]) % as_signed(zargs[1])); |
| 84 | } |
| 85 | |
| 86 | void zlog_shift() |
| 87 | { |
| 88 | int16_t places = as_signed(zargs[1]); |
| 89 | |
| 90 | // Shifting more than 15 bits is undefined (as of Standard 1.1), but |
| 91 | // do the most sensible thing. |
| 92 | if (places < -15 || places > 15) { |
| 93 | store(0); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (places < 0) { |
| 98 | store(zargs[0] >> -places); |
| 99 | } else { |
| 100 | store(zargs[0] << places); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void zart_shift() |
| 105 | { |
| 106 | int16_t number = as_signed(zargs[0]), places = as_signed(zargs[1]); |
| 107 | |
| 108 | // Shifting more than 15 bits is undefined (as of Standard 1.1), but |
| 109 | // do the most sensible thing. |
| 110 | if (places < -15 || places > 15) { |
| 111 | store(number < 0 ? -1 : 0); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // Shifting a negative value in C++ has some consequences: |
| 116 | // • Shifting a negative value left is undefined. |
| 117 | // • Shifting a negative value right is implementation defined. |
| 118 | // |
| 119 | // Thus these are done by hand. The Z-machine requires a right-shift |
| 120 | // of a negative value to propagate the sign bit. This is easily |
| 121 | // accomplished by complementing the value (yielding a positive |
| 122 | // number), shifting it right (zero filling), and complementing again |
| 123 | // (flip the shifted-in zeroes to ones). |
| 124 | // |
| 125 | // For a left-shift, the result should presumably be the same as a |
| 126 | // logical shift, so do that. |
| 127 | if (number < 0) { |
| 128 | if (places < 0) { |
| 129 | store(~(~number >> -places)); |
| 130 | } else { |
| 131 | store(zargs[0] << places); |
| 132 | } |
| 133 | } else { |
| 134 | if (places < 0) { |
| 135 | store(zargs[0] >> -places); |
| 136 | } else { |
| 137 | store(zargs[0] << places); |
| 138 | } |
| 139 | } |
| 140 | } |