sqrt.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <u.h>
  2. #include <libc.h>
  3. static long sqtab[64] =
  4. {
  5. 0x6cdb2, 0x726d4, 0x77ea3, 0x7d52f, 0x82a85, 0x87eb1, 0x8d1c0, 0x923bd,
  6. 0x974b2, 0x9c4a8, 0xa13a9, 0xa61be, 0xaaeee, 0xafb41, 0xb46bf, 0xb916e,
  7. 0xbdb55, 0xc247a, 0xc6ce3, 0xcb495, 0xcfb95, 0xd41ea, 0xd8796, 0xdcca0,
  8. 0xe110c, 0xe54dd, 0xe9818, 0xedac0, 0xf1cd9, 0xf5e67, 0xf9f6e, 0xfdfef,
  9. 0x01fe0, 0x05ee6, 0x09cfd, 0x0da30, 0x11687, 0x1520c, 0x18cc8, 0x1c6c1,
  10. 0x20000, 0x2388a, 0x27068, 0x2a79e, 0x2de32, 0x3142b, 0x3498c, 0x37e5b,
  11. 0x3b29d, 0x3e655, 0x41989, 0x44c3b, 0x47e70, 0x4b02b, 0x4e16f, 0x51241,
  12. 0x542a2, 0x57296, 0x5a220, 0x5d142, 0x60000, 0x62e5a, 0x65c55, 0x689f2,
  13. };
  14. double
  15. sqrt(double arg)
  16. {
  17. int e, ms;
  18. double a, t;
  19. union
  20. {
  21. double d;
  22. struct
  23. {
  24. long ms;
  25. long ls;
  26. };
  27. } u;
  28. u.d = arg;
  29. ms = u.ms;
  30. /*
  31. * sign extend the mantissa with
  32. * exponent. result should be > 0 for
  33. * normal case.
  34. */
  35. e = ms >> 20;
  36. if(e <= 0) {
  37. if(e == 0)
  38. return 0;
  39. return NaN();
  40. }
  41. /*
  42. * pick up arg/4 by adjusting exponent
  43. */
  44. u.ms = ms - (2 << 20);
  45. a = u.d;
  46. /*
  47. * use 5 bits of mantissa and 1 bit
  48. * of exponent to form table index.
  49. * insert exponent/2 - 1.
  50. */
  51. e = (((e - 1023) >> 1) + 1022) << 20;
  52. u.ms = *(long*)((char*)sqtab + ((ms >> 13) & 0xfc)) | e;
  53. u.ls = 0;
  54. /*
  55. * three laps of newton
  56. */
  57. e = 1 << 20;
  58. t = u.d;
  59. u.d = t + a/t;
  60. u.ms -= e; /* u.d /= 2; */
  61. t = u.d;
  62. u.d = t + a/t;
  63. u.ms -= e; /* u.d /= 2; */
  64. t = u.d;
  65. return t + a/t;
  66. }
  67. /*
  68. * this is the program that generated the table.
  69. * it calls sqrt by some other means.
  70. *
  71. * void
  72. * main(void)
  73. * {
  74. * int i;
  75. * union U
  76. * {
  77. * double d;
  78. * struct
  79. * {
  80. * long ms;
  81. * long ls;
  82. * };
  83. * } u;
  84. *
  85. * for(i=0; i<64; i++) {
  86. * u.ms = (i<<15) | 0x3fe04000;
  87. * u.ls = 0;
  88. * u.d = sqrt(u.d);
  89. * print(" 0x%.5lux,", u.ms & 0xfffff);
  90. * }
  91. * print("\n");
  92. * exits(0);
  93. * }
  94. */