首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改变FPU舍入方式

改变FPU舍入方式
EN

Stack Overflow用户
提问于 2014-04-30 15:23:37
回答 1查看 1.8K关注 0票数 1

我想知道,我应该使用哪些值来改变FPU舍入模式。

代码语言:javascript
复制
.data
nearest:
    ??
down:
    ??
up:
    ??
zero:
    ??
.text
.global round
    pushl   %ebp
    movl    %esp, %ebp
    movl 8(%ebp), %ecx

    cmpl $1, %ecx
je
    fldcw nearest

    cmpl $2, %ecx
je
    fldcw down

    cmpl $3, %ecx
je
    fldcw up

    cmpl $4, %ecx
je
    fldcw zero
leave
ret

我发现了这样的东西:

代码语言:javascript
复制
down:
    .byte 0x7f, 0x07
up:
    .byte 0x7f, 0x0b

但我不知道为什么有人用它。我知道我应该改变8和9位,像这样: 00圆到最近的01圈向下(向负无穷远) 10圈向上(向正无穷大) 11圈向零。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-30 18:26:05

舍入的类型由FPU的控制字中的两个位决定。您可以在这里获得有关FPU的信息:http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm。只改变这两位而不改变其他部分是有点棘手的。看看我的例子。我试图尽可能接近您的代码:

代码语言:javascript
复制
.data
    num:        .double 6.5     # play with the number!

    old:        .word 0
    nearest:    .word 0x0000
    down:       .word 0x0400
    up:         .word 0x0800
    zero:       .word 0x0C00
    result:     .double 0
    fmt1:       .asciz "nearest: %f -> %f\n"
    fmt2:       .asciz "down:    %f -> %f\n"
    fmt3:       .asciz "up:      %f -> %f\n"
    fmt4:       .asciz "zero:    %f -> %f\n"

.text
.globl main
main:

    fstcw old               # store old control word

    movw old, %ax
    andb $0b11110011, %ah   # clear RC field
    orw %ax, nearest
    orw %ax, down
    orw %ax, up
    orw %ax, zero

    fldcw nearest           # banker's rounding
    call round
    mov $fmt1, %esi
    call out

    fldcw down              # down toward -infinity
    call round
    mov $fmt2, %esi
    call out

    fldcw up                # up toward +infinity
    call round
    mov $fmt3, %esi
    call out

    fldcw zero              # truncating
    call round
    mov $fmt4, %esi
    call out

    fldcw old               # restore old control word

    xor %eax, %eax          # exit(0)
    ret                     # GCC only needs RET

round:
    fldl num
    frndint
    fstpl result
    ret

out:
    push result+4
    push result
    push num+4
    push num
    push %esi
    call printf             # "%f" needs a double
    add $20, %esp
    ret
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23392070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档