sfp-machine.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * BK Id: SCCS/s.sfp-machine.h 1.5 05/17/01 18:14:23 cort
  3.  */
  4. /* Machine-dependent software floating-point definitions.  PPC version.
  5.    Copyright (C) 1997 Free Software Foundation, Inc.
  6.    This file is part of the GNU C Library.
  7.    The GNU C Library is free software; you can redistribute it and/or
  8.    modify it under the terms of the GNU Library General Public License as
  9.    published by the Free Software Foundation; either version 2 of the
  10.    License, or (at your option) any later version.
  11.    The GNU C Library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    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
  17.    not, write to the Free Software Foundation, Inc.,
  18.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
  19.    Actually, this is a PPC (32bit) version, written based on the
  20.    i386, sparc, and sparc64 versions, by me, 
  21.    Peter Maydell (pmaydell@chiark.greenend.org.uk).
  22.    Comments are by and large also mine, although they may be inaccurate.
  23.    In picking out asm fragments I've gone with the lowest common
  24.    denominator, which also happens to be the hardware I have :->
  25.    That is, a SPARC without hardware multiply and divide.
  26.  */
  27. /* basic word size definitions */
  28. #define _FP_W_TYPE_SIZE 32
  29. #define _FP_W_TYPE unsigned long
  30. #define _FP_WS_TYPE signed long
  31. #define _FP_I_TYPE long
  32. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  33. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  34. #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
  35. /* You can optionally code some things like addition in asm. For
  36.  * example, i386 defines __FP_FRAC_ADD_2 as asm. If you don't
  37.  * then you get a fragment of C code [if you change an #ifdef 0
  38.  * in op-2.h] or a call to add_ssaaaa (see below).
  39.  * Good places to look for asm fragments to use are gcc and glibc.
  40.  * gcc's longlong.h is useful.
  41.  */
  42. /* We need to know how to multiply and divide. If the host word size
  43.  * is >= 2*fracbits you can use FP_MUL_MEAT_n_imm(t,R,X,Y) which
  44.  * codes the multiply with whatever gcc does to 'a * b'.
  45.  * _FP_MUL_MEAT_n_wide(t,R,X,Y,f) is used when you have an asm 
  46.  * function that can multiply two 1W values and get a 2W result. 
  47.  * Otherwise you're stuck with _FP_MUL_MEAT_n_hard(t,R,X,Y) which
  48.  * does bitshifting to avoid overflow.
  49.  * For division there is FP_DIV_MEAT_n_imm(t,R,X,Y,f) for word size
  50.  * >= 2*fracbits, where f is either _FP_DIV_HELP_imm or 
  51.  * _FP_DIV_HELP_ldiv (see op-1.h).
  52.  * _FP_DIV_MEAT_udiv() is if you have asm to do 2W/1W => (1W, 1W).
  53.  * [GCC and glibc have longlong.h which has the asm macro udiv_qrnnd
  54.  * to do this.]
  55.  * In general, 'n' is the number of words required to hold the type,
  56.  * and 't' is either S, D or Q for single/double/quad.
  57.  *           -- PMM
  58.  */
  59. /* Example: SPARC64:
  60.  * #define _FP_MUL_MEAT_S(R,X,Y) _FP_MUL_MEAT_1_imm(S,R,X,Y)
  61.  * #define _FP_MUL_MEAT_D(R,X,Y) _FP_MUL_MEAT_1_wide(D,R,X,Y,umul_ppmm)
  62.  * #define _FP_MUL_MEAT_Q(R,X,Y) _FP_MUL_MEAT_2_wide(Q,R,X,Y,umul_ppmm)
  63.  *
  64.  * #define _FP_DIV_MEAT_S(R,X,Y) _FP_DIV_MEAT_1_imm(S,R,X,Y,_FP_DIV_HELP_imm)
  65.  * #define _FP_DIV_MEAT_D(R,X,Y) _FP_DIV_MEAT_1_udiv(D,R,X,Y)
  66.  * #define _FP_DIV_MEAT_Q(R,X,Y) _FP_DIV_MEAT_2_udiv_64(Q,R,X,Y)
  67.  *
  68.  * Example: i386:
  69.  * #define _FP_MUL_MEAT_S(R,X,Y)   _FP_MUL_MEAT_1_wide(S,R,X,Y,_i386_mul_32_64)
  70.  * #define _FP_MUL_MEAT_D(R,X,Y)   _FP_MUL_MEAT_2_wide(D,R,X,Y,_i386_mul_32_64)
  71.  *
  72.  * #define _FP_DIV_MEAT_S(R,X,Y)   _FP_DIV_MEAT_1_udiv(S,R,X,Y,_i386_div_64_32)
  73.  * #define _FP_DIV_MEAT_D(R,X,Y)   _FP_DIV_MEAT_2_udiv_64(D,R,X,Y)
  74.  */
  75. #define _FP_MUL_MEAT_S(R,X,Y)   _FP_MUL_MEAT_1_wide(S,R,X,Y,umul_ppmm)
  76. #define _FP_MUL_MEAT_D(R,X,Y)   _FP_MUL_MEAT_2_wide(D,R,X,Y,umul_ppmm)
  77. #define _FP_DIV_MEAT_S(R,X,Y)   _FP_DIV_MEAT_1_udiv(S,R,X,Y)
  78. #define _FP_DIV_MEAT_D(R,X,Y)   _FP_DIV_MEAT_2_udiv_64(D,R,X,Y)
  79. /* These macros define what NaN looks like. They're supposed to expand to 
  80.  * a comma-separated set of 32bit unsigned ints that encode NaN.
  81.  */
  82. #define _FP_NANFRAC_S _FP_QNANBIT_S
  83. #define _FP_NANFRAC_D _FP_QNANBIT_D, 0
  84. #define _FP_NANFRAC_Q           _FP_QNANBIT_Q, 0, 0, 0
  85. #define _FP_KEEPNANFRACP 1
  86. /* This macro appears to be called when both X and Y are NaNs, and 
  87.  * has to choose one and copy it to R. i386 goes for the larger of the
  88.  * two, sparc64 just picks Y. I don't understand this at all so I'll
  89.  * go with sparc64 because it's shorter :->   -- PMM 
  90.  */
  91. #define _FP_CHOOSENAN(fs, wc, R, X, Y)
  92.   do {
  93.     R##_s = Y##_s;
  94.     _FP_FRAC_COPY_##wc(R,Y);
  95.     R##_c = FP_CLS_NAN;
  96.   } while (0)
  97.   
  98. extern void fp_unpack_d(long *, unsigned long *, unsigned long *,
  99. long *, long *, void *);
  100. extern int  fp_pack_d(void *, long, unsigned long, unsigned long, long, long);
  101. extern int  fp_pack_ds(void *, long, unsigned long, unsigned long, long, long);
  102. #define __FP_UNPACK_RAW_1(fs, X, val)
  103.   do {
  104.     union _FP_UNION_##fs *_flo =
  105.      (union _FP_UNION_##fs *)val;
  106.     X##_f = _flo->bits.frac;
  107.     X##_e = _flo->bits.exp;
  108.     X##_s = _flo->bits.sign;
  109.   } while (0)
  110. #define __FP_UNPACK_RAW_2(fs, X, val)
  111.   do {
  112.     union _FP_UNION_##fs *_flo =
  113.      (union _FP_UNION_##fs *)val;
  114.     X##_f0 = _flo->bits.frac0;
  115.     X##_f1 = _flo->bits.frac1;
  116.     X##_e  = _flo->bits.exp;
  117.     X##_s  = _flo->bits.sign;
  118.   } while (0)
  119. #define __FP_UNPACK_S(X,val)
  120.   do {
  121.     __FP_UNPACK_RAW_1(S,X,val);
  122.     _FP_UNPACK_CANONICAL(S,1,X);
  123.   } while (0)
  124. #define __FP_UNPACK_D(X,val)
  125. fp_unpack_d(&X##_s, &X##_f1, &X##_f0, &X##_e, &X##_c, val)
  126. #define __FP_PACK_RAW_1(fs, val, X)
  127.   do {
  128.     union _FP_UNION_##fs *_flo =
  129.      (union _FP_UNION_##fs *)val;
  130.     _flo->bits.frac = X##_f;
  131.     _flo->bits.exp  = X##_e;
  132.     _flo->bits.sign = X##_s;
  133.   } while (0)
  134.   
  135. #define __FP_PACK_RAW_2(fs, val, X)
  136.   do {
  137.     union _FP_UNION_##fs *_flo =
  138.      (union _FP_UNION_##fs *)val;
  139.     _flo->bits.frac0 = X##_f0;
  140.     _flo->bits.frac1 = X##_f1;
  141.     _flo->bits.exp   = X##_e;
  142.     _flo->bits.sign  = X##_s;
  143.   } while (0)
  144. #include <linux/kernel.h>
  145. #include <linux/sched.h>
  146. #define __FPU_FPSCR (current->thread.fpscr)
  147. /* We only actually write to the destination register
  148.  * if exceptions signalled (if any) will not trap.
  149.  */
  150. #define __FPU_ENABLED_EXC 
  151. ({
  152. (__FPU_FPSCR >> 3) & 0x1f;
  153. })
  154. #define __FPU_TRAP_P(bits) 
  155. ((__FPU_ENABLED_EXC & (bits)) != 0)
  156. #define __FP_PACK_S(val,X)
  157. ({  int __exc = _FP_PACK_CANONICAL(S,1,X);
  158.     if(!__exc || !__FPU_TRAP_P(__exc))
  159.         __FP_PACK_RAW_1(S,val,X);
  160.     __exc;
  161. })
  162. #define __FP_PACK_D(val,X)
  163. fp_pack_d(val, X##_s, X##_f1, X##_f0, X##_e, X##_c)
  164. #define __FP_PACK_DS(val,X)
  165. fp_pack_ds(val, X##_s, X##_f1, X##_f0, X##_e, X##_c)
  166. /* Obtain the current rounding mode. */
  167. #define FP_ROUNDMODE
  168. ({
  169. __FPU_FPSCR & 0x3;
  170. })
  171. /* the asm fragments go here: all these are taken from glibc-2.0.5's
  172.  * stdlib/longlong.h
  173.  */
  174. #include <linux/types.h>
  175. #include <asm/byteorder.h>
  176. /* add_ssaaaa is used in op-2.h and should be equivalent to
  177.  * #define add_ssaaaa(sh,sl,ah,al,bh,bl) (sh = ah+bh+ (( sl = al+bl) < al))
  178.  * add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  179.  * high_addend_2, low_addend_2) adds two UWtype integers, composed by
  180.  * HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and LOW_ADDEND_2
  181.  * respectively.  The result is placed in HIGH_SUM and LOW_SUM.  Overflow
  182.  * (i.e. carry out) is not stored anywhere, and is lost.
  183.  */
  184. #define add_ssaaaa(sh, sl, ah, al, bh, bl)
  185.   do {
  186.     if (__builtin_constant_p (bh) && (bh) == 0)
  187.       __asm__ ("{a%I4|add%I4c} %1,%3,%4nt{aze|addze} %0,%2"
  188.      : "=r" ((USItype)(sh)),
  189.        "=&r" ((USItype)(sl))
  190.      : "%r" ((USItype)(ah)),
  191.        "%r" ((USItype)(al)),
  192.        "rI" ((USItype)(bl)));
  193.     else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0)
  194.       __asm__ ("{a%I4|add%I4c} %1,%3,%4nt{ame|addme} %0,%2"
  195.      : "=r" ((USItype)(sh)),
  196.        "=&r" ((USItype)(sl))
  197.      : "%r" ((USItype)(ah)),
  198.        "%r" ((USItype)(al)),
  199.        "rI" ((USItype)(bl)));
  200.     else
  201.       __asm__ ("{a%I5|add%I5c} %1,%4,%5nt{ae|adde} %0,%2,%3"
  202.      : "=r" ((USItype)(sh)),
  203.        "=&r" ((USItype)(sl))
  204.      : "%r" ((USItype)(ah)),
  205.        "r" ((USItype)(bh)),
  206.        "%r" ((USItype)(al)),
  207.        "rI" ((USItype)(bl)));
  208.   } while (0)
  209. /* sub_ddmmss is used in op-2.h and udivmodti4.c and should be equivalent to
  210.  * #define sub_ddmmss(sh, sl, ah, al, bh, bl) (sh = ah-bh - ((sl = al-bl) > al))
  211.  * sub_ddmmss(high_difference, low_difference, high_minuend, low_minuend,
  212.  * high_subtrahend, low_subtrahend) subtracts two two-word UWtype integers,
  213.  * composed by HIGH_MINUEND_1 and LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and
  214.  * LOW_SUBTRAHEND_2 respectively.  The result is placed in HIGH_DIFFERENCE
  215.  * and LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
  216.  * and is lost.
  217.  */
  218. #define sub_ddmmss(sh, sl, ah, al, bh, bl)
  219.   do {
  220.     if (__builtin_constant_p (ah) && (ah) == 0)
  221.       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3nt{sfze|subfze} %0,%2"
  222.        : "=r" ((USItype)(sh)),
  223.  "=&r" ((USItype)(sl))
  224.        : "r" ((USItype)(bh)),
  225.  "rI" ((USItype)(al)),
  226.  "r" ((USItype)(bl)));
  227.     else if (__builtin_constant_p (ah) && (ah) ==~(USItype) 0)
  228.       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3nt{sfme|subfme} %0,%2"
  229.        : "=r" ((USItype)(sh)),
  230.  "=&r" ((USItype)(sl))
  231.        : "r" ((USItype)(bh)),
  232.  "rI" ((USItype)(al)),
  233.  "r" ((USItype)(bl)));
  234.     else if (__builtin_constant_p (bh) && (bh) == 0)
  235.       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3nt{ame|addme} %0,%2"
  236.        : "=r" ((USItype)(sh)),
  237.  "=&r" ((USItype)(sl))
  238.        : "r" ((USItype)(ah)),
  239.  "rI" ((USItype)(al)),
  240.  "r" ((USItype)(bl)));
  241.     else if (__builtin_constant_p (bh) && (bh) ==~(USItype) 0)
  242.       __asm__ ("{sf%I3|subf%I3c} %1,%4,%3nt{aze|addze} %0,%2"
  243.        : "=r" ((USItype)(sh)),
  244.  "=&r" ((USItype)(sl))
  245.        : "r" ((USItype)(ah)),
  246.  "rI" ((USItype)(al)),
  247.  "r" ((USItype)(bl)));
  248.     else
  249.       __asm__ ("{sf%I4|subf%I4c} %1,%5,%4nt{sfe|subfe} %0,%3,%2"
  250.        : "=r" ((USItype)(sh)),
  251.  "=&r" ((USItype)(sl))
  252.        : "r" ((USItype)(ah)),
  253.  "r" ((USItype)(bh)),
  254.  "rI" ((USItype)(al)),
  255.  "r" ((USItype)(bl)));
  256.   } while (0)
  257. /* asm fragments for mul and div */  
  258. /* umul_ppmm(high_prod, low_prod, multipler, multiplicand) multiplies two
  259.  * UWtype integers MULTIPLER and MULTIPLICAND, and generates a two UWtype
  260.  * word product in HIGH_PROD and LOW_PROD.
  261.  */
  262. #define umul_ppmm(ph, pl, m0, m1)
  263.   do {
  264.     USItype __m0 = (m0), __m1 = (m1);
  265.     __asm__ ("mulhwu %0,%1,%2"
  266.      : "=r" ((USItype)(ph))
  267.      : "%r" (__m0),
  268.                "r" (__m1));
  269.     (pl) = __m0 * __m1;
  270.   } while (0)
  271. /* udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  272.  * denominator) divides a UDWtype, composed by the UWtype integers
  273.  * HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
  274.  * in QUOTIENT and the remainder in REMAINDER.  HIGH_NUMERATOR must be less
  275.  * than DENOMINATOR for correct operation.  If, in addition, the most
  276.  * significant bit of DENOMINATOR must be 1, then the pre-processor symbol
  277.  * UDIV_NEEDS_NORMALIZATION is defined to 1.
  278.  */
  279. #define udiv_qrnnd(q, r, n1, n0, d)
  280.   do {
  281.     UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m;
  282.     __d1 = __ll_highpart (d);
  283.     __d0 = __ll_lowpart (d);
  284.     __r1 = (n1) % __d1;
  285.     __q1 = (n1) / __d1;
  286.     __m = (UWtype) __q1 * __d0;
  287.     __r1 = __r1 * __ll_B | __ll_highpart (n0);
  288.     if (__r1 < __m)
  289.       {
  290. __q1--, __r1 += (d);
  291. if (__r1 >= (d)) /* we didn't get carry when adding to __r1 */
  292.   if (__r1 < __m)
  293.     __q1--, __r1 += (d);
  294.       }
  295.     __r1 -= __m;
  296.     __r0 = __r1 % __d1;
  297.     __q0 = __r1 / __d1;
  298.     __m = (UWtype) __q0 * __d0;
  299.     __r0 = __r0 * __ll_B | __ll_lowpart (n0);
  300.     if (__r0 < __m)
  301.       {
  302. __q0--, __r0 += (d);
  303. if (__r0 >= (d))
  304.   if (__r0 < __m)
  305.     __q0--, __r0 += (d);
  306.       }
  307.     __r0 -= __m;
  308.     (q) = (UWtype) __q1 * __ll_B | __q0;
  309.     (r) = __r0;
  310.   } while (0)
  311. #define UDIV_NEEDS_NORMALIZATION 1
  312. #define abort()
  313. return 0
  314. #ifdef __BIG_ENDIAN
  315. #define __BYTE_ORDER __BIG_ENDIAN
  316. #else
  317. #define __BYTE_ORDER __LITTLE_ENDIAN
  318. #endif
  319. /* Exception flags. */
  320. #define EFLAG_INVALID (1 << (31 - 2))
  321. #define EFLAG_OVERFLOW (1 << (31 - 3))
  322. #define EFLAG_UNDERFLOW (1 << (31 - 4))
  323. #define EFLAG_DIVZERO (1 << (31 - 5))
  324. #define EFLAG_INEXACT (1 << (31 - 6))
  325. #define EFLAG_VXSNAN (1 << (31 - 7))
  326. #define EFLAG_VXISI (1 << (31 - 8))
  327. #define EFLAG_VXIDI (1 << (31 - 9))
  328. #define EFLAG_VXZDZ (1 << (31 - 10))
  329. #define EFLAG_VXIMZ (1 << (31 - 11))
  330. #define EFLAG_VXVC (1 << (31 - 12))
  331. #define EFLAG_VXSOFT (1 << (31 - 21))
  332. #define EFLAG_VXSQRT (1 << (31 - 22))
  333. #define EFLAG_VXCVI (1 << (31 - 23))