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.

531 lines
14 KiB

  1. /* file: cvt_vax_g.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 G_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_g_to_cray
  52. ** cvt_vax_g_to_ibm_long
  53. ** cvt_vax_g_to_ieee_double
  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. **
  67. ** Routine:
  68. **
  69. ** cvt_vax_g_to_ieee_double
  70. **
  71. ** Functional Description:
  72. **
  73. ** This routine converts a VAX G_Float floating point number
  74. ** into an IEEE double precision floating point number.
  75. **
  76. ** Formal Parameters:
  77. **
  78. ** input_value A VAX G_Float floating point number.
  79. **
  80. ** options An integer bit mask. Set bits in the mask represent
  81. ** selected routine options. Applicable options are:
  82. **
  83. ** CVT_C_BIG_ENDIAN - default is little endian
  84. ** CVT_C_ERR_UNDERFLOW - Raise underflows
  85. ** CVT_C_TRUNCATE - truncate
  86. ** CVT_C_ROUND_TO_POS - round to +infinity
  87. ** CVT_C_ROUND_TO_NEG - round to -infinity
  88. ** CVT_C_ROUND_TO_NEAREST - round to nearest
  89. ** CVT_C_VAX_ROUNDING - VAX rounding
  90. **
  91. ** NOTE: If no rounding mode is selected the following
  92. ** default rounding mode is assumed:
  93. **
  94. ** CVT_C_ROUND_TO_NEAREST.
  95. **
  96. ** output_value The IEEE double precision representation of the VAX
  97. ** G_Float number.
  98. **
  99. ** Side Effects/Signaled Errors:
  100. **
  101. ** cvt__invalid_value - an invalid input value was specified.
  102. ** cvt__invalid_option - an invalid option was specified.
  103. ** cvt__underflow - an underlow occurred during conversion while
  104. ** Raise underflow was set.
  105. **
  106. */
  107. /*
  108. * C binding
  109. */
  110. void cvt_vax_g_to_ieee_double(
  111. CVT_VAX_G input_value,
  112. CVT_SIGNED_INT options,
  113. CVT_IEEE_DOUBLE output_value )
  114. {
  115. int i, round_bit_position;
  116. UNPACKED_REAL r;
  117. switch ( options & ~(CVT_C_BIG_ENDIAN | CVT_C_ERR_UNDERFLOW) ) {
  118. case 0 : options |= CVT_C_ROUND_TO_NEAREST;
  119. case CVT_C_ROUND_TO_NEAREST :
  120. case CVT_C_TRUNCATE :
  121. case CVT_C_ROUND_TO_POS :
  122. case CVT_C_ROUND_TO_NEG :
  123. case CVT_C_VAX_ROUNDING : break;
  124. default : RAISE(cvt__invalid_option);
  125. }
  126. // ===========================================================================
  127. //
  128. // This file used to be included as a separate file.
  129. //#include "unp_vaxg.c"
  130. //
  131. // ===========================================================================
  132. /* file: unpack_vax_g.c */
  133. /*
  134. **
  135. ** COPYRIGHT (c) 1989 BY
  136. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  137. ** ALL RIGHTS RESERVED.
  138. **
  139. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  140. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  141. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  142. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  143. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  144. ** TRANSFERRED.
  145. **
  146. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  147. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  148. ** CORPORATION.
  149. **
  150. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  151. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  152. **
  153. */
  154. /*
  155. **++
  156. ** Facility:
  157. **
  158. ** CVT Run-Time Library
  159. **
  160. ** Abstract:
  161. **
  162. ** This module contains code to extract information from a VAX
  163. ** g_floating number and to initialize an UNPACKED_REAL structure
  164. ** with those bits.
  165. **
  166. ** This module is meant to be used as an include file.
  167. **
  168. ** Author: Math RTL
  169. **
  170. ** Creation Date: November 24, 1989.
  171. **
  172. ** Modification History:
  173. **
  174. **--
  175. */
  176. /*
  177. **++
  178. ** Functional Description:
  179. **
  180. ** This module contains code to extract information from a VAX
  181. ** g_floating number and to initialize an UNPACKED_REAL structure
  182. ** with those bits.
  183. **
  184. ** See the header files for a description of the UNPACKED_REAL
  185. ** structure.
  186. **
  187. ** A VAX g_floating number in (16 bit words) looks like:
  188. **
  189. ** [0]: Sign bit, 11 exp bits (bias 1024), 4 fraction bits
  190. ** [1]: 16 more fraction bits
  191. ** [2]: 16 more fraction bits
  192. ** [3]: 16 more fraction bits
  193. **
  194. ** 0.5 <= fraction < 1.0, MSB implicit
  195. **
  196. **
  197. ** Implicit parameters:
  198. **
  199. ** input_value: a pointer to the input parameter.
  200. **
  201. ** r: an UNPACKED_REAL structure
  202. **
  203. **--
  204. */
  205. RpcpMemoryCopy(&r[1], input_value, 8);
  206. /* Initialize FLAGS and perhaps set NEGATIVE bit */
  207. r[U_R_FLAGS] = (r[1] >> 15) & U_R_NEGATIVE;
  208. /* Extract VAX biased exponent */
  209. r[U_R_EXP] = (r[1] >> 4) & 0x000007FFL;
  210. if (r[U_R_EXP] == 0) {
  211. if (r[U_R_FLAGS])
  212. r[U_R_FLAGS] |= U_R_INVALID;
  213. else
  214. r[U_R_FLAGS] = U_R_ZERO;
  215. } else {
  216. /* Adjust for VAX 16 bit floating format */
  217. r[1] = ((r[1] << 16) | (r[1] >> 16));
  218. r[2] = ((r[2] << 16) | (r[2] >> 16));
  219. /* Add unpacked real bias and subtract VAX bias */
  220. r[U_R_EXP] += (U_R_BIAS - 1024);
  221. /* Set hidden bit */
  222. r[1] |= 0x00100000L;
  223. /* Left justify fraction bits */
  224. r[1] <<= 11;
  225. r[1] |= (r[2] >> 21);
  226. r[2] <<= 11;
  227. /* Clear uninitialized part of unpacked real */
  228. r[3] = 0;
  229. r[4] = 0;
  230. }
  231. // end of file: unpack_vax_g.c
  232. //
  233. // ===========================================================================
  234. //
  235. // This file used to be included as a separate file.
  236. //#include "pack_iet.c"
  237. //
  238. // ===========================================================================
  239. /* file: pack_ieee_t.c */
  240. /*
  241. **
  242. ** COPYRIGHT (c) 1989 BY
  243. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  244. ** ALL RIGHTS RESERVED.
  245. **
  246. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  247. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  248. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  249. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  250. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  251. ** TRANSFERRED.
  252. **
  253. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  254. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  255. ** CORPORATION.
  256. **
  257. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  258. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  259. **
  260. */
  261. /*
  262. **++
  263. ** Facility:
  264. **
  265. ** CVT Run-Time Library
  266. **
  267. ** Abstract:
  268. **
  269. ** This module contains code to extract information from an
  270. ** UNPACKED_REAL structure and to create an IEEE double floating number
  271. ** with those bits.
  272. **
  273. ** This module is meant to be used as an include file.
  274. **
  275. ** Author: Math RTL
  276. **
  277. ** Creation Date: November 24, 1989.
  278. **
  279. ** Modification History:
  280. **
  281. **--
  282. */
  283. /*
  284. **++
  285. ** Functional Description:
  286. **
  287. ** This module contains code to extract information from an
  288. ** UNPACKED_REAL structure and to create an IEEE double floating number
  289. ** with those bits.
  290. **
  291. ** See the header files for a description of the UNPACKED_REAL
  292. ** structure.
  293. **
  294. ** A normalized IEEE double precision floating number looks like:
  295. **
  296. ** [0]: 32 low order fraction bits
  297. ** [1]: Sign bit, 11 exp bits (bias 1023), 20 fraction bits
  298. **
  299. ** 1.0 <= fraction < 2.0, MSB implicit
  300. **
  301. ** For more details see "Mips R2000 Risc Architecture"
  302. ** by Gerry Kane, page 6-8 or ANSI/IEEE Std 754-1985.
  303. **
  304. **
  305. ** Implicit parameters:
  306. **
  307. ** options: a word of flags, see include files.
  308. **
  309. ** output_value: a pointer to the input parameter.
  310. **
  311. ** r: an UNPACKED_REAL structure.
  312. **
  313. ** i: a temporary integer variable
  314. **
  315. **--
  316. */
  317. if (r[U_R_FLAGS] & U_R_UNUSUAL) {
  318. if (r[U_R_FLAGS] & U_R_ZERO)
  319. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  320. RpcpMemoryCopy(output_value, IEEE_T_NEG_ZERO, 8);
  321. else
  322. RpcpMemoryCopy(output_value, IEEE_T_POS_ZERO, 8);
  323. else if (r[U_R_FLAGS] & U_R_INFINITY) {
  324. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  325. RpcpMemoryCopy(output_value, IEEE_T_NEG_INFINITY, 8);
  326. else
  327. RpcpMemoryCopy(output_value, IEEE_T_POS_INFINITY, 8);
  328. } else if (r[U_R_FLAGS] & U_R_INVALID) {
  329. RpcpMemoryCopy(output_value, IEEE_T_INVALID, 8);
  330. RAISE(cvt__invalid_value);
  331. }
  332. } else {
  333. /* Precision varies if value will be a denorm */
  334. /* So, figure out where to round (0 <= i <= 53). */
  335. round_bit_position = r[U_R_EXP] - ((U_R_BIAS - 1022) - 52);
  336. if (round_bit_position < 0)
  337. round_bit_position = 0;
  338. else if (round_bit_position > 53)
  339. round_bit_position = 53;
  340. #include "round.cxx"
  341. if (r[U_R_EXP] < (U_R_BIAS - 1021)) {
  342. /* Denorm or underflow */
  343. if (r[U_R_EXP] < ((U_R_BIAS - 1021) - 52)) {
  344. /* Value is too small for a denorm, so underflow */
  345. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  346. RpcpMemoryCopy(output_value, IEEE_T_NEG_ZERO, 8);
  347. else
  348. RpcpMemoryCopy(output_value, IEEE_T_POS_ZERO, 8);
  349. if (options & CVT_C_ERR_UNDERFLOW) {
  350. RAISE(cvt__underflow);
  351. }
  352. } else {
  353. /* Figure leading zeros for denorm and right-justify fraction */
  354. i = 64 - (r[U_R_EXP] - ((U_R_BIAS - 1022) - 52));
  355. if (i > 31) {
  356. i -= 32;
  357. r[2] = (r[1] >> i);
  358. r[1] = 0;
  359. } else {
  360. r[2] >>= i;
  361. r[2] |= (r[1] << (32 - i));
  362. r[1] >>= i;
  363. }
  364. /* OR in sign bit */
  365. r[1] |= (r[U_R_FLAGS] << 31);
  366. if (options & CVT_C_BIG_ENDIAN) {
  367. r[0] = ((r[1] << 24) | (r[1] >> 24));
  368. r[0] |= ((r[1] << 8) & 0x00FF0000L);
  369. r[0] |= ((r[1] >> 8) & 0x0000FF00L);
  370. r[1] = ((r[2] << 24) | (r[2] >> 24));
  371. r[1] |= ((r[2] << 8) & 0x00FF0000L);
  372. r[1] |= ((r[2] >> 8) & 0x0000FF00L);
  373. } else {
  374. r[0] = r[2];
  375. }
  376. RpcpMemoryCopy(output_value, r, 8);
  377. }
  378. } else if (r[U_R_EXP] > (U_R_BIAS + 1024)) {
  379. /* Overflow */
  380. if (options & CVT_C_TRUNCATE) {
  381. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  382. RpcpMemoryCopy(output_value, IEEE_T_NEG_HUGE, 8);
  383. else
  384. RpcpMemoryCopy(output_value, IEEE_T_POS_HUGE, 8);
  385. } else if ((options & CVT_C_ROUND_TO_POS)
  386. && (r[U_R_FLAGS] & U_R_NEGATIVE)) {
  387. RpcpMemoryCopy(output_value, IEEE_T_NEG_HUGE, 8);
  388. } else if ((options & CVT_C_ROUND_TO_NEG)
  389. && !(r[U_R_FLAGS] & U_R_NEGATIVE)) {
  390. RpcpMemoryCopy(output_value, IEEE_T_POS_HUGE, 8);
  391. } else {
  392. if (r[U_R_FLAGS] & U_R_NEGATIVE)
  393. RpcpMemoryCopy(output_value, IEEE_T_NEG_INFINITY, 8);
  394. else
  395. RpcpMemoryCopy(output_value, IEEE_T_POS_INFINITY, 8);
  396. }
  397. RAISE(cvt__overflow);
  398. } else {
  399. /* Adjust bias of exponent */
  400. r[U_R_EXP] -= (U_R_BIAS - 1022);
  401. /* Make room for exponent and sign bit */
  402. r[2] >>= 11;
  403. r[2] |= (r[1] << 21);
  404. r[1] >>= 11;
  405. /* Clear implicit bit */
  406. r[1] &= 0x000FFFFFL;
  407. /* OR in exponent and sign bit */
  408. r[1] |= (r[U_R_EXP] << 20);
  409. r[1] |= (r[U_R_FLAGS] << 31);
  410. if (options & CVT_C_BIG_ENDIAN) {
  411. r[0] = ((r[1] << 24) | (r[1] >> 24));
  412. r[0] |= ((r[1] << 8) & 0x00FF0000L);
  413. r[0] |= ((r[1] >> 8) & 0x0000FF00L);
  414. r[1] = ((r[2] << 24) | (r[2] >> 24));
  415. r[1] |= ((r[2] << 8) & 0x00FF0000L);
  416. r[1] |= ((r[2] >> 8) & 0x0000FF00L);
  417. } else {
  418. r[0] = r[2];
  419. }
  420. RpcpMemoryCopy(output_value, r, 8);
  421. }
  422. }
  423. // end of file: pack_iet.c
  424. }
  425.