1 /* IEEE754 floating point arithmetic
2 * double precision: common utilities
5 * MIPS floating point support
6 * Copyright (C) 1994-2000 Algorithmics Ltd.
7 * Copyright (C) 2017 Imagination Technologies, Ltd.
8 * Author: Aleksandar Markovic <aleksandar.markovic@imgtec.com>
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License (Version 2) as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * You should have received a copy of the GNU General Public License along
23 #include "ieee754dp.h"
25 union ieee754dp ieee754dp_rint(union ieee754dp x)
40 if (xc == IEEE754_CLASS_SNAN)
41 return ieee754dp_nanxcpt(x);
43 if ((xc == IEEE754_CLASS_QNAN) ||
44 (xc == IEEE754_CLASS_INF) ||
45 (xc == IEEE754_CLASS_ZERO))
54 sticky = residue != 0;
57 residue = xm << (64 - DP_FBITS + xe);
58 round = (residue >> 63) != 0;
59 sticky = (residue << 1) != 0;
63 odd = (xm & 0x1) != 0x0;
65 switch (ieee754_csr.rm) {
66 case FPU_CSR_RN: /* toward nearest */
67 if (round && (sticky || odd))
70 case FPU_CSR_RZ: /* toward zero */
72 case FPU_CSR_RU: /* toward +infinity */
73 if ((round || sticky) && !xs)
76 case FPU_CSR_RD: /* toward -infinity */
77 if ((round || sticky) && xs)
83 ieee754_setcx(IEEE754_INEXACT);
85 ret = ieee754dp_flong(xm);