Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
12 KiB

  1. /* file: cvt_vax_f.c */
  2. /*
  3. **
  4. ** COPYRIGHT (c) 1989, 1990 BY
  5. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  6. ** ALL RIGHTS RESERVED.
  7. **
  8. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  9. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  10. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  11. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  12. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  13. ** TRANSFERRED.
  14. **
  15. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  16. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  17. ** CORPORATION.
  18. **
  19. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  20. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  21. **
  22. */
  23. /*
  24. **++
  25. ** Facility:
  26. **
  27. ** CVT Run-Time Library
  28. **
  29. ** Abstract:
  30. **
  31. ** This module contains routines to convert VAX F_Float floating
  32. ** point data into other supported floating point formats.
  33. **
  34. ** Authors:
  35. **
  36. ** Math RTL
  37. **
  38. ** Creation Date: December 5, 1989.
  39. **
  40. ** Modification History:
  41. **
  42. ** 1-001 Original created. MRTL 5-Dec-1989.
  43. ** 1-002 Add VMS and F77 bindings. TS 26-Mar-1990.
  44. **
  45. **--
  46. */
  47. /*
  48. **
  49. ** TABLE OF CONTENTS
  50. **
  51. ** cvt_vax_f_to_cray
  52. ** cvt_vax_f_to_ibm_short
  53. ** cvt_vax_f_to_ieee_single
  54. **
  55. */
  56. #include <stdio.h>
  57. #include <sysinc.h>
  58. #include <rpc.h>
  59. #include "cvt.h"
  60. #include "cvtpvt.h"
  61. //
  62. // Added for the MS NT environment
  63. //
  64. #include <stdlib.h>
  65. /*
  66. * C binding
  67. */
  68. void cvt_vax_f_to_ieee_single(
  69. CVT_VAX_F input_value,
  70. CVT_SIGNED_INT options,
  71. CVT_IEEE_SINGLE output_value
  72. )
  73. {
  74. int i, round_bit_position;
  75. UNPACKED_REAL r;
  76. switch ( options & ~(CVT_C_BIG_ENDIAN | CVT_C_ERR_UNDERFLOW) ) {
  77. case 0 : options |= CVT_C_ROUND_TO_NEAREST;
  78. case CVT_C_ROUND_TO_NEAREST :
  79. case CVT_C_TRUNCATE :
  80. case CVT_C_ROUND_TO_POS :
  81. case CVT_C_ROUND_TO_NEG :
  82. case CVT_C_VAX_ROUNDING : break;
  83. default : RAISE(cvt__invalid_option);
  84. }
  85. // ===========================================================================
  86. //
  87. // This file used to be included as a separate file.
  88. //#include "unp_vaxf.c"
  89. //
  90. // ===========================================================================
  91. /* file: unpack_vax_f.c */
  92. /*
  93. **
  94. ** COPYRIGHT (c) 1989 BY
  95. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  96. ** ALL RIGHTS RESERVED.
  97. **
  98. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  99. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  100. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  101. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  102. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  103. ** TRANSFERRED.
  104. **
  105. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  106. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  107. ** CORPORATION.
  108. **
  109. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  110. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  111. **
  112. */
  113. /*
  114. **++
  115. ** Facility:
  116. **
  117. ** CVT Run-Time Library
  118. **
  119. ** Abstract:
  120. **
  121. ** This module contains code to extract information from a VAX
  122. ** f_floating number and to initialize an UNPACKED_REAL structure
  123. ** with those bits.
  124. **
  125. ** This module is meant to be used as an include file.
  126. **
  127. ** Author: Math RTL
  128. **
  129. ** Creation Date: November 24, 1989.
  130. **
  131. ** Modification History:
  132. **
  133. **--
  134. */
  135. /*
  136. **++
  137. ** Functional Description:
  138. **
  139. ** This module contains code to extract information from a VAX
  140. ** f_floating number and to initialize an UNPACKED_REAL structure
  141. ** with those bits.
  142. **
  143. ** See the header files for a description of the UNPACKED_REAL
  144. ** structure.
  145. **
  146. ** A VAX f_floating number in (16 bit words) looks like:
  147. **
  148. ** [0]: Sign bit, 8 exp bits (bias 128), 7 fraction bits
  149. ** [1]: 16 more fraction bits
  150. **
  151. ** 0.5 <= fraction < 1.0, MSB implicit
  152. **
  153. **
  154. ** Implicit parameters:
  155. **
  156. ** input_value: a pointer to the input parameter.
  157. **
  158. ** r: an UNPACKED_REAL structure
  159. **
  160. **--
  161. */
  162. RpcpMemoryCopy(&r[1], input_value, 4);
  163. /* Initialize FLAGS and perhaps set NEGATIVE bit */
  164. r[U_R_FLAGS] = (r[1] >> 15) & U_R_NEGATIVE;
  165. /* Extract VAX biased exponent */
  166. r[U_R_EXP] = (r[1] >> 7) & 0x000000FFL;
  167. if (r[U_R_EXP] == 0) {
  168. if (r[U_R_FLAGS])
  169. r[U_R_FLAGS] |= U_R_INVALID;
  170. else
  171. r[U_R_FLAGS] = U_R_ZERO;
  172. } else {
  173. /* Adjust for VAX 16 bit floating format */
  174. r[1] = ((r[1] << 16) | (r[1] >> 16));
  175. /* Add unpacked real bias and subtract VAX bias */
  176. r[U_R_EXP] += (U_R_BIAS - 128);
  177. /* Set hidden bit */
  178. r[1] |= 0x00800000L;
  179. /* Left justify fraction bits */
  180. r[1] <<= 8;
  181. /* Clear uninitialized parts for unpacked real */
  182. r[2] = 0;
  183. r[3] = 0;
  184. r[4] = 0;
  185. }
  186. // end of file: unpack_vax_f.c
  187. //
  188. // ===========================================================================
  189. //
  190. // This file used to be included as a separate file.
  191. //#include "pack_ies.c"
  192. //
  193. // ===========================================================================
  194. /* file: pack_ieee_s.c */
  195. /*
  196. **
  197. ** COPYRIGHT (c) 1989 BY
  198. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  199. ** ALL RIGHTS RESERVED.
  200. **
  201. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  202. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  203. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  204. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  205. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  206. ** TRANSFERRED.
  207. **
  208. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  209. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  210. ** CORPORATION.
  211. **
  212. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  213. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  214. **
  215. */
  216. /*
  217. **++
  218. ** Facility:
  219. **
  220. ** CVT Run-Time Library
  221. **
  222. ** Abstract:
  223. **
  224. ** This module contains code to extract information from an
  225. ** UNPACKED_REAL structure and to create an IEEE single floating number
  226. ** with those bits.
  227. **
  228. ** This module is meant to be used as an include file.
  229. **
  230. ** Author: Math RTL
  231. **
  232. ** Creation Date: November 24, 1989.
  233. **
  234. ** Modification History:
  235. **
  236. **--
  237. */
  238. /*
  239. **++
  240. ** Functional Description:
  241. **
  242. ** This module contains code to extract information from an
  243. ** UNPACKED_REAL structure and to create an IEEE single number
  244. ** with those bits.
  245. **
  246. ** See the header files for a description of the UNPACKED_REAL
  247. ** structure.
  248. **
  249. ** A normalized IEEE single precision floating number looks like:
  250. **
  251. ** Sign bit, 8 exp bits (bias 127), 23 fraction bits
  252. **
  253. ** 1.0 <= fraction < 2.0, MSB implicit
  254. **
  255. ** For more details see "Mips R2000 Risc Architecture"
  256. ** by Gerry Kane, page 6-8 or ANSI/IEEE Std 754-1985.
  257. **
  258. **
  259. ** Implicit parameters:
  260. **
  261. ** options: a word of flags, see include files.
  262. **
  263. ** output_value: a pointer to the input parameter.
  264. **
  265. ** r: an UNPACKED_REAL structure.
  266. **
  267. **--
  268. */
  269. if (r[U_R_FLAGS] & U_R_UNUSUAL) {
  270. if (r[U_R_FLAGS] & U_R_ZERO)
  271. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  272. RpcpMemoryCopy(output_value, IEEE_S_NEG_ZERO, 4);
  273. else
  274. RpcpMemoryCopy(output_value, IEEE_S_POS_ZERO, 4);
  275. else if (r[U_R_FLAGS] & U_R_INFINITY) {
  276. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  277. RpcpMemoryCopy(output_value, IEEE_S_NEG_INFINITY, 4);
  278. else
  279. RpcpMemoryCopy(output_value, IEEE_S_POS_INFINITY, 4);
  280. } else if (r[U_R_FLAGS] & U_R_INVALID) {
  281. RpcpMemoryCopy(output_value, IEEE_S_INVALID, 4);
  282. RAISE(cvt__invalid_value);
  283. }
  284. } else {
  285. /* Precision varies if value will be a denorm */
  286. /* So, figure out where to round (0 <= i <= 24). */
  287. round_bit_position = r[U_R_EXP] - ((U_R_BIAS - 126) - 23);
  288. if (round_bit_position < 0)
  289. round_bit_position = 0;
  290. else if (round_bit_position > 24)
  291. round_bit_position = 24;
  292. #include "round.cxx"
  293. if (r[U_R_EXP] < (U_R_BIAS - 125)) {
  294. /* Denorm or underflow */
  295. if (r[U_R_EXP] < ((U_R_BIAS - 125) - 23)) {
  296. /* Value is too small for a denorm, so underflow */
  297. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  298. RpcpMemoryCopy(output_value, IEEE_S_NEG_ZERO, 4);
  299. else
  300. RpcpMemoryCopy(output_value, IEEE_S_POS_ZERO, 4);
  301. if (options & CVT_C_ERR_UNDERFLOW) {
  302. RAISE(cvt__underflow);
  303. }
  304. } else {
  305. /* Figure leading zeros for denorm and right-justify fraction */
  306. i = 32 - (r[U_R_EXP] - ((U_R_BIAS - 126) - 23));
  307. r[1] >>= i;
  308. /* Set sign bit */
  309. r[1] |= (r[U_R_FLAGS] << 31);
  310. if (options & CVT_C_BIG_ENDIAN) {
  311. r[0] = ((r[1] << 24) | (r[1] >> 24));
  312. r[0] |= ((r[1] << 8) & 0x00FF0000L);
  313. r[0] |= ((r[1] >> 8) & 0x0000FF00L);
  314. RpcpMemoryCopy(output_value, r, 4);
  315. } else {
  316. RpcpMemoryCopy(output_value, &r[1], 4);
  317. }
  318. }
  319. } else if (r[U_R_EXP] > (U_R_BIAS + 128)) {
  320. /* Overflow */
  321. if (options & CVT_C_TRUNCATE) {
  322. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  323. RpcpMemoryCopy(output_value, IEEE_S_NEG_HUGE, 4);
  324. else
  325. RpcpMemoryCopy(output_value, IEEE_S_POS_HUGE, 4);
  326. } else if ((options & CVT_C_ROUND_TO_POS)
  327. && (r[U_R_FLAGS] & U_R_NEGATIVE)) {
  328. RpcpMemoryCopy(output_value, IEEE_S_NEG_HUGE, 4);
  329. } else if ((options & CVT_C_ROUND_TO_NEG)
  330. && !(r[U_R_FLAGS] & U_R_NEGATIVE)) {
  331. RpcpMemoryCopy(output_value, IEEE_S_POS_HUGE, 4);
  332. } else {
  333. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  334. RpcpMemoryCopy(output_value, IEEE_S_NEG_INFINITY, 4);
  335. else
  336. RpcpMemoryCopy(output_value, IEEE_S_POS_INFINITY, 4);
  337. }
  338. RAISE(cvt__overflow);
  339. } else {
  340. /* Adjust bias of exponent */
  341. r[U_R_EXP] -= (U_R_BIAS - 126);
  342. /* Make room for exponent and sign bit */
  343. r[1] >>= 8;
  344. /* Clear implicit bit */
  345. r[1] &= 0x007FFFFFL;
  346. /* OR in exponent and sign bit */
  347. r[1] |= (r[U_R_EXP] << 23);
  348. r[1] |= (r[U_R_FLAGS] << 31);
  349. if (options & CVT_C_BIG_ENDIAN) {
  350. r[0] = ((r[1] << 24) | (r[1] >> 24));
  351. r[0] |= ((r[1] << 8) & 0x00FF0000L);
  352. r[0] |= ((r[1] >> 8) & 0x0000FF00L);
  353. RpcpMemoryCopy(output_value, r, 4);
  354. } else {
  355. RpcpMemoryCopy(output_value, &r[1], 4);
  356. }
  357. }
  358. }
  359. // end of file: pack_ies.c
  360. }
  361.