copysign.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <ansidecl.h>
  2. #ifdef __IEEE_BIG_ENDIAN
  3. typedef union
  4. {
  5. double value;
  6. struct
  7. {
  8. unsigned int sign : 1;
  9. unsigned int exponent: 11;
  10. unsigned int fraction0:4;
  11. unsigned int fraction1:16;
  12. unsigned int fraction2:16;
  13. unsigned int fraction3:16;
  14. } number;
  15. struct
  16. {
  17. unsigned int sign : 1;
  18. unsigned int exponent: 11;
  19. unsigned int quiet:1;
  20. unsigned int function0:3;
  21. unsigned int function1:16;
  22. unsigned int function2:16;
  23. unsigned int function3:16;
  24. } nan;
  25. struct
  26. {
  27. unsigned long msw;
  28. unsigned long lsw;
  29. } parts;
  30. long aslong[2];
  31. } __ieee_double_shape_type;
  32. #endif
  33. #ifdef __IEEE_LITTLE_ENDIAN
  34. typedef union
  35. {
  36. double value;
  37. struct
  38. {
  39. #ifdef __SMALL_BITFIELDS
  40. unsigned int fraction3:16;
  41. unsigned int fraction2:16;
  42. unsigned int fraction1:16;
  43. unsigned int fraction0: 4;
  44. #else
  45. unsigned int fraction1:32;
  46. unsigned int fraction0:20;
  47. #endif
  48. unsigned int exponent :11;
  49. unsigned int sign : 1;
  50. } number;
  51. struct
  52. {
  53. #ifdef __SMALL_BITFIELDS
  54. unsigned int function3:16;
  55. unsigned int function2:16;
  56. unsigned int function1:16;
  57. unsigned int function0:3;
  58. #else
  59. unsigned int function1:32;
  60. unsigned int function0:19;
  61. #endif
  62. unsigned int quiet:1;
  63. unsigned int exponent: 11;
  64. unsigned int sign : 1;
  65. } nan;
  66. struct
  67. {
  68. unsigned long lsw;
  69. unsigned long msw;
  70. } parts;
  71. long aslong[2];
  72. } __ieee_double_shape_type;
  73. #endif
  74. #ifdef __IEEE_BIG_ENDIAN
  75. typedef union
  76. {
  77. float value;
  78. struct
  79. {
  80. unsigned int sign : 1;
  81. unsigned int exponent: 8;
  82. unsigned int fraction0: 7;
  83. unsigned int fraction1: 16;
  84. } number;
  85. struct
  86. {
  87. unsigned int sign:1;
  88. unsigned int exponent:8;
  89. unsigned int quiet:1;
  90. unsigned int function0:6;
  91. unsigned int function1:16;
  92. } nan;
  93. long p1;
  94. } __ieee_float_shape_type;
  95. #endif
  96. #ifdef __IEEE_LITTLE_ENDIAN
  97. typedef union
  98. {
  99. float value;
  100. struct
  101. {
  102. unsigned int fraction0: 7;
  103. unsigned int fraction1: 16;
  104. unsigned int exponent: 8;
  105. unsigned int sign : 1;
  106. } number;
  107. struct
  108. {
  109. unsigned int function1:16;
  110. unsigned int function0:6;
  111. unsigned int quiet:1;
  112. unsigned int exponent:8;
  113. unsigned int sign:1;
  114. } nan;
  115. long p1;
  116. } __ieee_float_shape_type;
  117. #endif
  118. #if defined(__IEEE_BIG_ENDIAN) || defined(__IEEE_LITTLE_ENDIAN)
  119. double
  120. copysign (double x, double y)
  121. {
  122. __ieee_double_shape_type a,b;
  123. b.value = y;
  124. a.value = x;
  125. a.number.sign =b.number.sign;
  126. return a.value;
  127. }
  128. #else
  129. double
  130. copysign (double x, double y)
  131. {
  132. if ((x < 0 && y > 0) || (x > 0 && y < 0))
  133. return -x;
  134. return x;
  135. }
  136. #endif