xref: /kernel/linux/linux-6.6/include/math-emu/op-1.h (revision 62306a36)
162306a36Sopenharmony_ci/* Software floating-point emulation.
262306a36Sopenharmony_ci   Basic one-word fraction declaration and manipulation.
362306a36Sopenharmony_ci   Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
462306a36Sopenharmony_ci   This file is part of the GNU C Library.
562306a36Sopenharmony_ci   Contributed by Richard Henderson (rth@cygnus.com),
662306a36Sopenharmony_ci		  Jakub Jelinek (jj@ultra.linux.cz),
762306a36Sopenharmony_ci		  David S. Miller (davem@redhat.com) and
862306a36Sopenharmony_ci		  Peter Maydell (pmaydell@chiark.greenend.org.uk).
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci   The GNU C Library is free software; you can redistribute it and/or
1162306a36Sopenharmony_ci   modify it under the terms of the GNU Library General Public License as
1262306a36Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
1362306a36Sopenharmony_ci   License, or (at your option) any later version.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci   The GNU C Library is distributed in the hope that it will be useful,
1662306a36Sopenharmony_ci   but WITHOUT ANY WARRANTY; without even the implied warranty of
1762306a36Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1862306a36Sopenharmony_ci   Library General Public License for more details.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci   You should have received a copy of the GNU Library General Public
2162306a36Sopenharmony_ci   License along with the GNU C Library; see the file COPYING.LIB.  If
2262306a36Sopenharmony_ci   not, write to the Free Software Foundation, Inc.,
2362306a36Sopenharmony_ci   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#ifndef    __MATH_EMU_OP_1_H__
2662306a36Sopenharmony_ci#define    __MATH_EMU_OP_1_H__
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define _FP_FRAC_DECL_1(X)	_FP_W_TYPE X##_f=0
2962306a36Sopenharmony_ci#define _FP_FRAC_COPY_1(D,S)	(D##_f = S##_f)
3062306a36Sopenharmony_ci#define _FP_FRAC_SET_1(X,I)	(X##_f = I)
3162306a36Sopenharmony_ci#define _FP_FRAC_HIGH_1(X)	(X##_f)
3262306a36Sopenharmony_ci#define _FP_FRAC_LOW_1(X)	(X##_f)
3362306a36Sopenharmony_ci#define _FP_FRAC_WORD_1(X,w)	(X##_f)
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#define _FP_FRAC_ADDI_1(X,I)	(X##_f += I)
3662306a36Sopenharmony_ci#define _FP_FRAC_SLL_1(X,N)			\
3762306a36Sopenharmony_ci  do {						\
3862306a36Sopenharmony_ci    if (__builtin_constant_p(N) && (N) == 1)	\
3962306a36Sopenharmony_ci      X##_f += X##_f;				\
4062306a36Sopenharmony_ci    else					\
4162306a36Sopenharmony_ci      X##_f <<= (N);				\
4262306a36Sopenharmony_ci  } while (0)
4362306a36Sopenharmony_ci#define _FP_FRAC_SRL_1(X,N)	(X##_f >>= N)
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* Right shift with sticky-lsb.  */
4662306a36Sopenharmony_ci#define _FP_FRAC_SRS_1(X,N,sz)	__FP_FRAC_SRS_1(X##_f, N, sz)
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#define __FP_FRAC_SRS_1(X,N,sz)						\
4962306a36Sopenharmony_ci   (X = (X >> (N) | (__builtin_constant_p(N) && (N) == 1		\
5062306a36Sopenharmony_ci		     ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci#define _FP_FRAC_ADD_1(R,X,Y)	(R##_f = X##_f + Y##_f)
5362306a36Sopenharmony_ci#define _FP_FRAC_SUB_1(R,X,Y)	(R##_f = X##_f - Y##_f)
5462306a36Sopenharmony_ci#define _FP_FRAC_DEC_1(X,Y)	(X##_f -= Y##_f)
5562306a36Sopenharmony_ci#define _FP_FRAC_CLZ_1(z, X)	__FP_CLZ(z, X##_f)
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* Predicates */
5862306a36Sopenharmony_ci#define _FP_FRAC_NEGP_1(X)	((_FP_WS_TYPE)X##_f < 0)
5962306a36Sopenharmony_ci#define _FP_FRAC_ZEROP_1(X)	(X##_f == 0)
6062306a36Sopenharmony_ci#define _FP_FRAC_OVERP_1(fs,X)	(X##_f & _FP_OVERFLOW_##fs)
6162306a36Sopenharmony_ci#define _FP_FRAC_CLEAR_OVERP_1(fs,X)	(X##_f &= ~_FP_OVERFLOW_##fs)
6262306a36Sopenharmony_ci#define _FP_FRAC_EQ_1(X, Y)	(X##_f == Y##_f)
6362306a36Sopenharmony_ci#define _FP_FRAC_GE_1(X, Y)	(X##_f >= Y##_f)
6462306a36Sopenharmony_ci#define _FP_FRAC_GT_1(X, Y)	(X##_f > Y##_f)
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define _FP_ZEROFRAC_1		0
6762306a36Sopenharmony_ci#define _FP_MINFRAC_1		1
6862306a36Sopenharmony_ci#define _FP_MAXFRAC_1		(~(_FP_WS_TYPE)0)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/*
7162306a36Sopenharmony_ci * Unpack the raw bits of a native fp value.  Do not classify or
7262306a36Sopenharmony_ci * normalize the data.
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define _FP_UNPACK_RAW_1(fs, X, val)				\
7662306a36Sopenharmony_ci  do {								\
7762306a36Sopenharmony_ci    union _FP_UNION_##fs _flo; _flo.flt = (val);		\
7862306a36Sopenharmony_ci								\
7962306a36Sopenharmony_ci    X##_f = _flo.bits.frac;					\
8062306a36Sopenharmony_ci    X##_e = _flo.bits.exp;					\
8162306a36Sopenharmony_ci    X##_s = _flo.bits.sign;					\
8262306a36Sopenharmony_ci  } while (0)
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#define _FP_UNPACK_RAW_1_P(fs, X, val)				\
8562306a36Sopenharmony_ci  do {								\
8662306a36Sopenharmony_ci    union _FP_UNION_##fs *_flo =				\
8762306a36Sopenharmony_ci      (union _FP_UNION_##fs *)(val);				\
8862306a36Sopenharmony_ci								\
8962306a36Sopenharmony_ci    X##_f = _flo->bits.frac;					\
9062306a36Sopenharmony_ci    X##_e = _flo->bits.exp;					\
9162306a36Sopenharmony_ci    X##_s = _flo->bits.sign;					\
9262306a36Sopenharmony_ci  } while (0)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Repack the raw bits of a native fp value.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#define _FP_PACK_RAW_1(fs, val, X)				\
9962306a36Sopenharmony_ci  do {								\
10062306a36Sopenharmony_ci    union _FP_UNION_##fs _flo;					\
10162306a36Sopenharmony_ci								\
10262306a36Sopenharmony_ci    _flo.bits.frac = X##_f;					\
10362306a36Sopenharmony_ci    _flo.bits.exp  = X##_e;					\
10462306a36Sopenharmony_ci    _flo.bits.sign = X##_s;					\
10562306a36Sopenharmony_ci								\
10662306a36Sopenharmony_ci    (val) = _flo.flt;						\
10762306a36Sopenharmony_ci  } while (0)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define _FP_PACK_RAW_1_P(fs, val, X)				\
11062306a36Sopenharmony_ci  do {								\
11162306a36Sopenharmony_ci    union _FP_UNION_##fs *_flo =				\
11262306a36Sopenharmony_ci      (union _FP_UNION_##fs *)(val);				\
11362306a36Sopenharmony_ci								\
11462306a36Sopenharmony_ci    _flo->bits.frac = X##_f;					\
11562306a36Sopenharmony_ci    _flo->bits.exp  = X##_e;					\
11662306a36Sopenharmony_ci    _flo->bits.sign = X##_s;					\
11762306a36Sopenharmony_ci  } while (0)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/*
12162306a36Sopenharmony_ci * Multiplication algorithms:
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci/* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
12562306a36Sopenharmony_ci   multiplication immediately.  */
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y)				\
12862306a36Sopenharmony_ci  do {									\
12962306a36Sopenharmony_ci    R##_f = X##_f * Y##_f;						\
13062306a36Sopenharmony_ci    /* Normalize since we know where the msb of the multiplicands	\
13162306a36Sopenharmony_ci       were (bit B), we know that the msb of the of the product is	\
13262306a36Sopenharmony_ci       at either 2B or 2B-1.  */					\
13362306a36Sopenharmony_ci    _FP_FRAC_SRS_1(R, wfracbits-1, 2*wfracbits);			\
13462306a36Sopenharmony_ci  } while (0)
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci/* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci#define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit)			\
13962306a36Sopenharmony_ci  do {									\
14062306a36Sopenharmony_ci    _FP_W_TYPE _Z_f0, _Z_f1;						\
14162306a36Sopenharmony_ci    doit(_Z_f1, _Z_f0, X##_f, Y##_f);					\
14262306a36Sopenharmony_ci    /* Normalize since we know where the msb of the multiplicands	\
14362306a36Sopenharmony_ci       were (bit B), we know that the msb of the of the product is	\
14462306a36Sopenharmony_ci       at either 2B or 2B-1.  */					\
14562306a36Sopenharmony_ci    _FP_FRAC_SRS_2(_Z, wfracbits-1, 2*wfracbits);			\
14662306a36Sopenharmony_ci    R##_f = _Z_f0;							\
14762306a36Sopenharmony_ci  } while (0)
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/* Finally, a simple widening multiply algorithm.  What fun!  */
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci#define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y)				\
15262306a36Sopenharmony_ci  do {									\
15362306a36Sopenharmony_ci    _FP_W_TYPE _xh, _xl, _yh, _yl, _z_f0, _z_f1, _a_f0, _a_f1;		\
15462306a36Sopenharmony_ci									\
15562306a36Sopenharmony_ci    /* split the words in half */					\
15662306a36Sopenharmony_ci    _xh = X##_f >> (_FP_W_TYPE_SIZE/2);					\
15762306a36Sopenharmony_ci    _xl = X##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);		\
15862306a36Sopenharmony_ci    _yh = Y##_f >> (_FP_W_TYPE_SIZE/2);					\
15962306a36Sopenharmony_ci    _yl = Y##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);		\
16062306a36Sopenharmony_ci									\
16162306a36Sopenharmony_ci    /* multiply the pieces */						\
16262306a36Sopenharmony_ci    _z_f0 = _xl * _yl;							\
16362306a36Sopenharmony_ci    _a_f0 = _xh * _yl;							\
16462306a36Sopenharmony_ci    _a_f1 = _xl * _yh;							\
16562306a36Sopenharmony_ci    _z_f1 = _xh * _yh;							\
16662306a36Sopenharmony_ci									\
16762306a36Sopenharmony_ci    /* reassemble into two full words */				\
16862306a36Sopenharmony_ci    if ((_a_f0 += _a_f1) < _a_f1)					\
16962306a36Sopenharmony_ci      _z_f1 += (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2);			\
17062306a36Sopenharmony_ci    _a_f1 = _a_f0 >> (_FP_W_TYPE_SIZE/2);				\
17162306a36Sopenharmony_ci    _a_f0 = _a_f0 << (_FP_W_TYPE_SIZE/2);				\
17262306a36Sopenharmony_ci    _FP_FRAC_ADD_2(_z, _z, _a);						\
17362306a36Sopenharmony_ci									\
17462306a36Sopenharmony_ci    /* normalize */							\
17562306a36Sopenharmony_ci    _FP_FRAC_SRS_2(_z, wfracbits - 1, 2*wfracbits);			\
17662306a36Sopenharmony_ci    R##_f = _z_f0;							\
17762306a36Sopenharmony_ci  } while (0)
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/*
18162306a36Sopenharmony_ci * Division algorithms:
18262306a36Sopenharmony_ci */
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
18562306a36Sopenharmony_ci   division immediately.  Give this macro either _FP_DIV_HELP_imm for
18662306a36Sopenharmony_ci   C primitives or _FP_DIV_HELP_ldiv for the ISO function.  Which you
18762306a36Sopenharmony_ci   choose will depend on what the compiler does with divrem4.  */
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit)		\
19062306a36Sopenharmony_ci  do {							\
19162306a36Sopenharmony_ci    _FP_W_TYPE _q, _r;					\
19262306a36Sopenharmony_ci    X##_f <<= (X##_f < Y##_f				\
19362306a36Sopenharmony_ci	       ? R##_e--, _FP_WFRACBITS_##fs		\
19462306a36Sopenharmony_ci	       : _FP_WFRACBITS_##fs - 1);		\
19562306a36Sopenharmony_ci    doit(_q, _r, X##_f, Y##_f);				\
19662306a36Sopenharmony_ci    R##_f = _q | (_r != 0);				\
19762306a36Sopenharmony_ci  } while (0)
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
20062306a36Sopenharmony_ci   that may be useful in this situation.  This first is for a primitive
20162306a36Sopenharmony_ci   that requires normalization, the second for one that does not.  Look
20262306a36Sopenharmony_ci   for UDIV_NEEDS_NORMALIZATION to tell which your machine needs.  */
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci#define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y)				\
20562306a36Sopenharmony_ci  do {									\
20662306a36Sopenharmony_ci    _FP_W_TYPE _nh, _nl, _q, _r, _y;					\
20762306a36Sopenharmony_ci									\
20862306a36Sopenharmony_ci    /* Normalize Y -- i.e. make the most significant bit set.  */	\
20962306a36Sopenharmony_ci    _y = Y##_f << _FP_WFRACXBITS_##fs;					\
21062306a36Sopenharmony_ci									\
21162306a36Sopenharmony_ci    /* Shift X op correspondingly high, that is, up one full word.  */	\
21262306a36Sopenharmony_ci    if (X##_f < Y##_f)							\
21362306a36Sopenharmony_ci      {									\
21462306a36Sopenharmony_ci	R##_e--;							\
21562306a36Sopenharmony_ci	_nl = 0;							\
21662306a36Sopenharmony_ci	_nh = X##_f;							\
21762306a36Sopenharmony_ci      }									\
21862306a36Sopenharmony_ci    else								\
21962306a36Sopenharmony_ci      {									\
22062306a36Sopenharmony_ci	_nl = X##_f << (_FP_W_TYPE_SIZE - 1);				\
22162306a36Sopenharmony_ci	_nh = X##_f >> 1;						\
22262306a36Sopenharmony_ci      }									\
22362306a36Sopenharmony_ci    									\
22462306a36Sopenharmony_ci    udiv_qrnnd(_q, _r, _nh, _nl, _y);					\
22562306a36Sopenharmony_ci    R##_f = _q | (_r != 0);						\
22662306a36Sopenharmony_ci  } while (0)
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#define _FP_DIV_MEAT_1_udiv(fs, R, X, Y)		\
22962306a36Sopenharmony_ci  do {							\
23062306a36Sopenharmony_ci    _FP_W_TYPE _nh, _nl, _q, _r;			\
23162306a36Sopenharmony_ci    if (X##_f < Y##_f)					\
23262306a36Sopenharmony_ci      {							\
23362306a36Sopenharmony_ci	R##_e--;					\
23462306a36Sopenharmony_ci	_nl = X##_f << _FP_WFRACBITS_##fs;		\
23562306a36Sopenharmony_ci	_nh = X##_f >> _FP_WFRACXBITS_##fs;		\
23662306a36Sopenharmony_ci      }							\
23762306a36Sopenharmony_ci    else						\
23862306a36Sopenharmony_ci      {							\
23962306a36Sopenharmony_ci	_nl = X##_f << (_FP_WFRACBITS_##fs - 1);	\
24062306a36Sopenharmony_ci	_nh = X##_f >> (_FP_WFRACXBITS_##fs + 1);	\
24162306a36Sopenharmony_ci      }							\
24262306a36Sopenharmony_ci    udiv_qrnnd(_q, _r, _nh, _nl, Y##_f);		\
24362306a36Sopenharmony_ci    R##_f = _q | (_r != 0);				\
24462306a36Sopenharmony_ci  } while (0)
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci/*
24862306a36Sopenharmony_ci * Square root algorithms:
24962306a36Sopenharmony_ci * We have just one right now, maybe Newton approximation
25062306a36Sopenharmony_ci * should be added for those machines where division is fast.
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci#define _FP_SQRT_MEAT_1(R, S, T, X, q)			\
25462306a36Sopenharmony_ci  do {							\
25562306a36Sopenharmony_ci    while (q != _FP_WORK_ROUND)				\
25662306a36Sopenharmony_ci      {							\
25762306a36Sopenharmony_ci        T##_f = S##_f + q;				\
25862306a36Sopenharmony_ci        if (T##_f <= X##_f)				\
25962306a36Sopenharmony_ci          {						\
26062306a36Sopenharmony_ci            S##_f = T##_f + q;				\
26162306a36Sopenharmony_ci            X##_f -= T##_f;				\
26262306a36Sopenharmony_ci            R##_f += q;					\
26362306a36Sopenharmony_ci          }						\
26462306a36Sopenharmony_ci        _FP_FRAC_SLL_1(X, 1);				\
26562306a36Sopenharmony_ci        q >>= 1;					\
26662306a36Sopenharmony_ci      }							\
26762306a36Sopenharmony_ci    if (X##_f)						\
26862306a36Sopenharmony_ci      {							\
26962306a36Sopenharmony_ci	if (S##_f < X##_f)				\
27062306a36Sopenharmony_ci	  R##_f |= _FP_WORK_ROUND;			\
27162306a36Sopenharmony_ci	R##_f |= _FP_WORK_STICKY;			\
27262306a36Sopenharmony_ci      }							\
27362306a36Sopenharmony_ci  } while (0)
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci/*
27662306a36Sopenharmony_ci * Assembly/disassembly for converting to/from integral types.
27762306a36Sopenharmony_ci * No shifting or overflow handled here.
27862306a36Sopenharmony_ci */
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci#define _FP_FRAC_ASSEMBLE_1(r, X, rsize)	(r = X##_f)
28162306a36Sopenharmony_ci#define _FP_FRAC_DISASSEMBLE_1(X, r, rsize)	(X##_f = r)
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci/*
28562306a36Sopenharmony_ci * Convert FP values between word sizes
28662306a36Sopenharmony_ci */
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci#define _FP_FRAC_CONV_1_1(dfs, sfs, D, S)				\
28962306a36Sopenharmony_ci  do {									\
29062306a36Sopenharmony_ci    D##_f = S##_f;							\
29162306a36Sopenharmony_ci    if (_FP_WFRACBITS_##sfs > _FP_WFRACBITS_##dfs)			\
29262306a36Sopenharmony_ci      {									\
29362306a36Sopenharmony_ci	if (S##_c != FP_CLS_NAN)					\
29462306a36Sopenharmony_ci	  _FP_FRAC_SRS_1(D, (_FP_WFRACBITS_##sfs-_FP_WFRACBITS_##dfs),	\
29562306a36Sopenharmony_ci			 _FP_WFRACBITS_##sfs);				\
29662306a36Sopenharmony_ci	else								\
29762306a36Sopenharmony_ci	  _FP_FRAC_SRL_1(D, (_FP_WFRACBITS_##sfs-_FP_WFRACBITS_##dfs));	\
29862306a36Sopenharmony_ci      }									\
29962306a36Sopenharmony_ci    else								\
30062306a36Sopenharmony_ci      D##_f <<= _FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs;		\
30162306a36Sopenharmony_ci  } while (0)
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci#endif /* __MATH_EMU_OP_1_H__ */
304