Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / regress / lib / libc / ieeefp / testfloat / systfloat.c
blob6b1af0df69ea191905b07a940886644efbea1393
1 /* $NetBSD: systfloat.c,v 1.7 2004/04/15 19:01:57 matt Exp $ */
3 /* This is a derivative work. */
5 /*-
6 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Ross Harvey.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 ===============================================================================
37 This C source file is part of TestFloat, Release 2a, a package of programs
38 for testing the correctness of floating-point arithmetic complying to the
39 IEC/IEEE Standard for Floating-Point.
41 Written by John R. Hauser. More information is available through the Web
42 page `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/TestFloat.html'.
44 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
45 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
46 TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
47 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
48 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
50 Derivative works are acceptable, even for commercial purposes, so long as
51 (1) they include prominent notice that the work is derivative, and (2) they
52 include prominent notice akin to these four paragraphs for those parts of
53 this code that are retained.
55 ===============================================================================
58 #include <sys/cdefs.h>
59 #ifndef __lint
60 __RCSID("$NetBSD: systfloat.c,v 1.7 2004/04/15 19:01:57 matt Exp $");
61 #endif
63 #include <math.h>
64 #include <ieeefp.h>
65 #include "milieu.h"
66 #include "softfloat.h"
67 #include "systfloat.h"
68 #include "systflags.h"
69 #include "systmodes.h"
71 typedef union {
72 float32 f32;
73 float f;
74 } union32;
75 typedef union {
76 float64 f64;
77 double d;
78 } union64;
79 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
80 typedef union {
81 floatx80 fx80;
82 long double ld;
83 } unionx80;
84 #endif
85 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
86 typedef union {
87 float128 f128;
88 long double ld;
89 } union128;
90 #endif
92 fp_except
93 syst_float_flags_clear(void)
95 return fpsetsticky(0)
96 & (FP_X_IMP | FP_X_UFL | FP_X_OFL | FP_X_DZ | FP_X_INV);
99 void
100 syst_float_set_rounding_mode(fp_rnd direction)
102 fpsetround(direction);
103 fpsetmask(0);
106 float32 syst_int32_to_float32( int32 a )
108 const union32 uz = { .f = a };
110 return uz.f32;
114 float64 syst_int32_to_float64( int32 a )
116 const union64 uz = { .d = a };
118 return uz.f64;
122 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
124 floatx80 syst_int32_to_floatx80( int32 a )
126 const unionx80 uz = { .ld = a };
128 return uz.fx80;
132 #endif
134 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
136 float128 syst_int32_to_float128( int32 a )
138 const union128 uz = { .ld = a };
140 return uz.f128;
144 #endif
146 #ifdef BITS64
148 float32 syst_int64_to_float32( int64 a )
150 const union32 uz = { .f = a };
152 return uz.f32;
155 float64 syst_int64_to_float64( int64 a )
157 const union64 uz = { .d = a };
159 return uz.f64;
162 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
164 floatx80 syst_int64_to_floatx80( int64 a )
166 const unionx80 uz = { .ld = a };
168 return uz.fx80;
171 #endif
173 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
175 float128 syst_int64_to_float128( int64 a )
177 const union128 uz = { .ld = a };
179 return uz.f128;
182 #endif
184 #endif
186 int32 syst_float32_to_int32_round_to_zero( float32 a )
188 const union32 uz = { .f32 = a };
190 return uz.f;
194 #ifdef BITS64
196 int64 syst_float32_to_int64_round_to_zero( float32 a )
198 const union32 uz = { .f32 = a };
200 return uz.f;
203 #endif
205 float64 syst_float32_to_float64( float32 a )
207 const union32 ua = { .f32 = a };
208 union64 uz;
210 uz.d = ua.f;
211 return uz.f64;
215 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
217 floatx80 syst_float32_to_floatx80( float32 a )
219 const union32 ua = { .f32 = a };
220 unionx80 uz;
222 uz.ld = ua.f;
223 return uz.fx80;
226 #endif
228 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
230 float128 syst_float32_to_float128( float32 a )
232 const union32 ua = { .f32 = a };
233 union128 ub;
235 ub.ld = ua.f;
236 return ub.f128;
239 #endif
241 float32 syst_float32_add( float32 a, float32 b )
243 const union32 ua = { .f32 = a }, ub = { .f32 = b };
244 union32 uz;
246 uz.f = ua.f + ub.f;
247 return uz.f32;
250 float32 syst_float32_sub( float32 a, float32 b )
252 const union32 ua = { .f32 = a }, ub = { .f32 = b };
253 union32 uz;
255 uz.f = ua.f - ub.f;
256 return uz.f32;
259 float32 syst_float32_mul( float32 a, float32 b )
261 const union32 ua = { .f32 = a }, ub = { .f32 = b };
262 union32 uz;
264 uz.f = ua.f * ub.f;
265 return uz.f32;
268 float32 syst_float32_div( float32 a, float32 b )
270 const union32 ua = { .f32 = a }, ub = { .f32 = b };
271 union32 uz;
273 uz.f = ua.f / ub.f;
274 return uz.f32;
277 flag syst_float32_eq( float32 a, float32 b )
279 const union32 ua = { .f32 = a }, ub = { .f32 = b };
281 return ua.f == ub.f;
284 flag syst_float32_le( float32 a, float32 b )
286 const union32 ua = { .f32 = a }, ub = { .f32 = b };
288 return ua.f <= ub.f;
291 flag syst_float32_lt( float32 a, float32 b )
293 const union32 ua = { .f32 = a }, ub = { .f32 = b };
295 return ua.f < ub.f;
298 int32 syst_float64_to_int32_round_to_zero( float64 a )
300 const union64 uz = { .f64 = a };
302 return uz.d;
305 #ifdef BITS64
307 int64 syst_float64_to_int64_round_to_zero( float64 a )
309 const union64 uz = { .f64 = a };
311 return uz.d;
314 #endif
316 float32 syst_float64_to_float32( float64 a )
318 const union64 ua = { .f64 = a };
319 union32 uz;
321 uz.f = ua.d;
322 return uz.f32;
325 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
327 floatx80 syst_float64_to_floatx80( float64 a )
329 const union64 ua = { .f64 = a };
330 unionx80 u;
332 u.ld = ua.d;
333 return u.fx80;
336 #endif
338 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
340 float128 syst_float64_to_float128( float64 a )
342 const union64 ua = { .f64 = a };
343 union128 uz;
345 uz.ld = ua.d;
346 return uz.f128;
349 #endif
351 float64 syst_float64_add( float64 a, float64 b )
353 const union64 ua = { .f64 = a }, ub = { .f64 = b };
354 union64 uz;
356 uz.d = ua.d + ub.d;
357 return uz.f64;
360 float64 syst_float64_sub( float64 a, float64 b )
362 const union64 ua = { .f64 = a }, ub = { .f64 = b };
363 union64 uz;
365 uz.d = ua.d - ub.d;
366 return uz.f64;
369 float64 syst_float64_mul( float64 a, float64 b )
371 const union64 ua = { .f64 = a }, ub = { .f64 = b };
372 union64 uz;
374 uz.d = ua.d * ub.d;
375 return uz.f64;
378 float64 syst_float64_div( float64 a, float64 b )
380 const union64 ua = { .f64 = a }, ub = { .f64 = b };
381 union64 uz;
383 uz.d = ua.d / ub.d;
384 return uz.f64;
387 float64 syst_float64_sqrt( float64 a )
389 const union64 ua = { .f64 = a };
390 union64 uz;
392 uz.d = sqrt(ua.d);
393 return uz.f64;
396 flag syst_float64_eq( float64 a, float64 b )
398 const union64 ua = { .f64 = a }, ub = { .f64 = b };
400 return ua.d == ub.d;
403 flag syst_float64_le( float64 a, float64 b )
405 const union64 ua = { .f64 = a }, ub = { .f64 = b };
407 return ua.d <= ub.d;
410 flag syst_float64_lt( float64 a, float64 b )
412 const union64 ua = { .f64 = a }, ub = { .f64 = b };
414 return ua.d < ub.d;
417 #if defined( FLOATX80 ) && defined( LONG_DOUBLE_IS_FLOATX80 )
419 int32 syst_floatx80_to_int32_round_to_zero( floatx80 a )
421 const unionx80 uz = { .fx80 = a };
423 return uz.ld;
426 #ifdef BITS64
428 int64 syst_floatx80_to_int64_round_to_zero( floatx80 a )
430 const unionx80 uz = { .fx80 = a };
432 return uz.ld;
435 #endif
437 float32 syst_floatx80_to_float32( floatx80 a )
439 const unionx80 ua = { .fx80 = a };
440 union32 uz;
442 uz.f = ua.ld;
443 return uz.f32;
446 float64 syst_floatx80_to_float64( floatx80 a )
448 const unionx80 ua = { .fx80 = a };
449 union64 uz;
451 uz.d = ua.ld;
452 return uz.f64;
455 floatx80 syst_floatx80_add( floatx80 a, floatx80 b )
457 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
458 unionx80 uz;
460 uz.ld = ua.ld + ub.ld;
461 return uz.fx80;
464 floatx80 syst_floatx80_sub( floatx80 a, floatx80 b )
466 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
467 unionx80 uz;
469 uz.ld = ua.ld - ub.ld;
470 return uz.fx80;
473 floatx80 syst_floatx80_mul( floatx80 a, floatx80 b )
475 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
476 unionx80 uz;
478 uz.ld = ua.ld * ub.ld;
479 return uz.fx80;
482 floatx80 syst_floatx80_div( floatx80 a, floatx80 b )
484 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
485 unionx80 uz;
487 uz.ld = ua.ld / ub.ld;
488 return uz.fx80;
491 flag syst_floatx80_eq( floatx80 a, floatx80 b )
493 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
495 return ua.ld == ub.ld;
498 flag syst_floatx80_le( floatx80 a, floatx80 b )
500 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
502 return ua.ld <= ub.ld;
505 flag syst_floatx80_lt( floatx80 a, floatx80 b )
507 const unionx80 ua = { .fx80 = a }, ub = { .fx80 = b };
509 return ua.ld < ub.ld;
512 #endif
514 #if defined( FLOAT128 ) && defined( LONG_DOUBLE_IS_FLOAT128 )
516 int32 syst_float128_to_int32_round_to_zero( float128 a )
518 const union128 ua = { .f128 = a };
520 return ua.ld;
523 #ifdef BITS64
525 int64 syst_float128_to_int64_round_to_zero( float128 a )
527 const union128 ua = { .f128 = a };
529 return ua.ld;
532 #endif
534 float32 syst_float128_to_float32( float128 a )
536 const union128 ua = { .f128 = a };
537 union32 uz;
539 uz.f = ua.ld;
540 return uz.f32;
544 float64 syst_float128_to_float64( float128 a )
546 const union128 ua = { .f128 = a };
547 union64 uz;
549 uz.d = ua.ld;
550 return uz.f64;
553 float128 syst_float128_add( float128 a, float128 b )
555 const union128 ua = { .f128 = a }, ub = { .f128 = b };
556 union128 uz;
558 uz.ld = ua.ld + ub.ld;
559 return uz.f128;
563 float128 syst_float128_sub( float128 a, float128 b )
565 const union128 ua = { .f128 = a }, ub = { .f128 = b };
566 union128 uz;
568 uz.ld = ua.ld - ub.ld;
569 return uz.f128;
572 float128 syst_float128_mul( float128 a, float128 b )
574 const union128 ua = { .f128 = a }, ub = { .f128 = b };
575 union128 uz;
577 uz.ld = ua.ld * ub.ld;
578 return uz.f128;
581 float128 syst_float128_div( float128 a, float128 b )
583 const union128 ua = { .f128 = a }, ub = { .f128 = b };
584 union128 uz;
586 uz.ld = ua.ld / ub.ld;
587 return uz.f128;
590 flag syst_float128_eq( float128 a, float128 b )
592 const union128 ua = { .f128 = a }, ub = { .f128 = b };
594 return ua.ld == ub.ld;
597 flag syst_float128_le( float128 a, float128 b )
599 const union128 ua = { .f128 = a }, ub = { .f128 = b };
601 return ua.ld <= ub.ld;
604 flag syst_float128_lt( float128 a, float128 b )
606 const union128 ua = { .f128 = a }, ub = { .f128 = b };
608 return ua.ld < ub.ld;
611 #endif