Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
 
32
/*
2 7u83 33
    Copyright (c) 1993 Open Software Foundation, Inc.
34
 
35
 
36
    All Rights Reserved
37
 
38
 
39
    Permission to use, copy, modify, and distribute this software
40
    and its documentation for any purpose and without fee is hereby
41
    granted, provided that the above copyright notice appears in all
42
    copies and that both the copyright notice and this permission
43
    notice appear in supporting documentation.
44
 
45
 
46
    OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
47
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
48
    PARTICULAR PURPOSE.
49
 
50
 
51
    IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
52
    CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
53
    LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
54
    NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
55
    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56
*/
57
 
58
/*
59
    		 Crown Copyright (c) 1997
7 7u83 60
 
2 7u83 61
    This TenDRA(r) Computer Program is subject to Copyright
62
    owned by the United Kingdom Secretary of State for Defence
63
    acting through the Defence Evaluation and Research Agency
64
    (DERA).  It is made available to Recipients with a
65
    royalty-free licence for its use, reproduction, transfer
66
    to other parties and amendment for any purpose not excluding
67
    product development provided that any such use et cetera
68
    shall be deemed to be acceptance of the following conditions:-
7 7u83 69
 
2 7u83 70
        (1) Its Recipients shall ensure that this Notice is
71
        reproduced upon any copies or amended versions of it;
7 7u83 72
 
2 7u83 73
        (2) Any amended version of it shall be clearly marked to
74
        show both the nature of and the organisation responsible
75
        for the relevant amendment or amendments;
7 7u83 76
 
2 7u83 77
        (3) Its onward transfer from a recipient to another
78
        party shall be deemed to be that party's acceptance of
79
        these conditions;
7 7u83 80
 
2 7u83 81
        (4) DERA gives no warranty or assurance as to its
82
        quality or suitability for any purpose and DERA accepts
83
        no liability whatsoever in relation to any use to which
84
        it may be put.
85
*/
86
 
87
 
88
/* from float.h */
89
#define FP_RND_RZ		0
90
#define FP_RND_RN		1
91
#define FP_RND_RP		2
92
#define FP_RND_RM		3
93
 
7 7u83 94
typedef unsigned short fprnd_t;
95
extern fprnd_t fp_read_rnd(void);
96
extern fprnd_t fp_swap_rnd(fprnd_t);
2 7u83 97
 
98
/* from math.h */
7 7u83 99
extern unsigned uitrunc(double);
100
extern int itrunc(double);
101
extern double rint(double);
2 7u83 102
 
103
/* from limits.h */
7 7u83 104
#define INT_MAX			(2147483647)
105
#define INT_MIN			(- (INT_MAX + 1))
2 7u83 106
 
7 7u83 107
extern int __TDFrnd_error;
108
extern unsigned int __TDFrnd_unsgned(double, fprnd_t);
109
extern int __TDFrnd_sgned(double, fprnd_t);
110
extern void __TDFerr_rnd_unsgned(double, fprnd_t);
111
extern void __TDFerr_rnd_sgned(double, fprnd_t);
2 7u83 112
 
7 7u83 113
int __TDFrnd_error = 0;
2 7u83 114
 
7 7u83 115
unsigned int
116
__TDFrnd_unsgned(double f, fprnd_t rnd_mode)
2 7u83 117
{
7 7u83 118
    fprnd_t swap;
119
    unsigned int ret_value;
120
    swap = fp_swap_rnd(rnd_mode);
121
    ret_value = uitrunc(rint(f));
122
   (void)fp_swap_rnd(swap);
123
    return(ret_value);
2 7u83 124
}
125
 
7 7u83 126
int
127
__TDFrnd_sgned(double f, fprnd_t rnd_mode)
2 7u83 128
{
7 7u83 129
    fprnd_t swap;
130
    int ret_value;
131
    swap = fp_swap_rnd(rnd_mode);
132
    ret_value = itrunc(rint(f));
133
   (void)fp_swap_rnd(swap);
134
    return(ret_value);
2 7u83 135
}
136
 
