Mulscale: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
Fox (talk | contribs)
Wow, much complicated
No edit summary
Line 1: Line 1:
'''mulscale''' <Result> <Value 1> <Value 2> <Value 3>
'''mulscale''' <Result> <Factor 1> <Factor 2> <Right Shift>


Equals the following series of commands:
In C terms, this command performs roughly the expression <code><Result> = (<Factor 1> * <Factor 2>) >> <Right Shift></code>.


  set <Result> <Value 1>
In CON terms, it is very similar to the following series of commands:
  mul <Result> <Value 2>
 
  shiftr <Result> <Value 3>
  set <Result> <Factor 1>
  mul <Result> <Factor 2>
  shiftr <Result> <Right Shift>


The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.
The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.


One example of where this limitation takes places is with rotatesprite scaling, where 320*65536 scaled with [[statusbarscale]] is dangerously close to 31 bits.
One example of where this limitation takes places is with [[rotatesprite]] scaling, where 320*65536 scaled with [[statusbarscale]] is dangerously close to 31 bits.
 
Another example is how the trigonometric commands [[sin]] and [[cos]] use a fixed-point fractional scaling factor of 1:16384, corresponding to a 14 bit shift. So, if you wanted to multiply some ''length'' by the cosine of the current sprite's angle, you could write:
 
  cos ''cosang'' sprite[].ang
  '''mulscale''' ''result'' ''length'' ''cosang'' 14


[[Category:EDuke32 specific commands]]
[[Category:EDuke32 specific commands]]
[[Category:Gamevar manipulation]]
[[Category:Gamevar manipulation]]

Revision as of 19:23, 13 November 2019

mulscale <Result> <Factor 1> <Factor 2> <Right Shift>

In C terms, this command performs roughly the expression <Result> = (<Factor 1> * <Factor 2>) >> <Right Shift>.

In CON terms, it is very similar to the following series of commands:

set <Result> <Factor 1>
mul <Result> <Factor 2>
shiftr <Result> <Right Shift>

The main difference is that the calculation uses 64 bits to prevent overflow, while using gamevars for intermediate values would limit it to 31 bits.

One example of where this limitation takes places is with rotatesprite scaling, where 320*65536 scaled with statusbarscale is dangerously close to 31 bits.

Another example is how the trigonometric commands sin and cos use a fixed-point fractional scaling factor of 1:16384, corresponding to a 14 bit shift. So, if you wanted to multiply some length by the cosine of the current sprite's angle, you could write:

 cos cosang sprite[].ang
 mulscale result length cosang 14