public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* 2.4, arch-dependent floating point exception and trap handling
@ 2002-07-30 13:06 Daniel Barlow
  2002-07-31 12:47 ` Anton Blanchard
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Barlow @ 2002-07-30 13:06 UTC (permalink / raw)
  To: linux-kernel


I'm seeing discrepancies between the way that floating point
exceptions are handled on different architectures.  Briefly, I have a
program that uses the glibc function feenableexcept() to enable SIGFPE
on divide-by-zero errors, then installs a SIGFPE handler that uses
sigsetjmp/siglongjmp, then divides 1.0 by 0.0 twice.

What I expect to happen is for my signal handler to be called twice.
What actually happens varies:

On Alpha: 2.4.19-pre1-ac2, gcc version 2.95.4 20011002, PCA56 (SX164)

:; cc -o foo foo.c -lm -mieee
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type
:; ./foo
exceptions enabled: 0
exceptions enabled: 40000
hello
in handler
abort
in handler
abort
terminating

- exactly as expected

On x86: 2.4.19-rc1, gcc version 2.95.4 20011002 , Pentium III (Coppermine)

my signal handler is called the first time, and then the second
attempt gets "inf".  I've run this under gdb as well; the second SIGFPE is 
not received by the process

:; gcc -o foo foo.c -lm
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type
:; ./foo
exceptions enabled: 0
exceptions enabled: 4
hello
in handler
abort
a/b=inf
terminating


On PPC: 2.4.19-pre8, gcc version 2.95.4 20011002,  266MHz 740/750 rev 2.2
(tangerine iMac) I have x86-like behaviour

:; cc -o foo foo.c -lm
foo.c: In function `main':
foo.c:23: warning: assignment from incompatible pointer type
:; ./foo
exceptions enabled: 0
exceptions enabled: 4000000
hello
in handler
abort
a/b=inf
terminating

All of these are uniprocessor machines running uniprocessor kernels.
The behaviour I'd like _most_ is for the pre-signal environment to be
restored, but at least to get the same behaviour everywhere would be a
good thing.

I append the test program I've been using so you can tell me whether
my whole approach is misguided

---cut here---
#include <signal.h>
#include <stdio.h>
#include <sys/signal.h>
#include <ucontext.h>
#include <fenv.h>
#include <setjmp.h>

sigjmp_buf env;
double a=1.0,b=0.0;

int handler(int signal, struct siginfo *info, struct ucontext *context) {
    printf("in handler\n");
    siglongjmp(env,signal);
}

main() {
    struct sigaction sa;

    a=1.0;
    printf("exceptions enabled: %x\n", fegetexcept());
    feenableexcept(FE_DIVBYZERO);   
    printf("exceptions enabled: %x\n", fegetexcept());
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_SIGINFO | SA_RESTART;
    sigaction(SIGFPE, &sa, NULL); 
    printf("hello\n");
    /* first time */
    if(sigsetjmp(env,1)) printf("abort\n");
    else                 printf("a/b=%f\n", a/b);

    /* now try again, see if things were restored */
    if(sigsetjmp(env,1)) printf("abort\n");
    else                 printf("a/b=%f\n", a/b);
    printf("terminating\n");
}

--- cut here ---

If this message should have been sent elsewhere (e.g. to port-specific
lists or maintainers) please feel free to tell me where and/or to
forward it.

Thanks!


-dan

-- 

  http://ww.telent.net/cliki/ - Link farm for free CL-on-Unix resources 

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

* Re: 2.4, arch-dependent floating point exception and trap handling
  2002-07-30 13:06 2.4, arch-dependent floating point exception and trap handling Daniel Barlow
@ 2002-07-31 12:47 ` Anton Blanchard
  0 siblings, 0 replies; 2+ messages in thread
From: Anton Blanchard @ 2002-07-31 12:47 UTC (permalink / raw)
  To: Daniel Barlow; +Cc: linux-kernel


Hi,

> I'm seeing discrepancies between the way that floating point
> exceptions are handled on different architectures.  Briefly, I have a
> program that uses the glibc function feenableexcept() to enable SIGFPE
> on divide-by-zero errors, then installs a SIGFPE handler that uses
> sigsetjmp/siglongjmp, then divides 1.0 by 0.0 twice.
> 
> What I expect to happen is for my signal handler to be called twice.
> What actually happens varies:

...

> my signal handler is called the first time, and then the second
> attempt gets "inf".  I've run this under gdb as well; the second SIGFPE is 
> not received by the process

The x86 like behaviour happens on ppc64 too:

exceptions enabled: 0
exceptions enabled: 4000000
hello
in handler
abort
a/b=inf
terminating

There is a reason for this. A signal handler might want to do floating
point so exceptions are disabled upon entering a signal handler.

Now the question that needs to be answered is whether sigsetjmp should
save the floating point exception register. I asked our resident
standards expert Chris Yeoh and the answer is no.

So the x86, ppc32 and ppc64 behaviour looks correct.

Anton

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

end of thread, other threads:[~2002-07-31 12:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-30 13:06 2.4, arch-dependent floating point exception and trap handling Daniel Barlow
2002-07-31 12:47 ` Anton Blanchard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox