News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Assemble-time floating point math

Started by Rockoon, October 30, 2007, 12:45:24 AM

Previous topic - Next topic

Rockoon


Trying to build a table from some floating point equates but keep running into an error.. have tried several techniques but none work..

a stripped down example of the problem:

f0 equ 1.1011
f1 equ 0.50110

.data

float4 f0 - f1

The error is:

"error A2008: syntax error : floating point constant"

..what are the work-arounds for this issue?
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

raymond

Quote..what are the work-arounds for this issue?
Declare and initialize them in the data section instead of using equates.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Rockoon

Quote from: raymond on October 30, 2007, 03:39:17 AM
Quote..what are the work-arounds for this issue?
Declare and initialize them in the data section instead of using equates.


How exactly is that going to help?


When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

Rockoon

Ah, I discovered that Float4 is a macro in macros.inc

replacing Float4 with Real4 gives a different error: "error A2187: must use floating-point initializer"

The problem still stands .. is it possible to do floating point math at assemble-time?
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

u

No, it's a limitation of masm. You need to find a workaround for your needs. (computing the stuff outside, or making a tiny app to compute+store them in an .inc file)
Please use a smaller graphic in your signature.

ToutEnMasm

Hello,
Floating points numbers are a human view.
They must be translate (IEEE) by the compiler,result is put in data.Any language do that,it isn't particular to masm.
C++ hide this,but the resulting code is the same.FPN are in data.
With masm,things are made in two steps.Create a constant,put it in a data and the translation is made.
Macros are only a help to do that.



Rockoon

Quote from: Ultrano on October 30, 2007, 07:00:56 AM
(computing the stuff outside, or making a tiny app to compute+store them in an .inc file)


ah well.. guess it is time to try a new assembler.. abslutely no reason for a source file to require a support binary :(

When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

MichaelW

#7
But what assembler would that be?

For NASM:

"NASM cannot do compile-time arithmetic on floating-point constants."

For FASM:

"flat assembler does not allow any floating point operations at compilation time"

For GoAsm I could not find any similar statement, but I suspect that it cannot.

In my test with GAS, this statement:

.double 1.1011 - 0.50110

Resulted in:

Error: junk at end of line, first unrecognized character is "-"

For TASM I don't know and I have no reasonable way to test.

EDIT: And I forgot PoAsm, but it's too late to test.



eschew obfuscation

Mark Jones

Time to make your own assembler! :bdg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Rockoon

Well, HLA supports this.

Unfortunately its HLA (no effense Hyde, but I have never been a fan of pointless syntax sugar)

Now considering two alternatives:

o) using an ansi-C compiler for the table definitions
o) creating macros that manipulate floating point values as strings

Note: GoAsm doesnt support this, and doesn't seem to raise an error either (unless easycode is hiding it from me)
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

raymond

Quotecreating macros that manipulate floating point values as strings

In order to do that, you need to be entirely familiar with the floating point format and it will not be an easy task. If you need to brush up on the IEEE floating point format, you can have a glance at:
http://www.ray.masmcode.com/tutorial/fpuchap2.htm#floats

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Rockoon

I assure you that I am extremely fluent with IEEE float representation.. 32 bit, 64 bit, and 80 bit.. :) I've twiddled them from time to time....

I am also familiar with the opengl 16-bit float specification but I am not sure that any GPU hardware actualy impliments them in that specific format
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

raymond

QuoteI assure you that I am extremely fluent with IEEE float representation..
Glad to hear that. You are in a minority. :clap: The link was offered just in case you were part of the 99% of programmers who don't have any clue about that format.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

MichaelW

The addition part of this turned out to be fairly straightforward, but I have no idea how to reasonably do the subtraction, and I don’t even want to think about multiplication or division. There is no error handling, and I haven’t thoroughly tested it, but it basically seems to work up to the point that either component (integer or fractional) exceeds 2^32-1.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    FPADD MACRO arg1, arg2

      LOCAL i1,f1,i2,f2,szf1,szf2,nf,ni,tf,ti,tout

      ;; --------------------------------------
      ;; Extract integer and fractional parts.
      ;; --------------------------------------

      ppos INSTR <arg1>, <.>
      i1 SUBSTR <arg1>, 1, ppos - 1
      f1 SUBSTR <arg1>, ppos + 1
      ppos INSTR <arg2>, <.>
      i2 SUBSTR <arg2>, 1, ppos - 1
      f2 SUBSTR <arg2>, ppos + 1

      ;; -------------------------------------------------------
      ;; Pad fractional parts to effectively align radix point.
      ;; -------------------------------------------------------
     
      szf1 = @SizeStr(%f1)
      szf2 = @SizeStr(%f2)
      WHILE szf1 LT szf2
        f1 CATSTR f1, <0>
        szf1 = @SizeStr(%f1)
      ENDM
      WHILE szf2 LT szf1
        f2 CATSTR f2, <0>
        szf2 = @SizeStr(%f2)
      ENDM

      ;; ------------------------------------
      ;; Perform the addition on both parts.
      ;; ------------------------------------

      nf = f1 + f2
      ni = i1 + i2

      ;; ---------------------------------------
      ;; Shift any carry into the integer part.
      ;; ---------------------------------------

      IF @SizeStr(%nf) GT szf1
        tf TEXTEQU @SubStr(%nf,2)
        ni = ni + 1
      ELSE
        tf TEXTEQU @SubStr(%nf,1)
      ENDIF
      ti TEXTEQU @SubStr(%ni,1)

      tout CATSTR ti, <.>, tf
      EXITM <tout>

    ENDM

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      x   dq 2147483648.2147483648
      y   dq 2147483647.2147483647
      z   dq 0
      zz  dq FPADD(2147483648.2147483648, 2147483647.2147483647)
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    fld   x
    fadd  y
    fstp  z
    invoke crt_printf, chr$("%f%c"),z,10
    invoke crt_printf, chr$("%f%c"),zz,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

Rockoon

ah string math.. thats another strategy.. tx

When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.