flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [proposition add @fpuMul]How int convert to float?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 06:07
My opinion fasmw 1.73 must have preprocessor command @fpuDiv or @fpuMul.

For example: a = 3005+3015 (analog float 30.05+30.15)
Than convert int to float:
divval = 100
data1 dd @fpuDiv a,divval ;fasmw return float value 60.20
Very simple code implementation to fasmw:
Code:
buf1 dd 0
;@fpuDiv for preprocessing fasmw 1.73
        finit
        mov dword [buf1],divval
        fild dword [buf1]
        mov dword [buf1],a 
        fild dword [buf1]
        fdiv st,st1
        fstp dword [buf1] ;out result 
ret
    


I found this code
Code:
float IntBitsToFloat(long long int bits)
{
    int sign     = ((bits & 0x80000000) == 0) ? 1 : -1;
    int exponent = ((bits & 0x7FFFFFFF) >> 23)-150;
    int mantissa =  (bits & 0x007FFFFF);

    mantissa |= 0x00800000;
   // Calculate the result:
   float f = (float)(sign * mantissa * Power(2, exponent));
   return f;
}
;and found this mikle__ code. int16 bits to float 32bits
x dw -10
y dd 0
section .text
        mov ax,[x]
        or ax,ax
        jz exit
        jns @f
        mov byte [y+3],80h
        neg ax
@@:     mov cl,16
        bsr bx,ax ;how this replaced for fasm preprocessor?
        sub cl,bl
        shl ax,cl
        shr ax,1
        add bx,127
        shl bx,7
        or word [y+2],bx
        or word [y+1],ax
exit:  

;int32bits to float32bits
mov eax,-436
        or eax,eax
        jz @f
        cdq ; if eax <0 then edx:=-1 else edx:=0
        xor eax,edx
        sub eax,edx
        and edx,256; Sign bit
        bsr ecx,eax
        ror eax,cl
        add eax,edx
        lea eax,[eax+ecx+126]
        rol eax,23;eax=0C3DA0000h=-436.0
@@:
    

My question how write macro to convert int to float?
Its usefull for preprocessor for Fasmw 1.73


