Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4 *
5 * Floating-point emulation code
6 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7 */
8/*
9 * BEGIN_DESC
10 *
11 * File:
12 * @(#) pa/spmath/sfcmp.c $Revision: 1.1 $
13 *
14 * Purpose:
15 * sgl_cmp: compare two values
16 *
17 * External Interfaces:
18 * sgl_fcmp(leftptr, rightptr, cond, status)
19 *
20 * Internal Interfaces:
21 *
22 * Theory:
23 * <<please update with a overview of the operation of this file>>
24 *
25 * END_DESC
26*/
27
28
29#include "float.h"
30#include "sgl_float.h"
31
32/*
33 * sgl_cmp: compare two values
34 */
35int
36sgl_fcmp (sgl_floating_point * leftptr, sgl_floating_point * rightptr,
37 unsigned int cond, unsigned int *status)
38
39 /* The predicate to be tested */
40
41 {
42 register unsigned int left, right;
43 register int xorresult;
44
45 /* Create local copies of the numbers */
46 left = *leftptr;
47 right = *rightptr;
48
49 /*
50 * Test for NaN
51 */
52 if( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
53 || (Sgl_exponent(right) == SGL_INFINITY_EXPONENT) )
54 {
55 /* Check if a NaN is involved. Signal an invalid exception when
56 * comparing a signaling NaN or when comparing quiet NaNs and the
57 * low bit of the condition is set */
58 if( ( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
59 && Sgl_isnotzero_mantissa(left)
60 && (Exception(cond) || Sgl_isone_signaling(left)))
61 ||
62 ( (Sgl_exponent(right) == SGL_INFINITY_EXPONENT)
63 && Sgl_isnotzero_mantissa(right)
64 && (Exception(cond) || Sgl_isone_signaling(right)) ) )
65 {
66 if( Is_invalidtrap_enabled() ) {
67 Set_status_cbit(Unordered(cond));
68 return(INVALIDEXCEPTION);
69 }
70 else Set_invalidflag();
71 Set_status_cbit(Unordered(cond));
72 return(NOEXCEPTION);
73 }
74 /* All the exceptional conditions are handled, now special case
75 NaN compares */
76 else if( ((Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
77 && Sgl_isnotzero_mantissa(left))
78 ||
79 ((Sgl_exponent(right) == SGL_INFINITY_EXPONENT)
80 && Sgl_isnotzero_mantissa(right)) )
81 {
82 /* NaNs always compare unordered. */
83 Set_status_cbit(Unordered(cond));
84 return(NOEXCEPTION);
85 }
86 /* infinities will drop down to the normal compare mechanisms */
87 }
88 /* First compare for unequal signs => less or greater or
89 * special equal case */
90 Sgl_xortointp1(left,right,xorresult);
91 if( xorresult < 0 )
92 {
93 /* left negative => less, left positive => greater.
94 * equal is possible if both operands are zeros. */
95 if( Sgl_iszero_exponentmantissa(left)
96 && Sgl_iszero_exponentmantissa(right) )
97 {
98 Set_status_cbit(Equal(cond));
99 }
100 else if( Sgl_isone_sign(left) )
101 {
102 Set_status_cbit(Lessthan(cond));
103 }
104 else
105 {
106 Set_status_cbit(Greaterthan(cond));
107 }
108 }
109 /* Signs are the same. Treat negative numbers separately
110 * from the positives because of the reversed sense. */
111 else if( Sgl_all(left) == Sgl_all(right) )
112 {
113 Set_status_cbit(Equal(cond));
114 }
115 else if( Sgl_iszero_sign(left) )
116 {
117 /* Positive compare */
118 if( Sgl_all(left) < Sgl_all(right) )
119 {
120 Set_status_cbit(Lessthan(cond));
121 }
122 else
123 {
124 Set_status_cbit(Greaterthan(cond));
125 }
126 }
127 else
128 {
129 /* Negative compare. Signed or unsigned compares
130 * both work the same. That distinction is only
131 * important when the sign bits differ. */
132 if( Sgl_all(left) > Sgl_all(right) )
133 {
134 Set_status_cbit(Lessthan(cond));
135 }
136 else
137 {
138 Set_status_cbit(Greaterthan(cond));
139 }
140 }
141 return(NOEXCEPTION);
142 }
1/*
2 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
3 *
4 * Floating-point emulation code
5 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21/*
22 * BEGIN_DESC
23 *
24 * File:
25 * @(#) pa/spmath/sfcmp.c $Revision: 1.1 $
26 *
27 * Purpose:
28 * sgl_cmp: compare two values
29 *
30 * External Interfaces:
31 * sgl_fcmp(leftptr, rightptr, cond, status)
32 *
33 * Internal Interfaces:
34 *
35 * Theory:
36 * <<please update with a overview of the operation of this file>>
37 *
38 * END_DESC
39*/
40
41
42#include "float.h"
43#include "sgl_float.h"
44
45/*
46 * sgl_cmp: compare two values
47 */
48int
49sgl_fcmp (sgl_floating_point * leftptr, sgl_floating_point * rightptr,
50 unsigned int cond, unsigned int *status)
51
52 /* The predicate to be tested */
53
54 {
55 register unsigned int left, right;
56 register int xorresult;
57
58 /* Create local copies of the numbers */
59 left = *leftptr;
60 right = *rightptr;
61
62 /*
63 * Test for NaN
64 */
65 if( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
66 || (Sgl_exponent(right) == SGL_INFINITY_EXPONENT) )
67 {
68 /* Check if a NaN is involved. Signal an invalid exception when
69 * comparing a signaling NaN or when comparing quiet NaNs and the
70 * low bit of the condition is set */
71 if( ( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
72 && Sgl_isnotzero_mantissa(left)
73 && (Exception(cond) || Sgl_isone_signaling(left)))
74 ||
75 ( (Sgl_exponent(right) == SGL_INFINITY_EXPONENT)
76 && Sgl_isnotzero_mantissa(right)
77 && (Exception(cond) || Sgl_isone_signaling(right)) ) )
78 {
79 if( Is_invalidtrap_enabled() ) {
80 Set_status_cbit(Unordered(cond));
81 return(INVALIDEXCEPTION);
82 }
83 else Set_invalidflag();
84 Set_status_cbit(Unordered(cond));
85 return(NOEXCEPTION);
86 }
87 /* All the exceptional conditions are handled, now special case
88 NaN compares */
89 else if( ((Sgl_exponent(left) == SGL_INFINITY_EXPONENT)
90 && Sgl_isnotzero_mantissa(left))
91 ||
92 ((Sgl_exponent(right) == SGL_INFINITY_EXPONENT)
93 && Sgl_isnotzero_mantissa(right)) )
94 {
95 /* NaNs always compare unordered. */
96 Set_status_cbit(Unordered(cond));
97 return(NOEXCEPTION);
98 }
99 /* infinities will drop down to the normal compare mechanisms */
100 }
101 /* First compare for unequal signs => less or greater or
102 * special equal case */
103 Sgl_xortointp1(left,right,xorresult);
104 if( xorresult < 0 )
105 {
106 /* left negative => less, left positive => greater.
107 * equal is possible if both operands are zeros. */
108 if( Sgl_iszero_exponentmantissa(left)
109 && Sgl_iszero_exponentmantissa(right) )
110 {
111 Set_status_cbit(Equal(cond));
112 }
113 else if( Sgl_isone_sign(left) )
114 {
115 Set_status_cbit(Lessthan(cond));
116 }
117 else
118 {
119 Set_status_cbit(Greaterthan(cond));
120 }
121 }
122 /* Signs are the same. Treat negative numbers separately
123 * from the positives because of the reversed sense. */
124 else if( Sgl_all(left) == Sgl_all(right) )
125 {
126 Set_status_cbit(Equal(cond));
127 }
128 else if( Sgl_iszero_sign(left) )
129 {
130 /* Positive compare */
131 if( Sgl_all(left) < Sgl_all(right) )
132 {
133 Set_status_cbit(Lessthan(cond));
134 }
135 else
136 {
137 Set_status_cbit(Greaterthan(cond));
138 }
139 }
140 else
141 {
142 /* Negative compare. Signed or unsigned compares
143 * both work the same. That distinction is only
144 * important when the sign bits differ. */
145 if( Sgl_all(left) > Sgl_all(right) )
146 {
147 Set_status_cbit(Lessthan(cond));
148 }
149 else
150 {
151 Set_status_cbit(Greaterthan(cond));
152 }
153 }
154 return(NOEXCEPTION);
155 }