9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] NaN(2)
@ 2007-02-20 23:55 Joel Salomon
  2007-02-21  0:00 ` Joel C. Salomon
  2007-02-21  5:50 ` Russ Cox
  0 siblings, 2 replies; 9+ messages in thread
From: Joel Salomon @ 2007-02-20 23:55 UTC (permalink / raw)
  To: 9fans

My system doesn’t seem to like it when I call NaN(2):

cpu% cat tnan.c
#include <u.h>
#include <libc.h>

void
main(int, char**)
{
	double d = NaN();
	print("d = %f\n", d);

	exits(0);
}
cpu% 8c -FVw tnan.c
cpu% 8l tnan.8
cpu% 8.out
8.out 55798: suicide: sys: fp: invalid operation fppc=0x10a4 status=0x8081 pc=0x00001028
cpu% acid 55798
/proc/55798/text:386 plan 9 executable

/sys/lib/acid/port
/sys/lib/acid/386
acid: stk()
main()+0x8 /usr/chesky/src/hak/lex/tnan.c:7
_main+0x31 /sys/src/libc/386/main9.s:16
acid: asm(main)
main 0x00001020	SUBL	$0x1c,SP
main+0x3 0x00001023	CALL	NaN(SB)
main+0x8 0x00001028	FMOVDP	F0,d+0x14(SP)
…

Why is this happening?

Also, what is Intel’s name for the FMOVDP instruction?

--Joel



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-20 23:55 [9fans] NaN(2) Joel Salomon
@ 2007-02-21  0:00 ` Joel C. Salomon
  2007-02-21  0:20   ` Joel Salomon
  2007-02-21  5:50 ` Russ Cox
  1 sibling, 1 reply; 9+ messages in thread
From: Joel C. Salomon @ 2007-02-21  0:00 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2/20/07, Joel Salomon <JoelCSalomon@gmail.com> wrote:
> My system doesn't seem to like it when I call NaN(2):

A simpler test case:

#include <u.h>
#include <libc.h>

void
main(int, char**)
{
	NaN();	// ☹

	exits(0);
}

--Joel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  0:00 ` Joel C. Salomon
@ 2007-02-21  0:20   ` Joel Salomon
  2007-02-21  1:00     ` erik quanstrom
  0 siblings, 1 reply; 9+ messages in thread
From: Joel Salomon @ 2007-02-21  0:20 UTC (permalink / raw)
  To: 9fans

Playing with acid a bit:

cpu% cat tnan.c
#include <u.h>
#include <libc.h>

void
main(int, char**)
{
	NaN();	// ☹

	exits(0);
}
cpu% 8c -FTVw tnan.c
cpu% 8l -o tnan tnan.8
cpu% tnan
tnan 55986: suicide: sys: fp: invalid operation fppc=0x108d status=0x8081 pc=0x00001028
cpu% acid -l acme 55986
/proc/55986/text:386 plan 9 executable

/sys/lib/acid/port
/sys/lib/acid/acme
/sys/lib/acid/386
acid: stk()
At pc:0x00001028:main+0x8 /usr/chesky/src/hak/lex/tnan.c:7
main() 
	called from _main+0x31 /sys/src/libc/386/main9.s:16
acid: regs()
PC	0x00001028 main+0x8  /usr/chesky/src/hak/lex/tnan.c:7
SP	0xdfffef5c ECODE 0xf0100661 EFLAG 0x00010292
CS	0x00000023 DS	 0x0000001b SS	0x0000001b
GS	0xf010001b FS	 0x0000001b ES	0x0000001b
TRAP	0x00000010 math coprocessor error
AX	0xdfffefb4 BX	0x00000001 CX	0x00000000 DX	0x0001d260
DI	0xdfffea17 SI	0x0001f425 BP	0x00031e00
acid: asm(main)
main 0x00001020	SUBL	$0x8,SP
main+0x3 0x00001023	CALL	NaN(SB)
main+0x8 0x00001028	FMOVDP	F0,F0
main+0xa 0x0000102a	MOVL	$0x0,0x0(SP)
main+0x11 0x00001031	CALL	exits(SB)
main+0x16 0x00001036	ADDL	$0x8,SP
main+0x19 0x00001039	RET
_main 0x0000103a	SUBL	$0x48,SP
acid: fpr()
F0	0
F1	0
F2	0
F3	0
F4	0
F5	0
F6	3.431198e+09
F7	1000
control	0x027f
status	0x0001
tag	0xffff
ip offset	0x0000108d
cs selector	0x0023
opcode	0xea17
data operand offset	0xef50
operand selector	0x001b
acid: asm(NaN)
NaN 0x0000107a	SUBL	$0xc,SP
NaN+0x3 0x0000107d	MOVL	$0x7ff00000,0x8(SP)
NaN+0xb 0x00001085	MOVL	$0x1,a+0x4(SP)
NaN+0x13 0x0000108d	FMOVD	a+0x4(SP),F0
NaN+0x17 0x00001091	ADDL	$0xc,SP
NaN+0x1a 0x00001094	RET
isNaN 0x00001095	SUBL	$0x1c,SP
acid: 

Considering that fppc points into NaN (0x108d), is the suicide
happening within NaN?  Anything I can do to pinpoint the bug better?

