~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/src/play/unix/fputest.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * fputest.c -- $Id: fputest.c,v 1.1 2003/03/08 15:26:48 travo Exp $
 
3
 * test for SIGFPE delivery
 
4
 *
 
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#ifndef NO_FPUSET
 
9
#define FROM_FPUTEST
 
10
#include "fpuset.c"
 
11
#else
 
12
extern void u_fpu_setup(int when);
 
13
void u_fpu_setup(int when) {}
 
14
#endif
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <signal.h>
 
19
 
 
20
#include <setjmp.h>
 
21
static jmp_buf u_jmp_target;
 
22
 
 
23
extern void u_sigfpe(int sig);
 
24
extern double reciprocal(double x);
 
25
extern double quotient(double x, double y);
 
26
extern double powpow(double x, int n);
 
27
extern int decrement(int *ip);
 
28
static int always_true = 0;
 
29
 
 
30
int
 
31
main(int argc, char *argv[])
 
32
{
 
33
  int i = 5;
 
34
  double zero = (double)(argc>1000);
 
35
  double huge = (argc>1000)? 2.0 : 1.e20;
 
36
  always_true = !(argc>1000);
 
37
 
 
38
  u_fpu_setup(-1);
 
39
  if (setjmp(u_jmp_target)) u_fpu_setup(0);
 
40
 
 
41
  /* signal *ought* to be enough to get SIGFPE delivered
 
42
   * -- but it never is -- see README.fpu */
 
43
  signal(SIGFPE, &u_sigfpe);
 
44
 
 
45
  /* need to make sure that loop index i actually decrements
 
46
   * despite interrupt */
 
47
  while (decrement(&i)) {
 
48
    printf("SIGFPE handling failed on pass %d, 1./0. = %g\n", 5-i,
 
49
           reciprocal(zero));
 
50
    if (i) break;
 
51
  }
 
52
  if (!i) {
 
53
    i = 5;
 
54
    if (setjmp(u_jmp_target)) u_fpu_setup(0);
 
55
    while (decrement(&i)) {
 
56
      printf("SIGFPE handling failed on pass %d, 0./0. = %g\n", 5-i,
 
57
             quotient(zero, zero));
 
58
      if (i) break;
 
59
    }
 
60
    if (!i) {
 
61
      i = 5;
 
62
      if (setjmp(u_jmp_target)) u_fpu_setup(0);
 
63
      while (decrement(&i)) {
 
64
        printf("SIGFPE handling failed on pass %d, 10.^20480 = %g\n", 5-i,
 
65
               powpow(huge, 10));
 
66
        if (i) break;
 
67
      }
 
68
      if (!i) {
 
69
        if (setjmp(u_jmp_target)) {
 
70
          puts("SIGFPE improperly generated on underflow");
 
71
          i = 11;
 
72
        } else {
 
73
          double x = powpow(1./huge, 10);
 
74
          if (x != 0.0)
 
75
            printf("SIGFPE handling works, but 10.^-20480 = %g\n", x);
 
76
          else
 
77
            puts("SIGFPE handling works properly");
 
78
        }
 
79
      }
 
80
    }
 
81
  }
 
82
  exit(i? 2 : 0);
 
83
}
 
84
 
 
85
void
 
86
u_sigfpe(int sig)
 
87
{
 
88
  if (sig==SIGFPE) {
 
89
    u_fpu_setup(1);
 
90
    signal(SIGFPE, &u_sigfpe);
 
91
    longjmp(u_jmp_target, 1);
 
92
  } else {
 
93
    puts("u_sigfpe called, but with bad parameter");
 
94
  }
 
95
  exit(1);
 
96
}
 
97
 
 
98
int
 
99
decrement(int *ip)
 
100
{
 
101
  int i = *ip;
 
102
  if (always_true) i--;
 
103
  else i -= 2;
 
104
  *ip = i;
 
105
  return i>0;
 
106
}
 
107
 
 
108
double
 
109
reciprocal(double x)
 
110
{
 
111
  return 1./x;
 
112
}
 
113
 
 
114
double
 
115
quotient(double x, double y)
 
116
{
 
117
  return x/y;
 
118
}
 
119
 
 
120
double
 
121
powpow(double x, int n)
 
122
{
 
123
  double y = x;
 
124
  while (n--) y *= y;
 
125
  return y;
 
126
}