Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/m9x/fmaxf.c
+++ new/usr/src/lib/libm/common/m9x/fmaxf.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #if defined(ELFOBJ)
31 31 #pragma weak fmaxf = __fmaxf
32 32 #endif
33 33
34 34 /*
35 35 * fmax(x,y) returns the larger of x and y. If just one of the
36 36 * arguments is NaN, fmax returns the other argument. If both
37 37 * arguments are NaN, fmax returns NaN (ideally, one of the
38 38 * argument NaNs).
39 39 *
40 40 * C99 does not require that fmax(-0,+0) = fmax(+0,-0) = +0, but
41 41 * ideally fmax should satisfy this.
42 42 *
43 43 * C99 makes no mention of exceptions for fmax. I suppose ideally
44 44 * either fmax never raises any exceptions or else it raises the
45 45 * invalid operation exception if and only if some argument is a
46 46 * signaling NaN. In the former case, fmax should always return
47 47 * one of its arguments. In the latter, fmax shouldn't return a
48 48 * signaling NaN, although when both arguments are signaling NaNs,
49 49 * this ideal is at odds with the stipulation that fmax should
50 50 * always return one of its arguments.
51 51 *
52 52 * Commutativity of fmax follows from the properties listed above
53 53 * except when both arguments are NaN. In that case, fmax may be
54 54 * declared commutative by fiat because there is no portable way
55 55 * to tell different NaNs apart. Ideally fmax would be truly com-
56 56 * mutative for all arguments.
57 57 *
58 58 * On SPARC V8, fmax must involve tests and branches. Ideally,
59 59 * an implementation on SPARC V9 should avoid branching, using
60 60 * conditional moves instead where necessary, and be as efficient
61 61 * as possible in its use of other resources.
62 62 *
63 63 * It appears to be impossible to attain all of the aforementioned
64 64 * ideals simultaneously. The implementation below satisfies the
65 65 * following (on SPARC):
66 66 *
67 67 * 1. fmax(x,y) returns the larger of x and y if neither x nor y
68 68 * is NaN and the non-NaN argument if just one of x or y is NaN.
69 69 * If both x and y are NaN, fmax(x,y) returns x unchanged.
70 70 * 2. fmax(-0,+0) = fmax(+0,-0) = +0.
71 71 * 3. If either argument is a signaling NaN, fmax raises the invalid
72 72 * operation exception. Otherwise, it raises no exceptions.
73 73 */
74 74
75 75 #include "libm.h" /* for isgreaterequal macro */
76 76
77 77 float
78 78 __fmaxf(float x, float y) {
79 79 /*
80 80 * On SPARC v8plus/v9, this could be implemented as follows
81 81 * (assuming %f0 = x, %f1 = y, return value left in %f0):
82 82 *
83 83 * fcmps %fcc0,%f1,%f1
84 84 * fmovsu %fcc0,%f0,%f1
85 85 * fcmps %fcc0,%f0,%f1
86 86 * fmovsul %fcc0,%f1,%f0
87 87 * st %f0,[x]
88 88 * st %f1,[y]
89 89 * ld [x],%l0
90 90 * ld [y],%l1
91 91 * and %l0,%l1,%l2
92 92 * sethi %hi(0x80000000),%l3
93 93 * andn %l3,%l2,%l2
94 94 * andn %l0,%l2,%l0
95 95 * st %l0,[x]
96 96 * ld [x],%f0
97 97 *
98 98 * If VIS instructions are available, use this code instead:
99 99 *
100 100 * fcmps %fcc0,%f1,%f1
101 101 * fmovsu %fcc0,%f0,%f1
102 102 * fcmps %fcc0,%f0,%f1
103 103 * fmovsul %fcc0,%f1,%f0
104 104 * fands %f0,%f1,%f2
105 105 * fzeros %f3
106 106 * fnegs %f3,%f3
107 107 * fandnot2s %f3,%f2,%f2
108 108 * fandnot2s %f0,%f2,%f0
109 109 *
110 110 * If VIS 3.0 instructions are available, use this:
111 111 *
112 112 * flcmps %fcc0,%f0,%f1
113 113 * fmovslg %fcc0,%f1,%f0 ! move if %fcc0 is 1 or 2
114 114 */
115 115
116 116 union {
117 117 unsigned i;
118 118 float f;
119 119 } xx, yy;
↓ open down ↓ |
119 lines elided |
↑ open up ↑ |
120 120 unsigned s;
121 121
122 122 /* if y is nan, replace it by x */
123 123 if (y != y)
124 124 y = x;
125 125
126 126 /* if x is nan, replace it by y */
127 127 if (x != x)
128 128 x = y;
129 129
130 - /* if x is less than y or x and y are unordered, replace x by y */
131 -#if defined(COMPARISON_MACRO_BUG)
132 - if (x < y)
133 -#else
134 - if (!isgreaterequal(x, y))
135 -#endif
130 + /* At this point, x and y are either both numeric, or both NaN */
131 + if (!isnan(x) && !isgreaterequal(x, y))
136 132 x = y;
137 133
138 134 /*
139 - * now x and y are either both NaN or both numeric; clear the
140 - * sign of the result if either x or y has its sign clear
135 + * clear the sign of the result if either x or y has its sign clear
141 136 */
142 137 xx.f = x;
143 138 yy.f = y;
144 139 s = ~(xx.i & yy.i) & 0x80000000;
145 140 xx.i &= ~s;
146 141
147 142 return (xx.f);
148 143 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX