source: mainline/uspace/lib/softfloat/comparison.c@ 8f6c7785

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8f6c7785 was 2416085, checked in by Martin Decky <martin@…>, 13 years ago

softfloat: move sources to a single directory
implement more ARM EABI bindings

  • Property mode set to 100644
File size: 11.9 KB
Line 
1/*
2 * Copyright (c) 2005 Josef Cejka
3 * Copyright (c) 2011 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup softfloat
31 * @{
32 */
33/** @file Comparison functions.
34 */
35
36#include "sftypes.h"
37#include "comparison.h"
38#include "common.h"
39
40/**
41 * Determines whether the given float represents NaN (either signalling NaN or
42 * quiet NaN).
43 *
44 * @param f Single-precision float.
45 * @return 1 if float is NaN, 0 otherwise.
46 */
47int is_float32_nan(float32 f)
48{
49 /* NaN : exp = 0xff and nonzero fraction */
50 return ((f.parts.exp == 0xFF) && (f.parts.fraction));
51}
52
53/**
54 * Determines whether the given float represents NaN (either signalling NaN or
55 * quiet NaN).
56 *
57 * @param d Double-precision float.
58 * @return 1 if float is NaN, 0 otherwise.
59 */
60int is_float64_nan(float64 d)
61{
62 /* NaN : exp = 0x7ff and nonzero fraction */
63 return ((d.parts.exp == 0x7FF) && (d.parts.fraction));
64}
65
66/**
67 * Determines whether the given float represents NaN (either signalling NaN or
68 * quiet NaN).
69 *
70 * @param ld Quadruple-precision float.
71 * @return 1 if float is NaN, 0 otherwise.
72 */
73int is_float128_nan(float128 ld)
74{
75 /* NaN : exp = 0x7fff and nonzero fraction */
76 return ((ld.parts.exp == 0x7FF) &&
77 !eq128(ld.parts.frac_hi, ld.parts.frac_lo, 0x0ll, 0x0ll));
78}
79
80/**
81 * Determines whether the given float represents signalling NaN.
82 *
83 * @param f Single-precision float.
84 * @return 1 if float is signalling NaN, 0 otherwise.
85 */
86int is_float32_signan(float32 f)
87{
88 /* SigNaN : exp = 0xff and fraction = 0xxxxx..x (binary),
89 * where at least one x is nonzero */
90 return ((f.parts.exp == 0xFF) &&
91 (f.parts.fraction < 0x400000) && (f.parts.fraction));
92}
93
94/**
95 * Determines whether the given float represents signalling NaN.
96 *
97 * @param d Double-precision float.
98 * @return 1 if float is signalling NaN, 0 otherwise.
99 */
100int is_float64_signan(float64 d)
101{
102 /* SigNaN : exp = 0x7ff and fraction = 0xxxxx..x (binary),
103 * where at least one x is nonzero */
104 return ((d.parts.exp == 0x7FF) &&
105 (d.parts.fraction) && (d.parts.fraction < 0x8000000000000ll));
106}
107
108/**
109 * Determines whether the given float represents signalling NaN.
110 *
111 * @param ld Quadruple-precision float.
112 * @return 1 if float is signalling NaN, 0 otherwise.
113 */
114int is_float128_signan(float128 ld)
115{
116 /* SigNaN : exp = 0x7fff and fraction = 0xxxxx..x (binary),
117 * where at least one x is nonzero */
118 return ((ld.parts.exp == 0x7FFF) &&
119 (ld.parts.frac_hi || ld.parts.frac_lo) &&
120 lt128(ld.parts.frac_hi, ld.parts.frac_lo, 0x800000000000ll, 0x0ll));
121
122}
123
124/**
125 * Determines whether the given float represents positive or negative infinity.
126 *
127 * @param f Single-precision float.
128 * @return 1 if float is infinite, 0 otherwise.
129 */
130int is_float32_infinity(float32 f)
131{
132 /* NaN : exp = 0x7ff and zero fraction */
133 return ((f.parts.exp == 0xFF) && (f.parts.fraction == 0x0));
134}
135
136/**
137 * Determines whether the given float represents positive or negative infinity.
138 *
139 * @param d Double-precision float.
140 * @return 1 if float is infinite, 0 otherwise.
141 */
142int is_float64_infinity(float64 d)
143{
144 /* NaN : exp = 0x7ff and zero fraction */
145 return ((d.parts.exp == 0x7FF) && (d.parts.fraction == 0x0));
146}
147
148/**
149 * Determines whether the given float represents positive or negative infinity.
150 *
151 * @param ld Quadruple-precision float.
152 * @return 1 if float is infinite, 0 otherwise.
153 */
154int is_float128_infinity(float128 ld)
155{
156 /* NaN : exp = 0x7fff and zero fraction */
157 return ((ld.parts.exp == 0x7FFF) &&
158 eq128(ld.parts.frac_hi, ld.parts.frac_lo, 0x0ll, 0x0ll));
159}
160
161/**
162 * Determines whether the given float represents positive or negative zero.
163 *
164 * @param f Single-precision float.
165 * @return 1 if float is zero, 0 otherwise.
166 */
167int is_float32_zero(float32 f)
168{
169 return (((f.bin) & 0x7FFFFFFF) == 0);
170}
171
172/**
173 * Determines whether the given float represents positive or negative zero.
174 *
175 * @param d Double-precision float.
176 * @return 1 if float is zero, 0 otherwise.
177 */
178int is_float64_zero(float64 d)
179{
180 return (((d.bin) & 0x7FFFFFFFFFFFFFFFll) == 0);
181}
182
183/**
184 * Determines whether the given float represents positive or negative zero.
185 *
186 * @param ld Quadruple-precision float.
187 * @return 1 if float is zero, 0 otherwise.
188 */
189int is_float128_zero(float128 ld)
190{
191 uint64_t tmp_hi;
192 uint64_t tmp_lo;
193
194 and128(ld.bin.hi, ld.bin.lo,
195 0x7FFFFFFFFFFFFFFFll, 0xFFFFFFFFFFFFFFFFll, &tmp_hi, &tmp_lo);
196
197 return eq128(tmp_hi, tmp_lo, 0x0ll, 0x0ll);
198}
199
200/**
201 * Determine whether two floats are equal. NaNs are not recognized.
202 *
203 * @a First single-precision operand.
204 * @b Second single-precision operand.
205 * @return 1 if both floats are equal, 0 otherwise.
206 */
207int is_float32_eq(float32 a, float32 b)
208{
209 /* a equals to b or both are zeros (with any sign) */
210 return ((a.bin == b.bin) ||
211 (((a.bin | b.bin) & 0x7FFFFFFF) == 0));
212}
213
214/**
215 * Determine whether two floats are equal. NaNs are not recognized.
216 *
217 * @a First double-precision operand.
218 * @b Second double-precision operand.
219 * @return 1 if both floats are equal, 0 otherwise.
220 */
221int is_float64_eq(float64 a, float64 b)
222{
223 /* a equals to b or both are zeros (with any sign) */
224 return ((a.bin == b.bin) ||
225 (((a.bin | b.bin) & 0x7FFFFFFFFFFFFFFFll) == 0));
226}
227
228/**
229 * Determine whether two floats are equal. NaNs are not recognized.
230 *
231 * @a First quadruple-precision operand.
232 * @b Second quadruple-precision operand.
233 * @return 1 if both floats are equal, 0 otherwise.
234 */
235int is_float128_eq(float128 a, float128 b)
236{
237 uint64_t tmp_hi;
238 uint64_t tmp_lo;
239
240 /* both are zeros (with any sign) */
241 or128(a.bin.hi, a.bin.lo,
242 b.bin.hi, b.bin.lo, &tmp_hi, &tmp_lo);
243 and128(tmp_hi, tmp_lo,
244 0x7FFFFFFFFFFFFFFFll, 0xFFFFFFFFFFFFFFFFll, &tmp_hi, &tmp_lo);
245 int both_zero = eq128(tmp_hi, tmp_lo, 0x0ll, 0x0ll);
246
247 /* a equals to b */
248 int are_equal = eq128(a.bin.hi, a.bin.lo, b.bin.hi, b.bin.lo);
249
250 return are_equal || both_zero;
251}
252
253/**
254 * Lower-than comparison between two floats. NaNs are not recognized.
255 *
256 * @a First single-precision operand.
257 * @b Second single-precision operand.
258 * @return 1 if a is lower than b, 0 otherwise.
259 */
260int is_float32_lt(float32 a, float32 b)
261{
262 if (((a.bin | b.bin) & 0x7FFFFFFF) == 0) {
263 /* +- zeroes */
264 return 0;
265 }
266
267 if ((a.parts.sign) && (b.parts.sign)) {
268 /* if both are negative, smaller is that with greater binary value */
269 return (a.bin > b.bin);
270 }
271
272 /*
273 * lets negate signs - now will be positive numbers always
274 * bigger than negative (first bit will be set for unsigned
275 * integer comparison)
276 */
277 a.parts.sign = !a.parts.sign;
278 b.parts.sign = !b.parts.sign;
279 return (a.bin < b.bin);
280}
281
282/**
283 * Lower-than comparison between two floats. NaNs are not recognized.
284 *
285 * @a First double-precision operand.
286 * @b Second double-precision operand.
287 * @return 1 if a is lower than b, 0 otherwise.
288 */
289int is_float64_lt(float64 a, float64 b)
290{
291 if (((a.bin | b.bin) & 0x7FFFFFFFFFFFFFFFll) == 0) {
292 /* +- zeroes */
293 return 0;
294 }
295
296 if ((a.parts.sign) && (b.parts.sign)) {
297 /* if both are negative, smaller is that with greater binary value */
298 return (a.bin > b.bin);
299 }
300
301 /*
302 * lets negate signs - now will be positive numbers always
303 * bigger than negative (first bit will be set for unsigned
304 * integer comparison)
305 */
306 a.parts.sign = !a.parts.sign;
307 b.parts.sign = !b.parts.sign;
308 return (a.bin < b.bin);
309}
310
311/**
312 * Lower-than comparison between two floats. NaNs are not recognized.
313 *
314 * @a First quadruple-precision operand.
315 * @b Second quadruple-precision operand.
316 * @return 1 if a is lower than b, 0 otherwise.
317 */
318int is_float128_lt(float128 a, float128 b)
319{
320 uint64_t tmp_hi;
321 uint64_t tmp_lo;
322
323 or128(a.bin.hi, a.bin.lo,
324 b.bin.hi, b.bin.lo, &tmp_hi, &tmp_lo);
325 and128(tmp_hi, tmp_lo,
326 0x7FFFFFFFFFFFFFFFll, 0xFFFFFFFFFFFFFFFFll, &tmp_hi, &tmp_lo);
327 if (eq128(tmp_hi, tmp_lo, 0x0ll, 0x0ll)) {
328 /* +- zeroes */
329 return 0;
330 }
331
332 if ((a.parts.sign) && (b.parts.sign)) {
333 /* if both are negative, smaller is that with greater binary value */
334 return lt128(b.bin.hi, b.bin.lo, a.bin.hi, a.bin.lo);
335 }
336
337 /*
338 * lets negate signs - now will be positive numbers always
339 * bigger than negative (first bit will be set for unsigned
340 * integer comparison)
341 */
342 a.parts.sign = !a.parts.sign;
343 b.parts.sign = !b.parts.sign;
344 return lt128(a.bin.hi, a.bin.lo, b.bin.hi, b.bin.lo);
345}
346
347/**
348 * Greater-than comparison between two floats. NaNs are not recognized.
349 *
350 * @a First single-precision operand.
351 * @b Second single-precision operand.
352 * @return 1 if a is greater than b, 0 otherwise.
353 */
354int is_float32_gt(float32 a, float32 b)
355{
356 if (((a.bin | b.bin) & 0x7FFFFFFF) == 0) {
357 /* zeroes are equal with any sign */
358 return 0;
359 }
360
361 if ((a.parts.sign) && (b.parts.sign)) {
362 /* if both are negative, greater is that with smaller binary value */
363 return (a.bin < b.bin);
364 }
365
366 /*
367 * lets negate signs - now will be positive numbers always
368 * bigger than negative (first bit will be set for unsigned
369 * integer comparison)
370 */
371 a.parts.sign = !a.parts.sign;
372 b.parts.sign = !b.parts.sign;
373 return (a.bin > b.bin);
374}
375
376/**
377 * Greater-than comparison between two floats. NaNs are not recognized.
378 *
379 * @a First double-precision operand.
380 * @b Second double-precision operand.
381 * @return 1 if a is greater than b, 0 otherwise.
382 */
383int is_float64_gt(float64 a, float64 b)
384{
385 if (((a.bin | b.bin) & 0x7FFFFFFFFFFFFFFFll) == 0) {
386 /* zeroes are equal with any sign */
387 return 0;
388 }
389
390 if ((a.parts.sign) && (b.parts.sign)) {
391 /* if both are negative, greater is that with smaller binary value */
392 return (a.bin < b.bin);
393 }
394
395 /*
396 * lets negate signs - now will be positive numbers always
397 * bigger than negative (first bit will be set for unsigned
398 * integer comparison)
399 */
400 a.parts.sign = !a.parts.sign;
401 b.parts.sign = !b.parts.sign;
402 return (a.bin > b.bin);
403}
404
405/**
406 * Greater-than comparison between two floats. NaNs are not recognized.
407 *
408 * @a First quadruple-precision operand.
409 * @b Second quadruple-precision operand.
410 * @return 1 if a is greater than b, 0 otherwise.
411 */
412int is_float128_gt(float128 a, float128 b)
413{
414 uint64_t tmp_hi;
415 uint64_t tmp_lo;
416
417 or128(a.bin.hi, a.bin.lo,
418 b.bin.hi, b.bin.lo, &tmp_hi, &tmp_lo);
419 and128(tmp_hi, tmp_lo,
420 0x7FFFFFFFFFFFFFFFll, 0xFFFFFFFFFFFFFFFFll, &tmp_hi, &tmp_lo);
421 if (eq128(tmp_hi, tmp_lo, 0x0ll, 0x0ll)) {
422 /* zeroes are equal with any sign */
423 return 0;
424 }
425
426 if ((a.parts.sign) && (b.parts.sign)) {
427 /* if both are negative, greater is that with smaller binary value */
428 return lt128(a.bin.hi, a.bin.lo, b.bin.hi, b.bin.lo);
429 }
430
431 /*
432 * lets negate signs - now will be positive numbers always
433 * bigger than negative (first bit will be set for unsigned
434 * integer comparison)
435 */
436 a.parts.sign = !a.parts.sign;
437 b.parts.sign = !b.parts.sign;
438 return lt128(b.bin.hi, b.bin.lo, a.bin.hi, a.bin.lo);
439}
440
441/** @}
442 */
Note: See TracBrowser for help on using the repository browser.