162306a36Sopenharmony_ci/* Machine-dependent software floating-point definitions.  PPC version.
262306a36Sopenharmony_ci   Copyright (C) 1997 Free Software Foundation, Inc.
362306a36Sopenharmony_ci   This file is part of the GNU C Library.
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci   The GNU C Library is free software; you can redistribute it and/or
662306a36Sopenharmony_ci   modify it under the terms of the GNU Library General Public License as
762306a36Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
862306a36Sopenharmony_ci   License, or (at your option) any later version.
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci   The GNU C Library is distributed in the hope that it will be useful,
1162306a36Sopenharmony_ci   but WITHOUT ANY WARRANTY; without even the implied warranty of
1262306a36Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1362306a36Sopenharmony_ci   Library General Public License for more details.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci   You should have received a copy of the GNU Library General Public
1662306a36Sopenharmony_ci   License along with the GNU C Library; see the file COPYING.LIB.  If
1762306a36Sopenharmony_ci   not, write to the Free Software Foundation, Inc.,
1862306a36Sopenharmony_ci   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci   Actually, this is a PPC (32bit) version, written based on the
2162306a36Sopenharmony_ci   i386, sparc, and sparc64 versions, by me,
2262306a36Sopenharmony_ci   Peter Maydell (pmaydell@chiark.greenend.org.uk).
2362306a36Sopenharmony_ci   Comments are by and large also mine, although they may be inaccurate.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci   In picking out asm fragments I've gone with the lowest common
2662306a36Sopenharmony_ci   denominator, which also happens to be the hardware I have :->
2762306a36Sopenharmony_ci   That is, a SPARC without hardware multiply and divide.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* basic word size definitions */
3162306a36Sopenharmony_ci#define _FP_W_TYPE_SIZE		32
3262306a36Sopenharmony_ci#define _FP_W_TYPE		unsigned int
3362306a36Sopenharmony_ci#define _FP_WS_TYPE		signed int
3462306a36Sopenharmony_ci#define _FP_I_TYPE		int
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define __ll_B			((UWtype) 1 << (W_TYPE_SIZE / 2))
3762306a36Sopenharmony_ci#define __ll_lowpart(t)		((UWtype) (t) & (__ll_B - 1))
3862306a36Sopenharmony_ci#define __ll_highpart(t)	((UWtype) (t) >> (W_TYPE_SIZE / 2))
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* You can optionally code some things like addition in asm. For
4162306a36Sopenharmony_ci * example, i386 defines __FP_FRAC_ADD_2 as asm. If you don't
4262306a36Sopenharmony_ci * then you get a fragment of C code [if you change an #ifdef 0
4362306a36Sopenharmony_ci * in op-2.h] or a call to add_ssaaaa (see below).
4462306a36Sopenharmony_ci * Good places to look for asm fragments to use are gcc and glibc.
4562306a36Sopenharmony_ci * gcc's longlong.h is useful.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* We need to know how to multiply and divide. If the host word size
4962306a36Sopenharmony_ci * is >= 2*fracbits you can use FP_MUL_MEAT_n_imm(t,R,X,Y) which
5062306a36Sopenharmony_ci * codes the multiply with whatever gcc does to 'a * b'.
5162306a36Sopenharmony_ci * _FP_MUL_MEAT_n_wide(t,R,X,Y,f) is used when you have an asm
5262306a36Sopenharmony_ci * function that can multiply two 1W values and get a 2W result.
5362306a36Sopenharmony_ci * Otherwise you're stuck with _FP_MUL_MEAT_n_hard(t,R,X,Y) which
5462306a36Sopenharmony_ci * does bitshifting to avoid overflow.
5562306a36Sopenharmony_ci * For division there is FP_DIV_MEAT_n_imm(t,R,X,Y,f) for word size
5662306a36Sopenharmony_ci * >= 2*fracbits, where f is either _FP_DIV_HELP_imm or
5762306a36Sopenharmony_ci * _FP_DIV_HELP_ldiv (see op-1.h).
5862306a36Sopenharmony_ci * _FP_DIV_MEAT_udiv() is if you have asm to do 2W/1W => (1W, 1W).
5962306a36Sopenharmony_ci * [GCC and glibc have longlong.h which has the asm macro udiv_qrnnd
6062306a36Sopenharmony_ci * to do this.]
6162306a36Sopenharmony_ci * In general, 'n' is the number of words required to hold the type,
6262306a36Sopenharmony_ci * and 't' is either S, D or Q for single/double/quad.
6362306a36Sopenharmony_ci *           -- PMM
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci/* Example: SPARC64:
6662306a36Sopenharmony_ci * #define _FP_MUL_MEAT_S(R,X,Y)	_FP_MUL_MEAT_1_imm(S,R,X,Y)
6762306a36Sopenharmony_ci * #define _FP_MUL_MEAT_D(R,X,Y)	_FP_MUL_MEAT_1_wide(D,R,X,Y,umul_ppmm)
6862306a36Sopenharmony_ci * #define _FP_MUL_MEAT_Q(R,X,Y)	_FP_MUL_MEAT_2_wide(Q,R,X,Y,umul_ppmm)
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci * #define _FP_DIV_MEAT_S(R,X,Y)	_FP_DIV_MEAT_1_imm(S,R,X,Y,_FP_DIV_HELP_imm)
7162306a36Sopenharmony_ci * #define _FP_DIV_MEAT_D(R,X,Y)	_FP_DIV_MEAT_1_udiv(D,R,X,Y)
7262306a36Sopenharmony_ci * #define _FP_DIV_MEAT_Q(R,X,Y)	_FP_DIV_MEAT_2_udiv_64(Q,R,X,Y)
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci * Example: i386:
7562306a36Sopenharmony_ci * #define _FP_MUL_MEAT_S(R,X,Y)   _FP_MUL_MEAT_1_wide(S,R,X,Y,_i386_mul_32_64)
7662306a36Sopenharmony_ci * #define _FP_MUL_MEAT_D(R,X,Y)   _FP_MUL_MEAT_2_wide(D,R,X,Y,_i386_mul_32_64)
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * #define _FP_DIV_MEAT_S(R,X,Y)   _FP_DIV_MEAT_1_udiv(S,R,X,Y,_i386_div_64_32)
7962306a36Sopenharmony_ci * #define _FP_DIV_MEAT_D(R,X,Y)   _FP_DIV_MEAT_2_udiv_64(D,R,X,Y)
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define _FP_MUL_MEAT_S(R,X,Y)   _FP_MUL_MEAT_1_wide(_FP_WFRACBITS_S,R,X,Y,umul_ppmm)
8362306a36Sopenharmony_ci#define _FP_MUL_MEAT_D(R,X,Y)   _FP_MUL_MEAT_2_wide(_FP_WFRACBITS_D,R,X,Y,umul_ppmm)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define _FP_DIV_MEAT_S(R,X,Y)	_FP_DIV_MEAT_1_udiv_norm(S,R,X,Y)
8662306a36Sopenharmony_ci#define _FP_DIV_MEAT_D(R,X,Y)	_FP_DIV_MEAT_2_udiv(D,R,X,Y)
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* These macros define what NaN looks like. They're supposed to expand to
8962306a36Sopenharmony_ci * a comma-separated set of 32bit unsigned ints that encode NaN.
9062306a36Sopenharmony_ci */
9162306a36Sopenharmony_ci#define _FP_NANFRAC_S		((_FP_QNANBIT_S << 1) - 1)
9262306a36Sopenharmony_ci#define _FP_NANFRAC_D		((_FP_QNANBIT_D << 1) - 1), -1
9362306a36Sopenharmony_ci#define _FP_NANFRAC_Q		((_FP_QNANBIT_Q << 1) - 1), -1, -1, -1
9462306a36Sopenharmony_ci#define _FP_NANSIGN_S		0
9562306a36Sopenharmony_ci#define _FP_NANSIGN_D		0
9662306a36Sopenharmony_ci#define _FP_NANSIGN_Q		0
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#define _FP_KEEPNANFRACP 1
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#ifdef FP_EX_BOOKE_E500_SPE
10162306a36Sopenharmony_ci#define FP_EX_INEXACT		(1 << 21)
10262306a36Sopenharmony_ci#define FP_EX_INVALID		(1 << 20)
10362306a36Sopenharmony_ci#define FP_EX_DIVZERO		(1 << 19)
10462306a36Sopenharmony_ci#define FP_EX_UNDERFLOW		(1 << 18)
10562306a36Sopenharmony_ci#define FP_EX_OVERFLOW		(1 << 17)
10662306a36Sopenharmony_ci#define FP_INHIBIT_RESULTS	0
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#define __FPU_FPSCR	(current->thread.spefscr)
10962306a36Sopenharmony_ci#define __FPU_ENABLED_EXC		\
11062306a36Sopenharmony_ci({					\
11162306a36Sopenharmony_ci	(__FPU_FPSCR >> 2) & 0x1f;	\
11262306a36Sopenharmony_ci})
11362306a36Sopenharmony_ci#else
11462306a36Sopenharmony_ci/* Exception flags.  We use the bit positions of the appropriate bits
11562306a36Sopenharmony_ci   in the FPSCR, which also correspond to the FE_* bits.  This makes
11662306a36Sopenharmony_ci   everything easier ;-).  */
11762306a36Sopenharmony_ci#define FP_EX_INVALID         (1 << (31 - 2))
11862306a36Sopenharmony_ci#define FP_EX_INVALID_SNAN	EFLAG_VXSNAN
11962306a36Sopenharmony_ci#define FP_EX_INVALID_ISI	EFLAG_VXISI
12062306a36Sopenharmony_ci#define FP_EX_INVALID_IDI	EFLAG_VXIDI
12162306a36Sopenharmony_ci#define FP_EX_INVALID_ZDZ	EFLAG_VXZDZ
12262306a36Sopenharmony_ci#define FP_EX_INVALID_IMZ	EFLAG_VXIMZ
12362306a36Sopenharmony_ci#define FP_EX_OVERFLOW        (1 << (31 - 3))
12462306a36Sopenharmony_ci#define FP_EX_UNDERFLOW       (1 << (31 - 4))
12562306a36Sopenharmony_ci#define FP_EX_DIVZERO         (1 << (31 - 5))
12662306a36Sopenharmony_ci#define FP_EX_INEXACT         (1 << (31 - 6))
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define __FPU_FPSCR	(current->thread.fp_state.fpscr)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/* We only actually write to the destination register
13162306a36Sopenharmony_ci * if exceptions signalled (if any) will not trap.
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_ci#define __FPU_ENABLED_EXC \
13462306a36Sopenharmony_ci({						\
13562306a36Sopenharmony_ci	(__FPU_FPSCR >> 3) & 0x1f;	\
13662306a36Sopenharmony_ci})
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci#endif
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/*
14162306a36Sopenharmony_ci * If one NaN is signaling and the other is not,
14262306a36Sopenharmony_ci * we choose that one, otherwise we choose X.
14362306a36Sopenharmony_ci */
14462306a36Sopenharmony_ci#define _FP_CHOOSENAN(fs, wc, R, X, Y, OP)			\
14562306a36Sopenharmony_ci  do {								\
14662306a36Sopenharmony_ci    if ((_FP_FRAC_HIGH_RAW_##fs(Y) & _FP_QNANBIT_##fs)		\
14762306a36Sopenharmony_ci	&& !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs))	\
14862306a36Sopenharmony_ci      {								\
14962306a36Sopenharmony_ci	R##_s = X##_s;						\
15062306a36Sopenharmony_ci	_FP_FRAC_COPY_##wc(R,X);				\
15162306a36Sopenharmony_ci      }								\
15262306a36Sopenharmony_ci    else							\
15362306a36Sopenharmony_ci      {								\
15462306a36Sopenharmony_ci	R##_s = Y##_s;						\
15562306a36Sopenharmony_ci	_FP_FRAC_COPY_##wc(R,Y);				\
15662306a36Sopenharmony_ci      }								\
15762306a36Sopenharmony_ci    R##_c = FP_CLS_NAN;						\
15862306a36Sopenharmony_ci  } while (0)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci#include <linux/kernel.h>
16262306a36Sopenharmony_ci#include <linux/sched.h>
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci#define __FPU_TRAP_P(bits) \
16562306a36Sopenharmony_ci	((__FPU_ENABLED_EXC & (bits)) != 0)
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci#define __FP_PACK_S(val,X)			\
16862306a36Sopenharmony_ci({  int __exc = _FP_PACK_CANONICAL(S,1,X);	\
16962306a36Sopenharmony_ci    if(!__exc || !__FPU_TRAP_P(__exc))		\
17062306a36Sopenharmony_ci        _FP_PACK_RAW_1_P(S,val,X);		\
17162306a36Sopenharmony_ci    __exc;					\
17262306a36Sopenharmony_ci})
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci#define __FP_PACK_D(val,X)			\
17562306a36Sopenharmony_ci   do {									\
17662306a36Sopenharmony_ci	_FP_PACK_CANONICAL(D, 2, X);					\
17762306a36Sopenharmony_ci	if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS))	\
17862306a36Sopenharmony_ci		_FP_PACK_RAW_2_P(D, val, X);				\
17962306a36Sopenharmony_ci   } while (0)
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci#define __FP_PACK_DS(val,X)							\
18262306a36Sopenharmony_ci   do {										\
18362306a36Sopenharmony_ci	   FP_DECL_S(__X);							\
18462306a36Sopenharmony_ci	   FP_CONV(S, D, 1, 2, __X, X);						\
18562306a36Sopenharmony_ci	   _FP_PACK_CANONICAL(S, 1, __X);					\
18662306a36Sopenharmony_ci	   if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS)) {	\
18762306a36Sopenharmony_ci		   _FP_UNPACK_CANONICAL(S, 1, __X);				\
18862306a36Sopenharmony_ci		   FP_CONV(D, S, 2, 1, X, __X);					\
18962306a36Sopenharmony_ci		   _FP_PACK_CANONICAL(D, 2, X);					\
19062306a36Sopenharmony_ci		   if (!FP_CUR_EXCEPTIONS || !__FPU_TRAP_P(FP_CUR_EXCEPTIONS))	\
19162306a36Sopenharmony_ci		   _FP_PACK_RAW_2_P(D, val, X);					\
19262306a36Sopenharmony_ci	   }									\
19362306a36Sopenharmony_ci   } while (0)
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci/* Obtain the current rounding mode. */
19662306a36Sopenharmony_ci#define FP_ROUNDMODE			\
19762306a36Sopenharmony_ci({					\
19862306a36Sopenharmony_ci	__FPU_FPSCR & 0x3;		\
19962306a36Sopenharmony_ci})
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci/* the asm fragments go here: all these are taken from glibc-2.0.5's
20262306a36Sopenharmony_ci * stdlib/longlong.h
20362306a36Sopenharmony_ci */
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci#include <linux/types.h>
20662306a36Sopenharmony_ci#include <asm/byteorder.h>
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci/* add_ssaaaa is used in op-2.h and should be equivalent to
20962306a36Sopenharmony_ci * #define add_ssaaaa(sh,sl,ah,al,bh,bl) (sh = ah+bh+ (( sl = al+bl) < al))
21062306a36Sopenharmony_ci * add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
21162306a36Sopenharmony_ci * high_addend_2, low_addend_2) adds two UWtype integers, composed by
21262306a36Sopenharmony_ci * HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
21362306a36Sopenharmony_ci * respectively.  The result is placed in HIGH_SUM and LOW_SUM.  Overflow
21462306a36Sopenharmony_ci * (i.e. carry out) is not stored anywhere, and is lost.
21562306a36Sopenharmony_ci */
21662306a36Sopenharmony_ci#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
21762306a36Sopenharmony_ci  do {									\
21862306a36Sopenharmony_ci    if (__builtin_constant_p (bh) && (bh) == 0)				\
21962306a36Sopenharmony_ci      __asm__ ("add%I4c %1,%3,%4\n\taddze %0,%2"		\
22062306a36Sopenharmony_ci	     : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
22162306a36Sopenharmony_ci    else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0)		\
22262306a36Sopenharmony_ci      __asm__ ("add%I4c %1,%3,%4\n\taddme %0,%2"		\
22362306a36Sopenharmony_ci	     : "=r" (sh), "=&r" (sl) : "r" (ah), "%r" (al), "rI" (bl));\
22462306a36Sopenharmony_ci    else								\
22562306a36Sopenharmony_ci      __asm__ ("add%I5c %1,%4,%5\n\tadde %0,%2,%3"		\
22662306a36Sopenharmony_ci	     : "=r" (sh), "=&r" (sl)					\
22762306a36Sopenharmony_ci	     : "%r" (ah), "r" (bh), "%r" (al), "rI" (bl));		\
22862306a36Sopenharmony_ci  } while (0)
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci/* sub_ddmmss is used in op-2.h and udivmodti4.c and should be equivalent to
23162306a36Sopenharmony_ci * #define sub_ddmmss(sh, sl, ah, al, bh, bl) (sh = ah-bh - ((sl = al-bl) > al))
23262306a36Sopenharmony_ci * sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
23362306a36Sopenharmony_ci * high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
23462306a36Sopenharmony_ci * composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
23562306a36Sopenharmony_ci * LOW_SUBTRAHEND_2 respectively.  The result is placed in HIGH_DIFFERENCE
23662306a36Sopenharmony_ci * and LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
23762306a36Sopenharmony_ci * and is lost.
23862306a36Sopenharmony_ci */
23962306a36Sopenharmony_ci#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
24062306a36Sopenharmony_ci  do {									\
24162306a36Sopenharmony_ci    if (__builtin_constant_p (ah) && (ah) == 0)				\
24262306a36Sopenharmony_ci      __asm__ ("subf%I3c %1,%4,%3\n\tsubfze %0,%2"	\
24362306a36Sopenharmony_ci	       : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
24462306a36Sopenharmony_ci    else if (__builtin_constant_p (ah) && (ah) == ~(USItype) 0)		\
24562306a36Sopenharmony_ci      __asm__ ("subf%I3c %1,%4,%3\n\tsubfme %0,%2"	\
24662306a36Sopenharmony_ci	       : "=r" (sh), "=&r" (sl) : "r" (bh), "rI" (al), "r" (bl));\
24762306a36Sopenharmony_ci    else if (__builtin_constant_p (bh) && (bh) == 0)			\
24862306a36Sopenharmony_ci      __asm__ ("subf%I3c %1,%4,%3\n\taddme %0,%2"		\
24962306a36Sopenharmony_ci	       : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
25062306a36Sopenharmony_ci    else if (__builtin_constant_p (bh) && (bh) == ~(USItype) 0)		\
25162306a36Sopenharmony_ci      __asm__ ("subf%I3c %1,%4,%3\n\taddze %0,%2"		\
25262306a36Sopenharmony_ci	       : "=r" (sh), "=&r" (sl) : "r" (ah), "rI" (al), "r" (bl));\
25362306a36Sopenharmony_ci    else								\
25462306a36Sopenharmony_ci      __asm__ ("subf%I4c %1,%5,%4\n\tsubfe %0,%3,%2"	\
25562306a36Sopenharmony_ci	       : "=r" (sh), "=&r" (sl)					\
25662306a36Sopenharmony_ci	       : "r" (ah), "r" (bh), "rI" (al), "r" (bl));		\
25762306a36Sopenharmony_ci  } while (0)
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci/* asm fragments for mul and div */
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci/* umul_ppmm(high_prod, low_prod, multipler, multiplicand) multiplies two
26262306a36Sopenharmony_ci * UWtype integers MULTIPLER and MULTIPLICAND, and generates a two UWtype
26362306a36Sopenharmony_ci * word product in HIGH_PROD and LOW_PROD.
26462306a36Sopenharmony_ci */
26562306a36Sopenharmony_ci#define umul_ppmm(ph, pl, m0, m1) \
26662306a36Sopenharmony_ci  do {									\
26762306a36Sopenharmony_ci    USItype __m0 = (m0), __m1 = (m1);					\
26862306a36Sopenharmony_ci    __asm__ ("mulhwu %0,%1,%2" : "=r" (ph) : "%r" (m0), "r" (m1));	\
26962306a36Sopenharmony_ci    (pl) = __m0 * __m1;							\
27062306a36Sopenharmony_ci  } while (0)
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci/* udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
27362306a36Sopenharmony_ci * denominator) divides a UDWtype, composed by the UWtype integers
27462306a36Sopenharmony_ci * HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
27562306a36Sopenharmony_ci * in QUOTIENT and the remainder in REMAINDER.  HIGH_NUMERATOR must be less
27662306a36Sopenharmony_ci * than DENOMINATOR for correct operation.  If, in addition, the most
27762306a36Sopenharmony_ci * significant bit of DENOMINATOR must be 1, then the pre-processor symbol
27862306a36Sopenharmony_ci * UDIV_NEEDS_NORMALIZATION is defined to 1.
27962306a36Sopenharmony_ci */
28062306a36Sopenharmony_ci#define udiv_qrnnd(q, r, n1, n0, d) \
28162306a36Sopenharmony_ci  do {									\
28262306a36Sopenharmony_ci    UWtype __d1, __d0, __q1, __q0;					\
28362306a36Sopenharmony_ci    UWtype __r1, __r0, __m;						\
28462306a36Sopenharmony_ci    __d1 = __ll_highpart (d);						\
28562306a36Sopenharmony_ci    __d0 = __ll_lowpart (d);						\
28662306a36Sopenharmony_ci									\
28762306a36Sopenharmony_ci    __r1 = (n1) % __d1;							\
28862306a36Sopenharmony_ci    __q1 = (n1) / __d1;							\
28962306a36Sopenharmony_ci    __m = (UWtype) __q1 * __d0;						\
29062306a36Sopenharmony_ci    __r1 = __r1 * __ll_B | __ll_highpart (n0);				\
29162306a36Sopenharmony_ci    if (__r1 < __m)							\
29262306a36Sopenharmony_ci      {									\
29362306a36Sopenharmony_ci	__q1--, __r1 += (d);						\
29462306a36Sopenharmony_ci	if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
29562306a36Sopenharmony_ci	  if (__r1 < __m)						\
29662306a36Sopenharmony_ci	    __q1--, __r1 += (d);					\
29762306a36Sopenharmony_ci      }									\
29862306a36Sopenharmony_ci    __r1 -= __m;							\
29962306a36Sopenharmony_ci									\
30062306a36Sopenharmony_ci    __r0 = __r1 % __d1;							\
30162306a36Sopenharmony_ci    __q0 = __r1 / __d1;							\
30262306a36Sopenharmony_ci    __m = (UWtype) __q0 * __d0;						\
30362306a36Sopenharmony_ci    __r0 = __r0 * __ll_B | __ll_lowpart (n0);				\
30462306a36Sopenharmony_ci    if (__r0 < __m)							\
30562306a36Sopenharmony_ci      {									\
30662306a36Sopenharmony_ci	__q0--, __r0 += (d);						\
30762306a36Sopenharmony_ci	if (__r0 >= (d))						\
30862306a36Sopenharmony_ci	  if (__r0 < __m)						\
30962306a36Sopenharmony_ci	    __q0--, __r0 += (d);					\
31062306a36Sopenharmony_ci      }									\
31162306a36Sopenharmony_ci    __r0 -= __m;							\
31262306a36Sopenharmony_ci									\
31362306a36Sopenharmony_ci    (q) = (UWtype) __q1 * __ll_B | __q0;				\
31462306a36Sopenharmony_ci    (r) = __r0;								\
31562306a36Sopenharmony_ci  } while (0)
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci#define UDIV_NEEDS_NORMALIZATION 1
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci#define abort()								\
32062306a36Sopenharmony_ci	return 0
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci#ifdef __BIG_ENDIAN
32362306a36Sopenharmony_ci#define __BYTE_ORDER __BIG_ENDIAN
32462306a36Sopenharmony_ci#else
32562306a36Sopenharmony_ci#define __BYTE_ORDER __LITTLE_ENDIAN
32662306a36Sopenharmony_ci#endif
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci/* Exception flags. */
32962306a36Sopenharmony_ci#define EFLAG_INVALID		(1 << (31 - 2))
33062306a36Sopenharmony_ci#define EFLAG_OVERFLOW		(1 << (31 - 3))
33162306a36Sopenharmony_ci#define EFLAG_UNDERFLOW		(1 << (31 - 4))
33262306a36Sopenharmony_ci#define EFLAG_DIVZERO		(1 << (31 - 5))
33362306a36Sopenharmony_ci#define EFLAG_INEXACT		(1 << (31 - 6))
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci#define EFLAG_VXSNAN		(1 << (31 - 7))
33662306a36Sopenharmony_ci#define EFLAG_VXISI		(1 << (31 - 8))
33762306a36Sopenharmony_ci#define EFLAG_VXIDI		(1 << (31 - 9))
33862306a36Sopenharmony_ci#define EFLAG_VXZDZ		(1 << (31 - 10))
33962306a36Sopenharmony_ci#define EFLAG_VXIMZ		(1 << (31 - 11))
34062306a36Sopenharmony_ci#define EFLAG_VXVC		(1 << (31 - 12))
34162306a36Sopenharmony_ci#define EFLAG_VXSOFT		(1 << (31 - 21))
34262306a36Sopenharmony_ci#define EFLAG_VXSQRT		(1 << (31 - 22))
34362306a36Sopenharmony_ci#define EFLAG_VXCVI		(1 << (31 - 23))
344