Mulscale: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
extended this a bit
Fox (talk | contribs)
No edit summary
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''mulscale''' ''<res>'' <eax> <edx> <ecx>
'''mulscale''' <Result> <Factor 1> <Factor 2> <Right Shift>


The formula is<br/>  
In C terms, this command performs roughly the expression <code><Result> = (<Factor 1> * <Factor 2>) >> <Right Shift></code>.
res=(eax*edx)>>ecx


The intermediate value is computed using 64 bits to prevent overflow and the result is 32 bits wide.
In CON terms, it is very similar to the following series of commands:
This is a math function which is implemented in assembler programing language because it makes the calculation faster.


The primary use of this function is in calculations involving scaled integers, that is, integers that have a binary point somewhere by convention. For example, the trigonometric functions [[Sin]] and [[Cos]] use a scaling of 1:16384, corresponding to a 14 bit shift. So, if you wanted to multiply some length (say, ''dx'') by a cosine value (say, ''cosang''), you could write
set <Result> <Factor 1>
mul <Result> <Factor 2>
shiftr <Result> <Right Shift>


  '''mulscale''' ''dx'' ''cosang'' 14
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.


For example, [[sin]] or [[cos]] use a scaling factor of 1:16384, corresponding to a 14 bit shift. So if you wanted to scale the result with a certain length, you would do:
  sin ''Sine'' sprite[].ang
  '''mulscale''' ''Result'' ''Length'' ''Sine'' 14
See also [[divscale]] and [[scalevar]].


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

Latest revision as of 13:09, 14 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.

For example, sin or cos use a scaling factor of 1:16384, corresponding to a 14 bit shift. So if you wanted to scale the result with a certain length, you would do:

 sin Sine sprite[].ang
 mulscale Result Length Sine 14

See also divscale and scalevar.