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
Hi qWord,
well done. :t You're the real macro wizzard. Is it only for jWasm?
Gunther
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
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.
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
very cool, qWord :t
Thanks qWord,
Well done, very usefull macros. :t
seems to be a good idea, qWord :t
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)
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
nice :t
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
Quote
time for some more macro fun !!!!!!
For fun can you made c++ macro like =+, =- ...
Must be not to much difficult to do.
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
i think these macros are great
i'm just not working on anything that needs them, at the moment
Quote from: dedndavei think these macros are great
i'm just not working on anything that needs them, at the moment
- me 2. You're the macro whizzzard, qWord!
I'm making something wrong, obviously.
vv1 = 7 vv2 = 3600
vv1 * vv2 = 25200
result1 8688.000000
result2 496
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
include \masm32\include\masm32rt.inc
include \masm32\macros\real_math.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
comment * -----------------------------------------------------
Build this console app with
"MAKEIT.BAT" on the PROJECT menu.
----------------------------------------------------- *
.const
MR_FROM_EXPR32 vv1, 7
MR_FROM_EXPR32 vv2, 3600
MR_MUL r_2, vv1,vv2
value REAL8 MR_TO_IEEE (<REAL8>, r_2) ; --------- Problem here , result1
.data
item dd 0
.code
start:
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
call main
inkey
exit
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
main proc
LOCAL sz[128]:CHAR
cls
MR_FROM_EXPR32 vv1, 7
MR_FROM_EXPR32 vv2, 3600
MR_MUL r_2, vv1,vv2
mov item, MR_TO_INT32(vv1, 0.1)
mov sz[0],0
print cat$(ADDR sz," vv1 = ", udword$(item)), 9
mov item, MR_TO_INT32(vv2, 0.1)
mov sz[0],0
print cat$(ADDR sz," vv2 = ", udword$(item)),13,10,10
mov item, 25200
mov sz[0],0
print cat$(ADDR sz," vv1 * vv2 = ", udword$(item)),13,10,10
print real8$(value),13,10,10
mov item, MR_TO_INT32(r_2, 0.1) ; ---------------- Problem here , result2
print udword$(item),13,10,10
inkey
ret
main endp
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
end start
Thanks. HSE
You are using jwasm (or one of its forks), where the HIGHWORD-operator is broken. Because this bug is simple to solve, I will not made any attempt to fix this from macro side.
regards,
qWord
Fantastic!! (not my error this time :biggrin:)
Thak you very much. HSE
BTW: some time back I've upload jwasm version 212pre after fixing some bugs myself:
http://masm32.com/board/index.php?topic=2941.msg30728#msg30728
Yes, thank! I found Habran and Johnsa take the corrección in HJWASM.
I am still using a modified JWAsm13. (because the so many macro levels when using ObjAsm and SmplMath)
LATER: I modified HJWASM last version and now MREAL is working in the real program.
⇒ user count ≥ 3 :t
Hi qWord!
Perfect: MR_FROM_STR rr1, 2.1E-4
Wrong:
MR_FROM_STR rr2, 8.4E-4
Integ01.asm(8) : Error A2114: forced error: sorry - value to large or small for conversion
Only one MR_FROM_STR work. The followings not work. It's a jwasm problem. Do you know what the problem is?
Thanks. HSE
Quote from: HSE on April 10, 2016, 06:55:55 AMIt's a jwasm problem. Do you know what the problem is?
It is a problem with the function CreateVariable() in equate.c (https://sourceforge.net/p/jwasm/bugs/303/), which does not clear the upper 32 bits of already existing equates.
Thanks :t . My HJWasm work now!