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