flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Main > load 128 bit signed integer to st0

Author
Thread Post new topic Reply to topic
petelomax



Joined: 19 Jan 2013
Posts: 4
Location: London
load 128 bit signed integer to st0
OK, I am starting to feel a little stupid now.. I have a 128-bit signed integer in rdx:rax, how do I get it (or a reasonable representation of it) into st0?

It is in an error handler, so neither speed nor pin-point accuracy is an issue, but getting 0 or -Infinity, the best I have yet managed, just because it is 65 bits (about 3e19) is not on!

Pete
Post 31 Jul 2017, 09:44
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15299
Location: Bigweld Industries
I guess the process could be like this:
  1. Scan the integer for the most significant bit (note that negative numbers make this a little bit tricky).
  2. Shift it left to move out the higher bits.
  3. Load the highest 64-bit part of the integer into st0.
  4. Scale st0 into range by the amount you shifted in step 2.
Post 31 Jul 2017, 10:23
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
Load rax into st1, rdx into st0, multiply st0 by 2^64 (adjusting exponent only really), add st0 and st1 and pop?

EDIT: hmm seems the numbers are signed, I guess you'll have to convert them to abs value somehow (not individually) or the simple method won't work Smile
Post 31 Jul 2017, 12:28
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6676
Location: Kraków, Poland
The 80-bit format used by FPU is actually quite simple and you can do such conversion without any FPU instructions. I quickly scribbled this version:

Code:
int128_to_float80:
; in: rdx:rax = singed 128-bit integer
; out: dx:rax = 80-bit float
        test    rdx,rdx
        jns     uint128_to_float80
        not     rdx     ;\
        not     rax     ; \  this is just a
        add     rax,1   ; /  neg rdx:rax
        adc     rdx,0   ;/
        call    uint128_to_float80
        or      dx,8000h
        retn
uint128_to_float80:
; in: rdx:rax = unsigned 128-bit integer
; out: dx:rax = 80-bit float
        bsr     r8,rdx
        jz      .low
        mov     cl,63
        sub     cl,r8l
        shld    rdx,rax,cl
        mov     rax,rdx
        lea     rdx,[16383+64+r8]
        retn
    .low:
        bsr     r8,rax
        jz      .zero
        mov     cl,63
        sub     cl,r8l
        shl     rax,cl
        lea     rdx,[16383+r8]
        retn
    .zero:
        xor     eax,eax
        xor     edx,edx
        retn

Since you need the result in ST0, you'd have to use it like this:

Code:
        call    int128_to_float80
        mov     qword [tmp],rax
        mov     word [tmp+8],dx
        fld     tword [tmp]

Post 31 Jul 2017, 15:25
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 889
You could shave off a few instructions with lzcnt instead of bsr (check the carry flag instead of zero though). Wink

(also if optimizing for size -- xor eax, eax/xor edx, edx can be replaced by xor eax, eax/cdq)
Post 31 Jul 2017, 15:56
View user's profile Send private message Reply with quote
petelomax



Joined: 19 Jan 2013
Posts: 4
Location: London

Tomasz Grysztar wrote:
I quickly scribbled this version:


Excellent stuff, works perfectly.

As a bonus, I don't feel quite so stupid for asking now!

Many thanks,
Pete
Post 31 Jul 2017, 19:49
View user's profile Send private message Visit poster's website 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.