ASM - 没有FPU数学运算 [英] ASM - math operations without FPU

查看:140
本文介绍了ASM - 没有FPU数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个程序,该程序帮我做任何操作上浮点数(也可以是另外),但不使用浮点单元(FPU)。

I'm looking for a routine, which help me to do any of operation on floating point numbers (it can be addition), but without using Floating Point Unit (FPU).

我必须做出一个方案,其中比较做操作,而FPU和FPU的时间。第二部分,使用FPU做,但我找不到任何好的程序。

I have to make a program, which compares time of doing operation without FPU and with FPU. Second part, with using FPU is done, but I can't find any good routine.

推荐答案

不知道为什么人们都惊呆了有关的复杂性。
加法,减法和乘法并不十分困难。
您将需要将数字拆分为标志,尾数和指数再使用整数运算来计算结果,并最终把它放回一个浮动。我正好有浮法乘法样本code(64位气体语法):

Not sure why people are all shocked about the complexity. Addition, subtraction and multiplication are not terribly difficult. You will need to split the numbers into sign, mantissa and exponent then use integer math to calculate the result and finally put it back into a float. I happen to have sample code for float multiplication (64 bit GAS syntax):

.globl main
main:
    subq $8, %rsp
    leaq format1(%rip), %rdi
    leaq (%rsp), %rsi
    leaq 4(%rsp), %rdx
    xor %eax, %eax
    call scanf

    movl (%rsp), %eax           # x
    movl 4(%rsp), %ecx          # y

    movl %eax, %edx             # edx = x
    xorl %ecx, %eax             # sgn(x*y) = sgn(x) ^ sgn(y)
    andl $0x80000000, %eax      # eax has correct sign bit now
    movl %ecx, %esi             # esi = y
    shrl $23, %esi
    andl $0xff, %esi            # esi = exp(y)
    movl %edx, %edi             # edi = x
    shrl $23, %edi
    andl $0xff, %edi            # edi = exp(x)
    addl %esi, %edi             # exp(x*y) = exp(x) + exp(y)
    subl $0x7f+23, %edi         # subtract bias and mantissa bits

    andl $0x007fffff, %edx      # edx = man(x)
    andl $0x007fffff, %ecx      # ecx = man(y)
    orl $0x00800000, %ecx       # put implicit leading 1 bit
    orl $0x00800000, %edx       # to both mantissas
    imulq %rcx, %rdx            # man(x*y) = man(x) * man(y)
    bsrq %rdx, %rcx             # find first set bit
    subl $23, %ecx              # leave 23 bits
    shrq %cl, %rdx              # shift the rest out
    andl $0x007fffff, %edx      # chop off implicit leading 1 bit
    addl %ecx, %edi             # adjust the exponent by the bits shifted out

    shll $23, %edi              # shift exponent into position
    orl %edi, %eax              # put exponent into result
    orl %edx, %eax              # put mantissa into result

    movss (%rsp), %xmm0
    movss 4(%rsp), %xmm1
    movd %eax, %xmm2
    cvtss2sd %xmm0, %xmm0
    cvtss2sd %xmm1, %xmm1
    cvtss2sd %xmm2, %xmm2
    movl $3, %eax
    leaq format2(%rip), %rdi
    call printf

    addq $8, %rsp
    ret
.data
    format1: .asciz "%f %f"
    format2: .asciz "%f * %f = %f\n"

特殊情况为零,男,INF,溢出,这样,就当作锻炼; Tibial读者:)

Special cases for zero, nan, inf, overflow and such are left as an excercise to the reader :)

这篇关于ASM - 没有FPU数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