summaryrefslogtreecommitdiff
path: root/mcon/U/d_castneg.U
blob: 77ce40a59ad5fcef56367345e5e0391c4bd4f0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
?RCS: $Id: d_castneg.U 167 2013-05-08 17:58:00Z rmanfredi $
?RCS:
?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
?RCS: 
?RCS: You may redistribute only under the terms of the Artistic License,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic License; a copy of which may be found at the root
?RCS: of the source tree for dist 4.0.
?RCS:
?RCS: $Log: d_castneg.U,v $
?RCS: Revision 3.0.1.2  1995/05/12  12:11:21  ram
?RCS: patch54: made sure cc and ccflags are conditional dependencies
?RCS: patch54: added improved test case for Interactive Unix
?RCS:
?RCS: Revision 3.0.1.1  1994/10/29  16:10:50  ram
?RCS: patch36: don't forget to tell user about compilation failures (ADO)
?RCS: patch36: declare signal handler correctly using 'signal_t' (ADO)
?RCS:
?RCS: Revision 3.0  1993/08/18  12:05:47  ram
?RCS: Baseline for dist 3.0 netwide release.
?RCS:
?X:
?X:	Can the compiler cast negative / odd floats to unsigned values.
?X:
?MAKE:d_castneg castflags: cat +cc +ccflags rm Setvar signal_t
?MAKE:	-pick add $@ %<
?S:d_castneg:
?S:	This variable conditionally defines CASTNEG, which indicates
?S:	wether the C compiler can cast negative float to unsigned.
?S:.
?S:castflags:
?S:	This variable contains a flag that precise difficulties the
?S:	compiler has casting odd floating values to unsigned long:
?S:		0 = ok
?S:		1 = couldn't cast < 0
?S:		2 = couldn't cast >= 0x80000000
?S:		4 = couldn't cast in argument expression list
?S:.
?C:CASTNEGFLOAT:
?C:	This symbol is defined if the C compiler can cast negative
?C:	numbers to unsigned longs, ints and shorts.
?C:.
?C:CASTFLAGS:
?C:	This symbol contains flags that say what difficulties the compiler
?C:	has casting odd floating values to unsigned long:
?C:		0 = ok
?C:		1 = couldn't cast < 0
?C:		2 = couldn't cast >= 0x80000000
?C:		4 = couldn't cast in argument expression list
?C:.
?H:#$d_castneg	CASTNEGFLOAT		/**/
?H:#define CASTFLAGS $castflags		/**/
?H:.
?F:!try
?LINT:set d_castneg
: check for ability to cast negative floats to unsigned
echo " "
echo 'Checking whether your C compiler can cast negative float to unsigned.' >&4
$cat >try.c <<EOCP
#include <sys/types.h>
#include <signal.h>
$signal_t blech() { exit(7); }
$signal_t blech_in_list() { exit(4); }
unsigned long dummy_long(p) unsigned long p; { return p; }
unsigned int dummy_int(p) unsigned int p; { return p; }
unsigned short dummy_short(p) unsigned short p; { return p; }
int main()
{
	double f = -123.;
	unsigned long along;
	unsigned int aint;
	unsigned short ashort;
	int result = 0;

	signal(SIGFPE, blech);
	along = (unsigned long)f;
	aint = (unsigned int)f;
	ashort = (unsigned short)f;
	if (along != (unsigned long)-123)
		result |= 1;
	if (aint != (unsigned int)-123)
		result |= 1;
	if (ashort != (unsigned short)-123)
		result |= 1;
	f = (double)0x40000000;
	f = f + f;
	along = 0;
	along = (unsigned long)f;
	if (along != 0x80000000)
		result |= 2;
	f -= 1.;
	along = 0;
	along = (unsigned long)f;
	if (along != 0x7fffffff)
		result |= 1;
	f += 2.;
	along = 0;
	along = (unsigned long)f;
	if (along != 0x80000001)
		result |= 2;
	if (result)
		exit(result);
?X:
?X: The following is a test for Interactive Unix Version 4.1, which
?X: has an 'improved' compiler which can correctly cast negative
?X: floats in expression lists, but apparently not in argument lists.
?X:	Contributed by Winfried Koenig <win@incom.rhein-main.de>
?X:
	signal(SIGFPE, blech_in_list);
	f = 123.;
	along = dummy_long((unsigned long)f);
	aint = dummy_int((unsigned int)f);
	ashort = dummy_short((unsigned short)f);
	if (along != (unsigned long)123)
		result |= 4;
	if (aint != (unsigned int)123)
		result |= 4;
	if (ashort != (unsigned short)123)
		result |= 4;
	exit(result);

}
EOCP
if $cc $ccflags -o try try.c >/dev/null 2>&1; then
	./try
	castflags=$?
else
	echo "(I can't seem to compile the test program--assuming it can't)"
	castflags=7
fi
case "$castflags" in
0)	val="$define"
	echo "Yup, it can."
	;;
*)	val="$undef"
	echo "Nope, it can't."
	;;
esac
set d_castneg
eval $setvar
$rm -f try.*