strtof.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /****************************************************************
  2. The author of this software is David M. Gay.
  3. Copyright (C) 1998, 2000 by Lucent Technologies
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and
  6. its documentation for any purpose and without fee is hereby
  7. granted, provided that the above copyright notice appear in all
  8. copies and that both that the copyright notice and this
  9. permission notice and warranty disclaimer appear in supporting
  10. documentation, and that the name of Lucent or any of its entities
  11. not be used in advertising or publicity pertaining to
  12. distribution of the software without specific, written prior
  13. permission.
  14. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  17. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  21. THIS SOFTWARE.
  22. ****************************************************************/
  23. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  24. * with " at " changed at "@" and " dot " changed to "."). */
  25. #include "gdtoaimp.h"
  26. float
  27. #ifdef KR_headers
  28. strtof(s, sp) CONST char *s; char **sp;
  29. #else
  30. strtof(CONST char *s, char **sp)
  31. #endif
  32. {
  33. static FPI fpi0 = { 24, 1-127-24+1, 254-127-24+1, 1, SI };
  34. ULong bits[1];
  35. Long exp;
  36. int k;
  37. union { ULong L[1]; float f; } u;
  38. #ifdef Honor_FLT_ROUNDS
  39. #include "gdtoa_fltrnds.h"
  40. #else
  41. #define fpi &fpi0
  42. #endif
  43. k = strtodg(s, sp, fpi, &exp, bits);
  44. switch(k & STRTOG_Retmask) {
  45. case STRTOG_NoNumber:
  46. case STRTOG_Zero:
  47. u.L[0] = 0;
  48. break;
  49. case STRTOG_Normal:
  50. case STRTOG_NaNbits:
  51. u.L[0] = (bits[0] & 0x7fffff) | ((exp + 0x7f + 23) << 23);
  52. break;
  53. case STRTOG_Denormal:
  54. u.L[0] = bits[0];
  55. break;
  56. case STRTOG_NoMemory:
  57. errno = ERANGE;
  58. /* FALLTHROUGH */
  59. case STRTOG_Infinite:
  60. u.L[0] = 0x7f800000;
  61. break;
  62. case STRTOG_NaN:
  63. u.L[0] = f_QNAN;
  64. }
  65. if (k & STRTOG_Neg)
  66. u.L[0] |= 0x80000000L;
  67. return u.f;
  68. }