Update.
[glibc.git] / sysdeps / powerpc / q_qtou.c
bloba5583cc8165c5e8fed1792055433360db2e4e26c
1 /* 128-bit floating point to 32-bit fixed point unsigned integer.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <quad_float.h>
22 /* unsigned int _q_qtou(const long double *a);
23 Convert 'a' to unsigned int by truncation.
25 Special cases:
26 -0: return 0
27 NaN: raise VXCVI, return 0
28 SNaN: raise VXSNAN, VXCVI, and return 0
29 Negative numbers (other than -0, but including -Inf): raise VXCVI, return 0
30 >= 2^32: raise VXCVI, return 0xffffffff
33 unsigned int
34 __q_qtou(const unsigned long long a[2])
36 int ax;
37 unsigned result;
38 unsigned long long a0;
40 /* Deal with non-special cases. */
41 a0 = a[0];
42 ax = (a0 >> 48) - 16383;
43 if (ax <= 31)
44 /* If we had a '>>' operator that behaved properly with respect to
45 large shifts, we wouldn't need the '& -(ax >> 31)' term, which
46 clears 'result' if ax is negative. */
47 return (unsigned)(a0 >> 17 | 0x80000000) >> 31-ax & -(ax >> 31);
49 /* 'result' is 1 if a is negative, 0 otherwise. */
50 result = a0 >> 63;
51 /* Check for -0, otherwise raise VXCVI. */
52 if (a0 != 0x8000000000000000ULL || a[1] != 0)
54 /* Check for SNaN, if so raise VXSNAN. */
55 if ((a0 >> 32 & 0x7fff8000) == 0x7fff0000
56 && (a[1] | a0 & 0x00007fffffffffffULL) != 0)
57 set_fpscr_bit(FPSCR_VXSNAN);
58 /* Treat all NaNs as large negative numbers. */
59 result |= cntlzw(~(a0 >> 32) & 0x7fff0000) >> 5;
61 set_fpscr_bit(FPSCR_VXCVI);
63 return result-1;