--Joel



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  0:20   ` Joel Salomon
@ 2007-02-21  1:00     ` erik quanstrom
  2007-02-21  2:26       ` Joel Salomon
  0 siblings, 1 reply; 9+ messages in thread
From: erik quanstrom @ 2007-02-21  1:00 UTC (permalink / raw)
  To: 9fans

fwiw, i think the gas construction syntax is this:

dd 44 24 04	fldl	0x04(%esp,1)

this might be an alignment problem.  but that's a wild guess.
you could rewrite NaN in assembler and adjust the stack frame
by hand.

you need a processor manual.

- erik


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  1:00     ` erik quanstrom
@ 2007-02-21  2:26       ` Joel Salomon
  2007-02-21  4:18         ` erik quanstrom
  0 siblings, 1 reply; 9+ messages in thread
From: Joel Salomon @ 2007-02-21  2:26 UTC (permalink / raw)
  To: 9fans

> this might be an alignment problem.  but that's a wild guess.

I think not:
acid: SP
0x00000044

> you could rewrite NaN in assembler and adjust the stack frame
> by hand.

I could.  It’s just hard to believe I’m the first to run across this
problem with NaN(2).

> you need a processor manual.

Have a few at home.  Trouble is, I’m at school now.

> fwiw, i think the gas construction syntax is this:
> 
> dd 44 24 04	fldl	0x04(%esp,1)

FLD, got it.  I’m trying to get acid to dump the hex bytes of the
FMOVD instruction so I can confirm that these are the same, but I’m not finding it in the manual.

--Joel



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  2:26       ` Joel Salomon
@ 2007-02-21  4:18         ` erik quanstrom
  2007-02-21  4:35           ` Joel C. Salomon
  2007-02-21  5:10           ` Joel C. Salomon
  0 siblings, 2 replies; 9+ messages in thread
From: erik quanstrom @ 2007-02-21  4:18 UTC (permalink / raw)
  To: 9fans

after a bit of actual thought, i think that NaN
is supposed to generate a note.  if you do catch
the note, the program should work fine.

e.g.

#include <u.h>
#include <libc.h>

void
note(void *, char *s)
{
	if(strncmp(s, "sys: fp: ", 8) == 0)
		noted(NCONT);
	noted(NDFLT);
}

void
main(void)
{
	double d;

	notify(note);
	d = NaN();
	print("%g\n", d);
}


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  4:18         ` erik quanstrom
@ 2007-02-21  4:35           ` Joel C. Salomon
  2007-02-21  5:10           ` Joel C. Salomon
  1 sibling, 0 replies; 9+ messages in thread
From: Joel C. Salomon @ 2007-02-21  4:35 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2/20/07, erik quanstrom <quanstro@coraid.com> wrote:
> after a bit of actual thought, i think that NaN
> is supposed to generate a note.  if you do catch
> the note, the program should work fine.

This is distinct from sNaN/qNaN, right?

--Joel


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-21  4:18         ` erik quanstrom
  2007-02-21  4:35           ` Joel C. Salomon
@ 2007-02-21  5:10           ` Joel C. Salomon
  1 sibling, 0 replies; 9+ messages in thread
From: Joel C. Salomon @ 2007-02-21  5:10 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2/20/07, erik quanstrom <quanstro@coraid.com> wrote:
> after a bit of actual thought, i think that NaN
> is supposed to generate a note.  if you do catch
> the note, the program should work fine.

That was it; thanks.

--Joel


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [9fans] NaN(2)
  2007-02-20 23:55 [9fans] NaN(2) Joel Salomon
  2007-02-21  0:00 ` Joel C. Salomon
@ 2007-02-21  5:50 ` Russ Cox
  1 sibling, 0 replies; 9+ messages in thread
From: Russ Cox @ 2007-02-21  5:50 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 2/20/07, Joel Salomon <JoelCSalomon@gmail.com> wrote:
> My system doesn't seem to like it when I call NaN(2):

That is the default behavior.  See getfcr(2).  In particular,
if you add

	setfcr(getfcr()&~FPINVAL);

to your program then you can carry NaNs around willy-nilly
without causing exceptions.

On 2/20/07, Joel Salomon <JoelCSalomon@gmail.com> wrote:
> > this might be an alignment problem.  but that's a wild guess.
>
> I think not:
> acid: SP
> 0x00000044

This tells you that SP's "address" is 0x44.  (The registers appear to
have memory addresses starting at 0 in acid.)  If you want to find
out what SP is, you have to dereference it, as in *SP.

On 2/20/07, erik quanstrom <quanstro@coraid.com> wrote:
> after a bit of actual thought, i think that NaN
> is supposed to generate a note.  if you do catch
> the note, the program should work fine.

This is true too, though setfcr will avoid the notes entirely.

Russ


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-02-21  5:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-20 23:55 [9fans] NaN(2) Joel Salomon
2007-02-21  0:00 ` Joel C. Salomon
2007-02-21  0:20   ` Joel Salomon
2007-02-21  1:00     ` erik quanstrom
2007-02-21  2:26       ` Joel Salomon
2007-02-21  4:18         ` erik quanstrom
2007-02-21  4:35           ` Joel C. Salomon
2007-02-21  5:10           ` Joel C. Salomon
2007-02-21  5:50 ` Russ Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).