Last edited by Roman on 06 May 2025, 15:07; edited 14 times in total
Post 04 May 2025, 06:07
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 09:08
Code:
macro mmflt v { mov eax,v#f }
;in code
tt = (-2*5)
mmflt tt ;fasm error undefined ttf    


I do
Code:
macro mmflt v { rept 1 n:v \{
           mov eax,n\#f \} } 

tt equ (-2*5)
mmflt tt        ;get mov eax,0xc1200000 its -10.0
mmflt (-2*10)   ;get mov eax,0xc1a00000 its -20.0
mmflt (-2*10)/2 ;get mov eax,0xc1200000 its -10.0
mmflt (-1/2)    ;get mov eax,0
    
Post 04 May 2025, 09:08
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 10:07
Code:
macro mmflt v,m=.0 { rept 1 n:v \{
           mov eax,n\#m \} } 
tt equ (-5/2)
mmflt tt,.5  ;get mov eax,0xc0200000 = -2.5

tt equ (-1/2)
mmflt tt,.5  ;get mov eax,0x3f000000 = 0.5
or eax,0x80000000 ;get -0.5
    
Post 04 May 2025, 10:07
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1708
Location: Toronto, Canada
AsmGuru62 04 May 2025, 11:51
Or you can use FPU.
Post 04 May 2025, 11:51
View user's profile Send private message Send e-mail Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 13:04
Quote:

Or you can use FPU.

My target preprocessing. And generate binary float data from macro.
Post 04 May 2025, 13:04
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 04 May 2025, 14:17
Only the preprocessor works with text. He doesn't count anything.
Post 04 May 2025, 14:17
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 04 May 2025, 14:28
macomics wrote:
Only the preprocessor works with text. He doesn't count anything.
With the exception of rept, it can do arithmetic at the preprocessor stage.
Post 04 May 2025, 14:28
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 15:03
Quote:
With the exception of rept

That's what I thought. Smile

And what about irps,irpv ?
Post 04 May 2025, 15:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20625
Location: In your JS exploiting you and your system
revolution 04 May 2025, 15:11
Roman wrote:
And what about irps,irpv ?
There is no arithmetic in those. Just plain iterations. You can make a rudimentary counter, but it is clumsy.
Post 04 May 2025, 15:11
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 04 May 2025, 15:16
Ok.
How do this with rept ?
macro addFlt 2:3,2:7

Idea b=3+7=10
if b >=10 than c=1 and b-10
a=2+2+c

2:3 equivalent float 2.3
2:7 equivalent float 2.7

result 5.0 float
Post 04 May 2025, 15:16
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 05 May 2025, 09:11
My opinion fasmw 1.73 must have preprocessor command @fpuDiv or @fpuMul.

For example: a = 3005+3015 (analog float 30.05+30.15)
Than convert int to float:
data1 dd @fpuDiv a,100 ;fasmw return float value 30.20
Post 05 May 2025, 09:11
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 05 May 2025, 17:00
https://wasm.in/threads/kak-zapixnut-v-registr-4x-bajtovoe-float-chislo.28260/?ysclid=mabbk4ahg3554685210

masm macro INT2FLT and @FltMul
How this macro write for fasmw 1.73 ?
Code:
;multiply two floats numbers
@FltMul MACRO  FltNum1:REQ,FltNum2:REQ
  __?FltResult = ((FltNum1) AND 80000000h) XOR ((FltNum2) AND 80000000h)
  IF ((FltNum1) AND 7fffffffh) NE 0
    IF ((FltNum2) AND 7fffffffh) NE 0
      IF ((FltNum1) AND 7f800000h) NE 7f800000h
        IF ((FltNum2) AND 7f800000h) NE 7f800000h
          __?FltQPart = ((FltNum1) SHR 23) AND 0ffh
          __?FltQPart = __?FltQPart+(((FltNum2) SHR 23) AND 0ffh)-127
          __?FltMPart = ((FltNum1) AND 007fffffh) OR 00800000h
          __?FltMPart = @Raw25Mul24r(__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
          IF (__?FltMPart AND 01000000h) NE 0
            __?FltQPart = __?FltQPart+1
            __?RoundBit = __?FltMPart AND 1  ;new round correction
            __?FltMPart = __?FltMPart SHR 1
          ENDIF
          __?FltMPart = __?FltMPart+__?RoundBit
          IF __?FltQPart GT 0
            IF __?FltQPart LT 255
              __?FltResult = __?FltResult OR (__?FltQPart SHL 23) OR \
                                             (__?FltMPart AND 007fffffh)
            ELSE
              __?FltResult = __?FltResult OR 7f800000h
            ENDIF
          ENDIF
        ELSE
          __?FltResult = __?FltResult OR ((FltNum2) AND 7fffffffh)
        ENDIF
      ELSE
        __?FltResult = __?FltResult OR ((FltNum1) AND 7fffffffh)
      ENDIF
    ENDIF
  ENDIF
  EXITM <__?FltResult>
ENDM    
Post 05 May 2025, 17:00
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 05 May 2025, 17:43
Even if you rewrite it, it will clearly not be for the preprocessor, but for the assembler.
1) NE is <>
2) @Raw25Mul24r is macro too
There is no need to change anything else.
Post 05 May 2025, 17:43
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 05 May 2025, 20:38
I rewrite for fast but not working and strange behavior when used macro @fltMul
Code:
macro @Raw25Mul24r Num1,Num2 {
  __?Num1Lo = (Num1) AND 0fffh
  __?Num1Hi = (Num1) SHR 12
  __?Num2Lo = (Num2) AND 0fffh
  __?Num2Hi = (Num2) SHR 12
  __?Result = (__?Num1Lo*__?Num2Lo) SHR 12
  __?Result = (__?Result+__?Num1Hi*__?Num2Lo+__?Num1Lo*__?Num2Hi) SHR 10
  __?RoundBit = __?Result AND 1
  __?Result = __?Result SHR 1
  __?Result = __?Result+((__?Num1Hi*__?Num2Hi) SHL 1)
  }

MACRO @FltMul  FltNum1,FltNum2 {
  __?FltResult = ((FltNum1) AND 80000000h) XOR ((FltNum2) AND 80000000h)
  IF ((FltNum1) AND 7fffffffh) <> 0
    IF ((FltNum2) AND 7fffffffh) <> 0
      IF ((FltNum1) AND 7f800000h) <> 7f800000h
        IF ((FltNum2) AND 7f800000h) <> 7f800000h
          __?FltQPart = ((FltNum1) SHR 23) AND 0ffh
          __?FltQPart = __?FltQPart+(((FltNum2) SHR 23) AND 0ffh)-127
          __?FltMPart = ((FltNum1) AND 007fffffh) OR 00800000h
          __?FltMPart = @Raw25Mul24r(__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
          IF (__?FltMPart AND 01000000h) <> 0
            __?FltQPart = __?FltQPart+1
            __?RoundBit = __?FltMPart AND 1  ;new round correction
            __?FltMPart = __?FltMPart SHR 1
          END IF
          __?FltMPart = __?FltMPart+__?RoundBit
          IF __?FltQPart > 0
            IF __?FltQPart < 255
              __?FltResult = __?FltResult OR (__?FltQPart SHL 23) OR \
                                             (__?FltMPart AND 007fffffh)
            ELSE
              __?FltResult = __?FltResult OR 7f800000h
            END IF
          END IF
        ELSE
          __?FltResult = __?FltResult OR ((FltNum2) AND 7fffffffh)
        END IF
      ELSE
        __?FltResult = __?FltResult OR ((FltNum1) AND 7fffffffh)
      END IF
    END IF
  END IF
  }       
Post 05 May 2025, 20:38
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 05 May 2025, 20:40
in code
Code:
;this is problem __?FltResult = ((FltNum1) AND 80000000h) XOR ((FltNum2) AND 80000000h) 
jmp @f ;fasm get error undefined @@. If comment @FltMul then ok. no error
      @FltMul 2.0,2.0
     mov eax,__?FltResult
  @@:    


this variant get error on __?FltMPart = @Raw25Mul24r(__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
Code:

jmp @f 
      @FltMul 0x3f800000,0x3f800000
     mov eax,__?FltResult
  @@:    
Post 05 May 2025, 20:40
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1149
Location: Russia
macomics 06 May 2025, 03:19
Code:
@Raw25Mul24r(__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
__?FltMPart = __?Result    
Post 06 May 2025, 03:19
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 06 May 2025, 10:02
macro @Raw25Mul24r Num1,Num2 {
__?Num1Lo = Num1 AND 0x0fff ;fasm error invalid value
...
}

I do
Code:
;rewrite  __?FltMPart = @Raw25Mul24r(__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
__ee1 =  ((FltNum2) AND 007fffffh) OR 00800000h
          @Raw25Mul24r __?FltMPart,__ee1
          __?FltMPart = __?Result     

Wow its work !
I checked result here https://gregstoll.com/~gregstoll/floattohex/
I do:
Code:
@FltMul 0x40400000,0x40400000 ;3.0,3.0
     mov eax,__?FltResult ;and get float 9.0 = 0x41100000
@FltMul 0x40400000,0x3f000000 ;3,0.5
     mov eax,__?FltResult ;get 3fc00000 = float 1.5 
    


Last edited by Roman on 06 May 2025, 10:26; edited 1 time in total
Post 06 May 2025, 10:02
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 06 May 2025, 10:19
http://jasperbekkers.nl/posts/floating-point/
right variant macro @FltMul
Code:
macro @Raw25Mul24r Num1,Num2 {
  __?Num1Lo = ((Num1) and 0x0fff)
  __?Num1Hi = (Num1) SHR 12
  __?Num2Lo = (Num2) AND 0fffh
  __?Num2Hi = (Num2) SHR 12
  __?Result = (__?Num1Lo*__?Num2Lo) SHR 12
  __?Result = (__?Result+__?Num1Hi*__?Num2Lo+__?Num1Lo*__?Num2Hi) SHR 10
  __?RoundBit = __?Result AND 1
  __?Result = __?Result SHR 1
  __?Result = __?Result+((__?Num1Hi*__?Num2Hi) SHL 1)
  }

macro @FltMul  FltNum1,FltNum2,reg=0 {
  __?FltResult = ((FltNum1) AND 80000000h) XOR ((FltNum2) AND 80000000h)
  IF ((FltNum1) AND 7fffffffh) <> 0
    IF ((FltNum2) AND 7fffffffh) <> 0
      IF ((FltNum1) AND 7f800000h) <> 7f800000h
        IF ((FltNum2) AND 7f800000h) <> 7f800000h
          __?FltQPart = ((FltNum1) SHR 23) AND 0ffh
          __?FltQPart = __?FltQPart+(((FltNum2) SHR 23) AND 0ffh)-127
          __?FltMPart = ((FltNum1) AND 007fffffh) OR 00800000h
          ;__?FltMPart = @Raw25Mul24r (__?FltMPart,((FltNum2) AND 007fffffh) OR 00800000h)
          __ee1 =  ((FltNum2) AND 007fffffh) OR 00800000h
          @Raw25Mul24r __?FltMPart,__ee1
          __?FltMPart = __?Result
          IF (__?FltMPart AND 01000000h) <> 0
            __?FltQPart = __?FltQPart+1
            __?RoundBit = __?FltMPart AND 1  ;new round correction
            __?FltMPart = __?FltMPart SHR 1
          END IF
          __?FltMPart = __?FltMPart + __?RoundBit
          IF __?FltQPart > 0
            IF __?FltQPart < 255
              __?FltResult = __?FltResult OR (__?FltQPart SHL 23) OR \
                                             (__?FltMPart AND 007fffffh)
            ELSE
              __?FltResult = __?FltResult OR 7f800000h
            END IF
          END IF
        ELSE
          __?FltResult = __?FltResult OR ((FltNum2) AND 7fffffffh)
        END IF
      ELSE
        __?FltResult = __?FltResult OR ((FltNum1) AND 7fffffffh)
      END IF
    END IF
  END IF
if reg eqtype eax | reg eqtype [0]
    mov reg,__?FltResult
    end if
  }   

;in code
result dd 0

      @FltMul 0x40400000,0x40400000,eax
      @FltMul 0x40400000,0x40400000,ebx
      @FltMul 0x40400000,0x40400000,[result] 
    
Post 06 May 2025, 10:19
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1938
Roman 09 May 2025, 01:48
Code:
macro @ror x,b {
 m = (1 shl b) - 1
 t = x and m
 z = x shr b
 @rorv = z or (t shl (32-b)) and 0xffffffff
}
macro @rol v,b {
 m = (1 shr b) - 1
 t = v and m
 z = v shl b
 @rolv = z or (t shr (32-b)) and 0xffffffff

}

macro @int2Flt v {
__@v0 = (v) or v
      __mantis@v = bsr __@v0

 @ror __@v0,__mantis@v
 __@v1 = @rorv

      __@v2 = __@v1+__mantis@v+126

@rol __@v2,23
__@reslt = @rolv
      mov eax,__@reslt
}
@int2Flt (122 +122 ) ;= float 244.0
;0.01=0x3c23d70a
@FltMul  0x3c23d70a,__@reslt,ebx ;= float 2.44

;sign
macro @int2Flt v {
__@v0 = (v) or v
__@sign = (__@v0 and 0x80000000)
if __@sign > 0
__@v0 =  (not (__@v0)+1) and 0x7fffffff;(__@v0 and 0x7fffffff)
end if
      __mantis@v = bsr __@v0

@ror __@v0,__mantis@v
 __@v1 = @rorv

      __@v2 = __@v1+__mantis@v+126

@rol __@v2,23
__@reslt = @rolv or __@sign
      mov eax,__@reslt
}

@int2Flt (122 -123 ) ;= float -1.0
;0.01=0x3c23d70a
@FltMul  0x3c23d70a,__@reslt,ebx ;= float -0.01

;or more simpler
muu equ 10-18
      rept 1 n:muu { muu equ n#f}
      mov eax,muu        ;= 0xC1000000 = float -8.0

    
Post 09 May 2025, 01:48
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.