Jump to content
IGNORED

Signed division by powers of 2 in IntyBASIC


DZ-Jay

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...