+DZ-Jay Posted October 18, 2021 Share Posted October 18, 2021 Is there a way to perform signed divisions of powers of two in IntyBASIC? Actually, what I'm looking for is to induce a "SAR" (Shift Arithmetic Right) instruction. I know the manual says that Division and Modulo operators treat numbers as unsigned but then, how do you scale a value down while preserving its sign? -dZ. Quote Link to comment Share on other sites More sharing options...
+nanochess Posted October 18, 2021 Share Posted October 18, 2021 Currently there is no way to do a signed division or remainder. The only way is to check if the operator is negative, and negate the value before and after applying the operation. Quote Link to comment Share on other sites More sharing options...
+DZ-Jay Posted October 19, 2021 Author Share Posted October 19, 2021 2 hours ago, nanochess said: Currently there is no way to do a signed division or remainder. The only way is to check if the operator is negative, and negate the value before and after applying the operation. Thanks, I thought as much. Unfortunately, that's a bit expensive. I hope this can be addressed in a future update to the compiler. Although it's a bit of an edge case, signed division is still a common operation, I think. dZ. Quote Link to comment Share on other sites More sharing options...
+DZ-Jay Posted October 19, 2021 Author Share Posted October 19, 2021 14 hours ago, nanochess said: Currently there is no way to do a signed division or remainder. The only way is to check if the operator is negative, and negate the value before and after applying the operation. I came up with this macro to extend the sign. It works only for powers of two divisors. '' ======================================================================== '' '' SignedDivision(val, div) '' '' Divides a signed number by a power of two, retaining its sign. '' '' '' '' Arguments: '' '' val: The value to divide. '' '' div: The divisor. Must be a power of two constant. '' '' '' '' Output: '' '' The signed value "val" divided by power of two "div". '' '' ======================================================================== '' DEF FN SignedDivision(val, div) = (((((val) / (div)) Xor ($8000 / (div))) - ($8000 / (div))) AND $FFFF) Making the divisor a constant results in most of the terms being evaluated at compile-time. If the divisor is not a constant then all bets are off, and it gets rather expensive. -dZ. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.