Linux 更改FPU舍入模式
我想知道,我应该使用什么值来更改FPU舍入模式Linux 更改FPU舍入模式,linux,assembly,x86,att,fpu,Linux,Assembly,X86,Att,Fpu,我想知道,我应该使用什么值来更改FPU舍入模式 .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 cm
.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
我发现了这样的东西:
down:
.byte 0x7f, 0x07
up:
.byte 0x7f, 0x0b
但我不知道为什么有人用它。我知道我应该更改8位和9位,如下所示:
00-四舍五入至最近
01-向下取整(朝向负无穷大)
10-向上取整(朝向正无穷大)
11-向零舍入舍入类型由FPU控制字中的两位决定。您可以在此处获取有关FPU的信息:。只更改这两个位而不更改其他位有点棘手。看看我的例子。我试图尽可能接近您的代码:
.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
我意识到它应该是这样的:最近的。字节0x7f,0x00b,向下:。字节0x7f,0x07,向上。字节0x7f,0x70b,零:。字节0x7f,0x77b??