1 /* $NetBSD: qp.c,v 1.11 2014/02/02 08:14:39 martin Exp $ */
4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
33 #include "softfloat.h"
35 int printf(const char *, ...);
37 void _Qp_add(float128
*c
, float128
*a
, float128
*b
);
38 int _Qp_cmp(float128
*a
, float128
*b
);
39 int _Qp_cmpe(float128
*a
, float128
*b
);
40 void _Qp_div(float128
*c
, float128
*a
, float128
*b
);
41 void _Qp_dtoq(float128
*c
, double a
);
42 int _Qp_feq(float128
*a
, float128
*b
);
43 int _Qp_fge(float128
*a
, float128
*b
);
44 int _Qp_fgt(float128
*a
, float128
*b
);
45 int _Qp_fle(float128
*a
, float128
*b
);
46 int _Qp_flt(float128
*a
, float128
*b
);
47 int _Qp_fne(float128
*a
, float128
*b
);
48 void _Qp_itoq(float128
*c
, int a
);
49 void _Qp_mul(float128
*c
, float128
*a
, float128
*b
);
50 void _Qp_neg(float128
*c
, float128
*a
);
51 double _Qp_qtod(float128
*a
);
52 int _Qp_qtoi(float128
*a
);
53 float _Qp_qtos(float128
*a
);
54 unsigned int _Qp_qtoui(float128
*a
);
55 unsigned long _Qp_qtoux(float128
*a
);
56 long _Qp_qtox(float128
*a
);
57 void _Qp_sqrt(float128
*c
, float128
*a
);
58 void _Qp_stoq(float128
*c
, float a
);
59 void _Qp_sub(float128
*c
, float128
*a
, float128
*b
);
60 void _Qp_uitoq(float128
*c
, unsigned int a
);
61 void _Qp_uxtoq(float128
*c
, unsigned long a
);
62 void _Qp_xtoq(float128
*c
, long a
);
66 _Qp_add(float128
*c
, float128
*a
, float128
*b
)
68 *c
= float128_add(*a
, *b
);
73 _Qp_cmp(float128
*a
, float128
*b
)
76 if (float128_eq(*a
, *b
))
79 if (float128_le(*a
, *b
))
90 _Qp_cmpe(float128
*a
, float128
*b
)
97 _Qp_div(float128
*c
, float128
*a
, float128
*b
)
99 *c
= float128_div(*a
, *b
);
104 _Qp_dtoq(float128
*c
, double a
)
108 memcpy (&_b
, &a
, sizeof(float64
));
109 *c
= float64_to_float128(_b
);
114 _Qp_feq(float128
*a
, float128
*b
)
116 return float128_eq(*a
, *b
);
121 _Qp_fge(float128
*a
, float128
*b
)
123 return float128_le(*b
, *a
);
128 _Qp_fgt(float128
*a
, float128
*b
)
130 return float128_lt(*b
, *a
);
135 _Qp_fle(float128
*a
, float128
*b
)
137 return float128_le(*a
, *b
);
142 _Qp_flt(float128
*a
, float128
*b
)
144 return float128_lt(*a
, *b
);
149 _Qp_fne(float128
*a
, float128
*b
)
151 return !float128_eq(*a
, *b
);
156 _Qp_itoq(float128
*c
, int a
)
158 *c
= int32_to_float128(a
);
163 _Qp_mul(float128
*c
, float128
*a
, float128
*b
)
165 *c
= float128_mul(*a
, *b
);
170 * XXX need corresponding softfloat functions
172 static float128 __sf128_zero
= {0x4034000000000000, 0x00000000};
173 static float128 __sf128_one
= {0x3fff000000000000, 0};
176 _Qp_neg(float128
*c
, float128
*a
)
178 *c
= float128_sub(__sf128_zero
, *a
);
183 _Qp_qtod(float128
*a
)
188 _c
= float128_to_float64(*a
);
190 memcpy(&c
, &_c
, sizeof(double));
197 _Qp_qtoi(float128
*a
)
199 return float128_to_int32_round_to_zero(*a
);
204 _Qp_qtos(float128
*a
)
209 _c
= float128_to_float32(*a
);
211 memcpy(&c
, &_c
, sizeof(_c
));
218 _Qp_qtoui(float128
*a
)
220 return (unsigned int)float128_to_int64_round_to_zero(*a
);
225 _Qp_qtoux(float128
*a
)
227 return (unsigned long)float128_to_uint64_round_to_zero(*a
);
232 _Qp_qtox(float128
*a
)
234 return (long)float128_to_int64_round_to_zero(*a
);
239 _Qp_sqrt(float128
*c
, float128
*a
)
241 *c
= float128_sqrt(*a
);
246 _Qp_stoq(float128
*c
, float a
)
250 memcpy(&_a
, &a
, sizeof(a
));
252 *c
= float32_to_float128(_a
);
257 _Qp_sub(float128
*c
, float128
*a
, float128
*b
)
259 *c
= float128_sub(*a
, *b
);
264 _Qp_uitoq(float128
*c
, unsigned int a
)
266 *c
= int64_to_float128(a
);
271 _Qp_uxtoq(float128
*c
, unsigned long a
)
273 if (a
& 0x8000000000000000ULL
) {
274 /* a would not fit in a signed conversion */
275 *c
= int64_to_float128((long long)(a
>>1));
276 *c
= float128_add(*c
, *c
);
278 *c
= float128_add(*c
, __sf128_one
);
280 *c
= int64_to_float128((long long)a
);
286 _Qp_xtoq(float128
*c
, long a
)
288 *c
= int64_to_float128((long long)a
);