### Author Topic: floating point arithmetic while assembling: MREAL-macros  (Read 16840 times)

#### qWord

• Member
• Posts: 1476
• The base type of a type is the type itself
##### floating point arithmetic while assembling: MREAL-macros
« on: May 26, 2014, 05:05:17 PM »
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:
Code: [Select]
`include real_math.incinclude ceval.inc; evaluate some expressionceval x = 123.456 * -2 ^ 4 * ( sqrt(2) + 1 )echo_mreal ansecho_mreal x; test conditionIF ccond( x lt 0 || x gt 123 ) echo fooENDIF.const; define const REAL8 valuesomeConst REAL8 cReal8( sqrt(2)/2 )`
The following example shows the usage of the low level API (real_math.inc):
Code: [Select]
`include real_math.inc; Default precision is 64 bit and the; rounding mode is "to nearest, half to even"MREAL x = 1, y = 10MR_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 REAL8foo REAL8 MR_TO_IEEE(<REAL8>,r); define REAL10 value in current segmentMR_DECL_REAL10 foo2, r; load some other valueMREAL c = 2.99792458E8; get the square root of cMR_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

« Last Edit: October 05, 2020, 02:48:52 AM by qWord »
MREAL macros - when you need floating point arithmetic while assembling!

#### Gunther

• Member
• Posts: 3596
• Forgive your enemies, but never forget their names
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #1 on: May 26, 2014, 07:30:24 PM »
Hi qWord,

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

Gunther
Get your facts first, and then you can distort them.

#### jj2007

• Member
• Posts: 11053
• Assembler is fun ;-)
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #2 on: May 26, 2014, 09:57:30 PM »
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

• Member
• Posts: 1476
• The base type of a type is the type itself
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #3 on: May 26, 2014, 11:19:44 PM »
Where would you typically use it?
When ever you need FP arithmetic while assembling
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).

Is 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

• Member
• Posts: 3596
• Forgive your enemies, but never forget their names
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #4 on: May 27, 2014, 02:08:33 AM »
Hi qWord,

anyway, the idea is good.

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
Get your facts first, and then you can distort them.

#### dedndave

• Member
• Posts: 8829
• Still using Abacus 2.0
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #5 on: May 27, 2014, 02:48:46 AM »
very cool, qWord   :t

#### Siekmanski

• Member
• Posts: 2358
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #6 on: May 27, 2014, 05:47:18 AM »
Thanks qWord,
Well done, very usefull macros.  :t
Creative coders use backward thinking techniques as a strategy.

#### RuiLoureiro

• Member
• Posts: 819
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #7 on: May 27, 2014, 06:25:53 AM »
seems to be a good idea, qWord  :t

#### qWord

• Member
• Posts: 1476
• The base type of a type is the type itself
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #8 on: June 26, 2014, 10:23:49 AM »
Just added one more macro that simplifies the initialization of one or more MREAL values:
Code: [Select]
`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.
Code: [Select]
`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:
Code: [Select]
`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

• Member
• Posts: 1476
• The base type of a type is the type itself
##### contant expression evaluation - ceval macros
« Reply #9 on: September 25, 2015, 02:42:50 PM »
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:
Code: [Select]
`ceval x = 1/10echo_mreal xceval x/fac(1) - x^3/fac(3) + x^5/fac(5)   ; fac = factorialecho_mreal ans,20IF ccond( (true and not false)^2 || x lt sqrt(pi/2) )   echo fooENDIF .constREAL8 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

• Member
• Posts: 8829
• Still using Abacus 2.0
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #10 on: September 26, 2015, 03:17:38 AM »
nice   :t

#### Biterider

• Member
• Posts: 613
• ObjAsm Developer
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #11 on: October 06, 2015, 07:15:28 PM »
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

• Member
• Posts: 1530
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #12 on: October 07, 2015, 12:31:39 AM »
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

• Member
• Posts: 1476
• The base type of a type is the type itself
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #13 on: October 10, 2015, 09:23:50 AM »
I recently introduced MREAL into a real live application. It works as expected and it is true relief for setting up constants.
⇒ user count ≥ 2 ■
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
« Last Edit: October 10, 2015, 01:55:53 PM by qWord »
MREAL macros - when you need floating point arithmetic while assembling!

#### dedndave

• Member
• Posts: 8829
• Still using Abacus 2.0
##### Re: floating point arithmetic while assembling: MREAL-macros
« Reply #14 on: October 11, 2015, 05:38:40 AM »
i think these macros are great
i'm just not working on anything that needs them, at the moment