fcnvfut.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  3.  *
  4.  * Floating-point emulation code
  5.  *  Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
  6.  *
  7.  *    This program is free software; you can redistribute it and/or modify
  8.  *    it under the terms of the GNU General Public License as published by
  9.  *    the Free Software Foundation; either version 2, or (at your option)
  10.  *    any later version.
  11.  *
  12.  *    This program is distributed in the hope that it will be useful,
  13.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *    GNU General Public License for more details.
  16.  *
  17.  *    You should have received a copy of the GNU General Public License
  18.  *    along with this program; if not, write to the Free Software
  19.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21. /*
  22.  * BEGIN_DESC
  23.  *
  24.  *  File:
  25.  * @(#) pa/spmath/fcnvfut.c $Revision: 1.1 $
  26.  *
  27.  *  Purpose:
  28.  * Floating-point to Unsigned Fixed-point Converts with Truncation
  29.  *
  30.  *  External Interfaces:
  31.  * dbl_to_dbl_fcnvfut(srcptr,nullptr,dstptr,status)
  32.  * dbl_to_sgl_fcnvfut(srcptr,nullptr,dstptr,status)
  33.  * sgl_to_dbl_fcnvfut(srcptr,nullptr,dstptr,status)
  34.  * sgl_to_sgl_fcnvfut(srcptr,nullptr,dstptr,status)
  35.  *
  36.  *  Internal Interfaces:
  37.  *
  38.  *  Theory:
  39.  * <<please update with a overview of the operation of this file>>
  40.  *
  41.  * END_DESC
  42. */
  43. #include "float.h"
  44. #include "sgl_float.h"
  45. #include "dbl_float.h"
  46. #include "cnv_float.h"
  47. /************************************************************************
  48.  *  Floating-point to Unsigned Fixed-point Converts with Truncation *
  49.  ************************************************************************/
  50. /*
  51.  *  Convert single floating-point to single fixed-point format
  52.  *  with truncated result
  53.  */
  54. /*ARGSUSED*/
  55. int
  56. sgl_to_sgl_fcnvfut (sgl_floating_point * srcptr, unsigned int *nullptr,
  57.     unsigned int *dstptr, unsigned int *status)
  58. {
  59. register unsigned int src, result;
  60. register int src_exponent;
  61. src = *srcptr;
  62. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  63. /* 
  64.  * Test for overflow
  65.  */
  66. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  67. if (Sgl_isone_sign(src)) {
  68. result = 0;
  69. } else {
  70. result = 0xffffffff;
  71. }
  72. if (Is_invalidtrap_enabled()) {
  73. return(INVALIDEXCEPTION);
  74. }
  75. Set_invalidflag();
  76. *dstptr = result;
  77. return(NOEXCEPTION);
  78. }
  79. /*
  80.  * Generate result
  81.  */
  82. if (src_exponent >= 0) {
  83. /* 
  84.  * Check sign.
  85.  * If negative, trap unimplemented.
  86.  */
  87. if (Sgl_isone_sign(src)) {
  88. result = 0;
  89. if (Is_invalidtrap_enabled()) {
  90. return(INVALIDEXCEPTION);
  91. }
  92. Set_invalidflag();
  93. *dstptr = result;
  94. return(NOEXCEPTION);
  95. }
  96. Sgl_clear_signexponent_set_hidden(src);
  97. Suint_from_sgl_mantissa(src,src_exponent,result);
  98. *dstptr = result;
  99. /* check for inexact */
  100. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  101. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  102. else Set_inexactflag();
  103. }
  104. }
  105. else {
  106. *dstptr = 0;
  107. /* check for inexact */
  108. if (Sgl_isnotzero_exponentmantissa(src)) {
  109. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  110. else Set_inexactflag();
  111. }
  112. }
  113. return(NOEXCEPTION);
  114. }
  115. /*
  116.  *  Single Floating-point to Double Unsigned Fixed 
  117.  */
  118. /*ARGSUSED*/
  119. int
  120. sgl_to_dbl_fcnvfut (sgl_floating_point * srcptr, unsigned int *nullptr,
  121.     dbl_unsigned * dstptr, unsigned int *status)
  122. {
  123. register int src_exponent;
  124. register unsigned int src, resultp1, resultp2;
  125. src = *srcptr;
  126. src_exponent = Sgl_exponent(src) - SGL_BIAS;
  127. /* 
  128.  * Test for overflow
  129.  */
  130. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  131. if (Sgl_isone_sign(src)) {
  132. resultp1 = resultp2 = 0;
  133. } else {
  134. resultp1 = resultp2 = 0xffffffff;
  135. }
  136. if (Is_invalidtrap_enabled()) {
  137. return(INVALIDEXCEPTION);
  138. }
  139. Set_invalidflag();
  140.      Duint_copytoptr(resultp1,resultp2,dstptr);
  141. return(NOEXCEPTION);
  142. }
  143. /*
  144.  * Generate result
  145.  */
  146. if (src_exponent >= 0) {
  147. /* 
  148.  * Check sign.
  149.  * If negative, trap unimplemented.
  150.  */
  151. if (Sgl_isone_sign(src)) {
  152. resultp1 = resultp2 = 0;
  153. if (Is_invalidtrap_enabled()) {
  154. return(INVALIDEXCEPTION);
  155. }
  156. Set_invalidflag();
  157.      Duint_copytoptr(resultp1,resultp2,dstptr);
  158. return(NOEXCEPTION);
  159. }
  160. Sgl_clear_signexponent_set_hidden(src);
  161. Duint_from_sgl_mantissa(src,src_exponent,resultp1,resultp2);
  162. Duint_copytoptr(resultp1,resultp2,dstptr);
  163. /* check for inexact */
  164. if (Sgl_isinexact_to_unsigned(src,src_exponent)) {
  165. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  166. else Set_inexactflag();
  167. }
  168. }
  169. else {
  170. Duint_setzero(resultp1,resultp2);
  171. Duint_copytoptr(resultp1,resultp2,dstptr);
  172. /* check for inexact */
  173. if (Sgl_isnotzero_exponentmantissa(src)) {
  174. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  175. else Set_inexactflag();
  176. }
  177. }
  178. return(NOEXCEPTION);
  179. }
  180. /*
  181.  *  Double Floating-point to Single Unsigned Fixed 
  182.  */
  183. /*ARGSUSED*/
  184. int
  185. dbl_to_sgl_fcnvfut (dbl_floating_point * srcptr, unsigned int *nullptr,
  186.     unsigned int *dstptr, unsigned int *status)
  187. {
  188. register unsigned int srcp1, srcp2, result;
  189. register int src_exponent;
  190. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  191. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  192. /* 
  193.  * Test for overflow
  194.  */
  195. if (src_exponent > SGL_FX_MAX_EXP + 1) {
  196. if (Dbl_isone_sign(srcp1)) {
  197. result = 0;
  198. } else {
  199. result = 0xffffffff;
  200. }
  201. if (Is_invalidtrap_enabled()) {
  202. return(INVALIDEXCEPTION);
  203. }
  204. Set_invalidflag();
  205. *dstptr = result;
  206. return(NOEXCEPTION);
  207. }
  208. /*
  209.  * Generate result
  210.  */
  211. if (src_exponent >= 0) {
  212. /* 
  213.  * Check sign.
  214.  * If negative, trap unimplemented.
  215.  */
  216. if (Dbl_isone_sign(srcp1)) {
  217. result = 0;
  218. if (Is_invalidtrap_enabled()) {
  219. return(INVALIDEXCEPTION);
  220. }
  221. Set_invalidflag();
  222. *dstptr = result;
  223. return(NOEXCEPTION);
  224. }
  225. Dbl_clear_signexponent_set_hidden(srcp1);
  226. Suint_from_dbl_mantissa(srcp1,srcp2,src_exponent,result);
  227. *dstptr = result;
  228. /* check for inexact */
  229. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  230. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  231. else Set_inexactflag();
  232. }
  233. }
  234. else {
  235. *dstptr = 0;
  236. /* check for inexact */
  237. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  238. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  239. else Set_inexactflag();
  240. }
  241. }
  242. return(NOEXCEPTION);
  243. }
  244. /*
  245.  *  Double Floating-point to Double Unsigned Fixed 
  246.  */
  247. /*ARGSUSED*/
  248. int
  249. dbl_to_dbl_fcnvfut (dbl_floating_point * srcptr, unsigned int *nullptr,
  250.     dbl_unsigned * dstptr, unsigned int *status)
  251. {
  252. register int src_exponent;
  253. register unsigned int srcp1, srcp2, resultp1, resultp2;
  254. Dbl_copyfromptr(srcptr,srcp1,srcp2);
  255. src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
  256. /* 
  257.  * Test for overflow
  258.  */
  259. if (src_exponent > DBL_FX_MAX_EXP + 1) {
  260. if (Dbl_isone_sign(srcp1)) {
  261. resultp1 = resultp2 = 0;
  262. } else {
  263. resultp1 = resultp2 = 0xffffffff;
  264. }
  265. if (Is_invalidtrap_enabled()) {
  266. return(INVALIDEXCEPTION);
  267. }
  268. Set_invalidflag();
  269.      Duint_copytoptr(resultp1,resultp2,dstptr);
  270. return(NOEXCEPTION);
  271. }
  272. /*
  273.  * Generate result
  274.  */
  275. if (src_exponent >= 0) {
  276. /* 
  277.  * Check sign.
  278.  * If negative, trap unimplemented.
  279.  */
  280. if (Dbl_isone_sign(srcp1)) {
  281. resultp1 = resultp2 = 0;
  282. if (Is_invalidtrap_enabled()) {
  283. return(INVALIDEXCEPTION);
  284. }
  285. Set_invalidflag();
  286.      Duint_copytoptr(resultp1,resultp2,dstptr);
  287. return(NOEXCEPTION);
  288. }
  289. Dbl_clear_signexponent_set_hidden(srcp1);
  290. Duint_from_dbl_mantissa(srcp1,srcp2,src_exponent,
  291.   resultp1,resultp2);
  292. Duint_copytoptr(resultp1,resultp2,dstptr);
  293. /* check for inexact */
  294. if (Dbl_isinexact_to_unsigned(srcp1,srcp2,src_exponent)) {
  295. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  296. else Set_inexactflag();
  297. }
  298. }
  299. else {
  300. Duint_setzero(resultp1,resultp2);
  301. Duint_copytoptr(resultp1,resultp2,dstptr);
  302. /* check for inexact */
  303. if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
  304. if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  305. else Set_inexactflag();
  306. }
  307. }
  308. return(NOEXCEPTION);
  309. }