News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

floating point arithmetic while assembling: MREAL-macros

Started by qWord, May 26, 2014, 05:05:17 PM

Previous topic - Next topic

qWord

Edit 10/2020: The code is also available at github: https://github.com/qwordAtGitHub/mreal-macros. There will be probably no further development from my side.

Hello all,

I've written some macros that allows floating point arithmetic while assembling. The goal was to implement a subset of the IEEE 754-2008 standard, as far as possible with MASM's preprocessor.
The key features are:
  • arbitrary precision: multiples of 16: 16, 32, 48, 64, ... bits
  • correctly rounded arithmetic: addition, subtraction, multiplication, division, square root and fused multiply accumulate.
  • rounding modes:

    • round to nearest, half to even (IEEE754 default mode)
    • round to nearest, half away from zero
    • round toward -∞ (round down)
    • round toward +∞ (round up)
    • round toward zero (truncate)
For more details please read the comment block at top of the file real_math.inc (see attachment).

With version 1.0.4 a new macro front-end has been introduced (in file ceval.inc), which simplify the usage enormously, because it allows to enter mathematical expressions, as known from high level programming languages:

include real_math.inc
include ceval.inc

; evaluate some expression
ceval x = 123.456 * -2 ^ 4 * ( sqrt(2) + 1 )
echo_mreal ans
echo_mreal x

; test condition
IF ccond( x lt 0 || x gt 123 )
echo foo
ENDIF

.const
; define const REAL8 value
someConst REAL8 cReal8( sqrt(2)/2 )


The following example shows the usage of the low level API (real_math.inc):
include real_math.inc

; Default precision is 64 bit and the
; rounding mode is "to nearest, half to even"
MREAL x = 1, y = 10
MR_DIV r,x,y           ; r = x/y

; output to build console
%echo MR_TO_UINT32(x)/MR_TO_UINT32(y) = MR_TO_IEEE_HEX_SEQ(r) = MR_TO_DECIMAL(r,2)

; convert r to REAL8
foo REAL8 MR_TO_IEEE(<REAL8>,r)

; define REAL10 value in current segment
MR_DECL_REAL10 foo2, r

; load some other value
MREAL c = 2.99792458E8

; get the square root of c
MR_SQRT r,c     ; r = sqrt(c)
%echo Sqrt(MR_TO_DECIMAL(c)) = MR_TO_DECIMAL(r)


In the attachment you can find more examples. Remarks that the macros currently won't work with jWasm due to incompatibilities.

If you find bugs or think something could be done better or is missing, please give some feed back (preferable with some code).

regards,
qWord

EDIT: new attachment (real_math.inc v1.0.4, ceval.inc v1.0.2); reason: bugfix for ceval macro

MREAL macros - when you need floating point arithmetic while assembling!

Gunther

Hi qWord,

well done.  :t You're the real macro wizzard. Is it only for jWasm?

Gunther
You have to know the facts before you can distort them.

jj2007

Works fine :t

Where would you typically use it?


x = +123000, y = -456
x+y =  1.2254400E+5 =  0x0.EF58000000000000p17 ~=~ +122544
x-y =  1.2345600E+5 =  0x0.F120000000000000p17 ~=~ +123456
x*y = -5.6088000E+7 = -0x0.D5F5700000000000p26 ~=~ -56088000
x/y = -2.6973684E+2 = -0x0.86DE50D79435E50Dp9  ~=~ -270

max(6.62E-34, 6.67E-11) = 6.67E-11

Pi - floor(Pi) = 1.4159265E-1

qWord

Quote from: jj2007 on May 26, 2014, 09:57:30 PMWhere would you typically use it?
When ever you need FP arithmetic while assembling  :biggrin:
Guess the case you've got some macros that create (for example) FPU code for given expression and you want to calculate constant terms while assembling rather than at runtime (as optimization) - actual that was the motivation to start the project, whereas later it was to deepen my knowledge on FP arithmetic (and IEEE754).

Quote from: Gunther on May 26, 2014, 07:30:24 PMIs it only for jWasm?
as I wrote, it does not work with jWasm. I've test the macros successfully with MASM version 6-10.
MREAL macros - when you need floating point arithmetic while assembling!

Gunther

Hi qWord,

anyway, the idea is good.

Quote from: qWord on May 26, 2014, 11:19:44 PM
as I wrote, it does not work with jWasm. I've test the macros successfully with MASM version 6-10.

Thank you for the information.

Gunther
You have to know the facts before you can distort them.

dedndave


Siekmanski

Creative coders use backward thinking techniques as a strategy.

RuiLoureiro


qWord

Just added one more macro that simplifies the initialization of one or more MREAL values:
MREAL x = -123 , y = 0x123 , z = 0.005 , k = 0f00h
See the attachment in the first post.


The following macro might be useful for people working with GDI+ or DirectX/Draw/..., because it allows to use floating point immediates (REAL4) as instruction parameter or with the INVOKE directive.
immFP4 macro numeric_literal:req
MR_FROM_STR ?x,numeric_literal
fp4__txt TEXTEQU MR_TO_IEEE(<REAl4>,?x)
fp4__size SIZESTR fp4__txt
EXITM @CatStr(@SubStr(%fp4__txt,1,fp4__size-1),<h>)
endm

example usage:

mov eax,immFP4(-123.E-8)
;
invoke GdipAddPathRectangle,path,0,0,immFP4(2.25),immFP4(3.3)

(the classical approach is to create an anonym REAL4 variable, which is copied at runtime. For compare see the FP4-macro of the MASM32 SDK)
MREAL macros - when you need floating point arithmetic while assembling!

qWord

time for some more macro fun: I've extend the MREAL macros with a new front-end that directly accepts mathematical expression, thus you can write things like this:
ceval x = 1/10
echo_mreal x

ceval x/fac(1) - x^3/fac(3) + x^5/fac(5)   ; fac = factorial
echo_mreal ans,20

IF ccond( (true and not false)^2 || x lt sqrt(pi/2) )
   echo foo
ENDIF

.const
REAL8 cReal8(pi^-2/4)


For those who are interested in: the shunting-yard algorithm is used to parse the expressions, just with a small modification to allow unary operators.

regards,
qWord

MREAL macros - when you need floating point arithmetic while assembling!

dedndave


Biterider

Hi qWord
I recently introduced MREAL into a real live application. It works as expected and it is true relief for setting up constants.
Thank you very much for your efforts!

Biterider

TouEnMasm

Quote
time for some more macro fun !!!!!!
For fun can you made c++ macro like =+, =- ...
Must be not to much difficult to do.


Fa is a musical note to play with CL

qWord

Quote from: Biterider on October 06, 2015, 07:15:28 PMI recently introduced MREAL into a real live application. It works as expected and it is true relief for setting up constants.
⇒ user count ≥ 2 ■  :biggrin:
Thanks for the feed back. Let's see if I can melt these macros with SmplMath (most probably in a new set of macros, rather than extending existing ones)


regards, qWord
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

i think these macros are great
i'm just not working on anything that needs them, at the moment