7 7u83 137
void
138
__TDFerr_rnd_unsgned(double f, fprnd_t rnd_mode)
2 7u83 139
{
7 7u83 140
    double fmin = 0.0;
141
    double fmax = 4294967295.0;
142
    switch(rnd_mode) {
143
	case FP_RND_RN: {
144
	    fmin += -0.5;
145
	    fmax += 0.5;
146
	    if (f < fmin || f > fmax) {
147
		__TDFrnd_error = 1;
2 7u83 148
	    } else {
7 7u83 149
		__TDFrnd_error = -1;
2 7u83 150
	    }
7 7u83 151
	    break;
2 7u83 152
	}
7 7u83 153
	case FP_RND_RZ: {
154
	    fmin += -1.0;
155
	    fmax += 1.0;
156
	    if (f <= fmin || f >= fmax) {
157
		__TDFrnd_error = 1;
2 7u83 158
	    } else {
7 7u83 159
		__TDFrnd_error = -1;
2 7u83 160
	    }
7 7u83 161
	    break;
2 7u83 162
	}
7 7u83 163
	case FP_RND_RP: {
164
	    fmin += -1.0;
165
	    if (f <= fmin || f > fmax) {
166
		__TDFrnd_error = 1;
2 7u83 167
	    } else {
7 7u83 168
		__TDFrnd_error = -1;
2 7u83 169
	    }
7 7u83 170
	    break;
2 7u83 171
	}
7 7u83 172
	case FP_RND_RM: {
173
	    fmax += 1.0;
174
	    if (f < fmin || f >= fmax) {
175
		__TDFrnd_error = 1;
2 7u83 176
	    } else {
7 7u83 177
		__TDFrnd_error = -1;
2 7u83 178
	    }
7 7u83 179
	    break;
2 7u83 180
	}
181
    }
7 7u83 182
    return;
2 7u83 183
}
184
 
7 7u83 185
void
186
__TDFerr_rnd_sgned(double f, fprnd_t rnd_mode)
2 7u83 187
{
7 7u83 188
    double fmin = -2147483648.0;
189
    double fmax = 2147483647.0;
190
 
191
    switch(rnd_mode) {
192
	case FP_RND_RN: {
193
	    fmin += -0.5;
194
	    fmax += 0.5;
195
	    if (f < fmin || f > fmax) {
196
		__TDFrnd_error = 1;
2 7u83 197
	    } else {
7 7u83 198
		 __TDFrnd_error = -1;
2 7u83 199
	    }
7 7u83 200
	    break;
2 7u83 201
	}
7 7u83 202
	case FP_RND_RZ: {
203
	    fmin += -1.0;
204
	    fmax += 1.0;
205
	    if (f <= fmin || f >= fmax) {
206
		__TDFrnd_error = 1;
2 7u83 207
	    } else {
7 7u83 208
		__TDFrnd_error = -1;
2 7u83 209
	    }
7 7u83 210
	    break;
2 7u83 211
	}
7 7u83 212
	case FP_RND_RP: {
213
	    fmin += -1.0;
214
	    if (f <= fmin || f > fmax) {
215
		__TDFrnd_error = 1;
2 7u83 216
	    } else {
7 7u83 217
		__TDFrnd_error = -1;
2 7u83 218
	    }
7 7u83 219
	    break;
2 7u83 220
	}
7 7u83 221
	case FP_RND_RM: {
222
	    fmax += 1.0;
223
	    if (f < fmin || f >= fmax) {
224
		__TDFrnd_error = 1;
2 7u83 225
	    } else {
7 7u83 226
		__TDFrnd_error = -1;
2 7u83 227
	    }
228
	    break;
229
	}
230
    }
7 7u83 231
    return;
2 7u83 232
}