1 /* 128-bit floating point to 32-bit fixed point signed 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 /* int _q_qtoi(const long double *a);
23 Convert 'a' to signed int by truncation.
26 NaN: raise VXCVI, return 0x80000000
27 SNaN: raise VXSNAN, VXCVI, and return 0x80000000
28 >= 2^32: raise VXCVI, return 0x7fffffff
29 <= -2^32: raise VXCVI, return 0x80000000
33 __q_qtoi(const unsigned long long a
[2])
36 unsigned long long a0
;
39 /* 'sgn' is -1 if 'a' is negative, 0 if positive. */
40 sgn
= (long long)a0
>> 63;
41 ax
= (a0
>> 48 & 0x7fff) - 16383;
42 /* Deal with non-special cases. */
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. The '^ sgn) - sgn' part is
47 equivalent in this case to multiplication by 'sgn', but usually
49 return (((unsigned)(a0
>> 17 | 0x80000000) >> 31-ax
& -(ax
>> 31))
52 /* Check for SNaN, if so raise VXSNAN. */
53 if ((a0
>> 32 & 0x7fff8000) == 0x7fff0000
54 && (a
[1] | a0
& 0x00007fffffffffffULL
) != 0)
55 set_fpscr_bit(FPSCR_VXSNAN
);
57 /* Treat all NaNs as large negative numbers. */
58 sgn
|= -cntlzw(~(a0
>> 32) & 0x7fff0000) >> 5;
60 /* All the specials raise VXCVI. */
61 set_fpscr_bit(FPSCR_VXCVI
);
63 /* Return 0x7fffffff (if a < 0) or 0x80000000 (otherwise). */
64 return sgn
^ 0x7fffffff;