Commit | Line | Data |
---|---|---|
959f3c4c JH |
1 | ?RCS: $Id: d_castneg.U,v 3.0.1.2 1995/05/12 12:11:21 ram Exp $ |
2 | ?RCS: | |
3 | ?RCS: Copyright (c) 1991-1993, Raphael Manfredi | |
4 | ?RCS: | |
5 | ?RCS: You may redistribute only under the terms of the Artistic Licence, | |
6 | ?RCS: as specified in the README file that comes with the distribution. | |
7 | ?RCS: You may reuse parts of this distribution only within the terms of | |
8 | ?RCS: that same Artistic Licence; a copy of which may be found at the root | |
9 | ?RCS: of the source tree for dist 3.0. | |
10 | ?RCS: | |
11 | ?RCS: $Log: d_castneg.U,v $ | |
12 | ?RCS: Revision 3.0.1.2 1995/05/12 12:11:21 ram | |
13 | ?RCS: patch54: made sure cc and ccflags are conditional dependencies | |
14 | ?RCS: patch54: added improved test case for Interactive Unix | |
15 | ?RCS: | |
16 | ?RCS: Revision 3.0.1.1 1994/10/29 16:10:50 ram | |
17 | ?RCS: patch36: don't forget to tell user about compilation failures (ADO) | |
18 | ?RCS: patch36: declare signal handler correctly using 'signal_t' (ADO) | |
19 | ?RCS: | |
20 | ?RCS: Revision 3.0 1993/08/18 12:05:47 ram | |
21 | ?RCS: Baseline for dist 3.0 netwide release. | |
22 | ?RCS: | |
23 | ?X: | |
24 | ?X: Can the compiler cast negative / odd floats to unsigned values. | |
25 | ?X: | |
3b148aa9 | 26 | ?MAKE:d_castneg castflags: cat Compile rm Setvar signal_t run i_stdlib |
959f3c4c JH |
27 | ?MAKE: -pick add $@ %< |
28 | ?S:d_castneg: | |
29 | ?S: This variable conditionally defines CASTNEG, which indicates | |
30 | ?S: wether the C compiler can cast negative float to unsigned. | |
31 | ?S:. | |
32 | ?S:castflags: | |
33 | ?S: This variable contains a flag that precise difficulties the | |
34 | ?S: compiler has casting odd floating values to unsigned long: | |
35 | ?S: 0 = ok | |
36 | ?S: 1 = couldn't cast < 0 | |
37 | ?S: 2 = couldn't cast >= 0x80000000 | |
38 | ?S: 4 = couldn't cast in argument expression list | |
39 | ?S:. | |
40 | ?C:CASTNEGFLOAT: | |
41 | ?C: This symbol is defined if the C compiler can cast negative | |
42 | ?C: numbers to unsigned longs, ints and shorts. | |
43 | ?C:. | |
44 | ?C:CASTFLAGS: | |
45 | ?C: This symbol contains flags that say what difficulties the compiler | |
46 | ?C: has casting odd floating values to unsigned long: | |
47 | ?C: 0 = ok | |
48 | ?C: 1 = couldn't cast < 0 | |
49 | ?C: 2 = couldn't cast >= 0x80000000 | |
50 | ?C: 4 = couldn't cast in argument expression list | |
51 | ?C:. | |
52 | ?H:#$d_castneg CASTNEGFLOAT /**/ | |
53 | ?H:#define CASTFLAGS $castflags /**/ | |
54 | ?H:. | |
55 | ?LINT:set d_castneg | |
56 | : check for ability to cast negative floats to unsigned | |
57 | echo " " | |
58 | echo 'Checking whether your C compiler can cast negative float to unsigned.' >&4 | |
59 | $cat >try.c <<EOCP | |
60 | #include <stdio.h> | |
3b148aa9 JH |
61 | #$i_stdlib I_STDLIB |
62 | #ifdef I_STDLIB | |
63 | #include <stdlib.h> | |
64 | #endif | |
959f3c4c JH |
65 | #include <sys/types.h> |
66 | #include <signal.h> | |
67 | $signal_t blech(s) int s; { exit(7); } | |
68 | $signal_t blech_in_list(s) int s; { exit(4); } | |
69 | unsigned long dummy_long(p) unsigned long p; { return p; } | |
70 | unsigned int dummy_int(p) unsigned int p; { return p; } | |
71 | unsigned short dummy_short(p) unsigned short p; { return p; } | |
72 | int main() | |
73 | { | |
74 | double f; | |
75 | unsigned long along; | |
76 | unsigned int aint; | |
77 | unsigned short ashort; | |
78 | int result = 0; | |
79 | char str[16]; | |
80 | ||
81 | /* Frustrate gcc-2.7.2's optimizer which failed this test with | |
82 | a direct f = -123. assignment. gcc-2.8.0 reportedly | |
83 | optimized the whole file away | |
84 | */ | |
85 | /* Store the number in a writable string for gcc to pass to | |
86 | sscanf under HP/UX. | |
87 | */ | |
88 | sprintf(str, "-123"); | |
89 | sscanf(str, "%lf", &f); /* f = -123.; */ | |
90 | ||
91 | signal(SIGFPE, blech); | |
92 | along = (unsigned long)f; | |
93 | aint = (unsigned int)f; | |
94 | ashort = (unsigned short)f; | |
95 | if (along != (unsigned long)-123) | |
96 | result |= 1; | |
97 | if (aint != (unsigned int)-123) | |
98 | result |= 1; | |
99 | if (ashort != (unsigned short)-123) | |
100 | result |= 1; | |
101 | sprintf(str, "1073741824."); | |
102 | sscanf(str, "%lf", &f); /* f = (double)0x40000000; */ | |
103 | f = f + f; | |
104 | along = 0; | |
105 | along = (unsigned long)f; | |
106 | if (along != 0x80000000) | |
107 | result |= 2; | |
108 | f -= 1.; | |
109 | along = 0; | |
110 | along = (unsigned long)f; | |
111 | if (along != 0x7fffffff) | |
112 | result |= 1; | |
113 | f += 2.; | |
114 | along = 0; | |
115 | along = (unsigned long)f; | |
116 | if (along != 0x80000001) | |
117 | result |= 2; | |
118 | if (result) | |
119 | exit(result); | |
120 | ?X: | |
121 | ?X: The following is a test for Interactive Unix Version 4.1, which | |
122 | ?X: has an 'improved' compiler which can correctly cast negative | |
123 | ?X: floats in expression lists, but apparently not in argument lists. | |
124 | ?X: Contributed by Winfried Koenig <win@incom.rhein-main.de> | |
125 | ?X: | |
126 | signal(SIGFPE, blech_in_list); | |
127 | sprintf(str, "123."); | |
128 | sscanf(str, "%lf", &f); /* f = 123.; */ | |
129 | along = dummy_long((unsigned long)f); | |
130 | aint = dummy_int((unsigned int)f); | |
131 | ashort = dummy_short((unsigned short)f); | |
132 | if (along != (unsigned long)123) | |
133 | result |= 4; | |
134 | if (aint != (unsigned int)123) | |
135 | result |= 4; | |
136 | if (ashort != (unsigned short)123) | |
137 | result |= 4; | |
138 | exit(result); | |
139 | ||
140 | } | |
141 | EOCP | |
142 | set try | |
143 | if eval $compile_ok; then | |
0f00356b | 144 | $run ./try |
959f3c4c JH |
145 | castflags=$? |
146 | else | |
147 | echo "(I can't seem to compile the test program--assuming it can't)" | |
148 | castflags=7 | |
149 | fi | |
150 | case "$castflags" in | |
151 | 0) val="$define" | |
152 | echo "Yup, it can." | |
153 | ;; | |
154 | *) val="$undef" | |
155 | echo "Nope, it can't." | |
156 | ;; | |
157 | esac | |
158 | set d_castneg | |
159 | eval $setvar | |
160 | $rm -f try.* | |
161 |