math.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. // TODO: Is there a compiler flag we can use to still get these math functions? (and compile with -nostdlib)
  27. // FIXME: What do we do with this regarding copyright stuff?
  28. // code for math functions taken from here:
  29. // https://gitlab.incom.co/CM-Shield/u-boot/commit/aa7839b39c2ee77f9ab8c393c56b8d812507dbb7
  30. // https://github.com/zayac/qemu-arm/blob/master/qemu/roms/ipxe/src/libgcc/__udivmoddi4.c
  31. // https://code.woboq.org/llvm/compiler-rt/lib/builtins/divdi3.c.html
  32. #include "math.h"
  33. extern "C" {
  34. union overlay64 {
  35. u64 longw;
  36. struct {
  37. u32 lower;
  38. u32 higher;
  39. } words;
  40. };
  41. u64 __ashldi3(u64 num, unsigned int shift)
  42. {
  43. union overlay64 output;
  44. output.longw = num;
  45. if (shift >= 32) {
  46. output.words.higher = output.words.lower << (shift - 32);
  47. output.words.lower = 0;
  48. } else {
  49. if (!shift)
  50. return num;
  51. output.words.higher = (output.words.higher << shift) | (output.words.lower >> (32 - shift));
  52. output.words.lower = output.words.lower << shift;
  53. }
  54. return output.longw;
  55. }
  56. u64 __lshrdi3(u64 num, unsigned int shift)
  57. {
  58. union overlay64 output;
  59. output.longw = num;
  60. if (shift >= 32) {
  61. output.words.lower = output.words.higher >> (shift - 32);
  62. output.words.higher = 0;
  63. } else {
  64. if (!shift)
  65. return num;
  66. output.words.lower = output.words.lower >> shift | (output.words.higher << (32 - shift));
  67. output.words.higher = output.words.higher >> shift;
  68. }
  69. return output.longw;
  70. }
  71. #define MAX_32BIT_UINT ((((u64)1) << 32) - 1)
  72. static u64 _64bit_divide(u64 dividend, u64 divider, u64* rem_p)
  73. {
  74. u64 result = 0;
  75. /*
  76. * If divider is zero - let the rest of the system care about the
  77. * exception.
  78. */
  79. if (!divider)
  80. return 1 / (u32)divider;
  81. /* As an optimization, let's not use 64 bit division unless we must. */
  82. if (dividend <= MAX_32BIT_UINT) {
  83. if (divider > MAX_32BIT_UINT) {
  84. result = 0;
  85. if (rem_p)
  86. *rem_p = divider;
  87. } else {
  88. result = (u32)dividend / (u32)divider;
  89. if (rem_p)
  90. *rem_p = (u32)dividend % (u32)divider;
  91. }
  92. return result;
  93. }
  94. while (divider <= dividend) {
  95. u64 locald = divider;
  96. u64 limit = __lshrdi3(dividend, 1);
  97. int shifts = 0;
  98. while (locald <= limit) {
  99. shifts++;
  100. locald = locald + locald;
  101. }
  102. result |= __ashldi3(1, shifts);
  103. dividend -= locald;
  104. }
  105. if (rem_p)
  106. *rem_p = dividend;
  107. return result;
  108. }
  109. u64 __udivdi3(u64 num, u64 den)
  110. {
  111. return _64bit_divide(num, den, nullptr);
  112. }
  113. u64 __umoddi3(u64 num, u64 den)
  114. {
  115. u64 v = 0;
  116. _64bit_divide(num, den, &v);
  117. return v;
  118. }
  119. uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t* rem_p)
  120. {
  121. uint64_t quot = 0, qbit = 1;
  122. if (den == 0) {
  123. return 1 / ((unsigned)den); /* Intentional divide by zero, without
  124. triggering a compiler warning which
  125. would abort the build */
  126. }
  127. /* Left-justify denominator and count shift */
  128. while ((int64_t)den >= 0) {
  129. den <<= 1;
  130. qbit <<= 1;
  131. }
  132. while (qbit) {
  133. if (den <= num) {
  134. num -= den;
  135. quot += qbit;
  136. }
  137. den >>= 1;
  138. qbit >>= 1;
  139. }
  140. if (rem_p)
  141. *rem_p = num;
  142. return quot;
  143. }
  144. int64_t __divdi3(int64_t a, int64_t b)
  145. {
  146. const int bits_in_dword_m1 = (int)(sizeof(int64_t) * 8) - 1;
  147. int64_t s_a = a >> bits_in_dword_m1; // s_a = a < 0 ? -1 : 0
  148. int64_t s_b = b >> bits_in_dword_m1; // s_b = b < 0 ? -1 : 0
  149. a = (a ^ s_a) - s_a; // negate if s_a == -1
  150. b = (b ^ s_b) - s_b; // negate if s_b == -1
  151. s_a ^= s_b; // sign of quotient
  152. return (__udivmoddi4(a, b, (uint64_t*)0) ^ s_a) - s_a; // negate if s_a == -1
  153. }
  154. int64_t __moddi3(int64_t a, int64_t b)
  155. {
  156. const int bits_in_dword_m1 = (int)(sizeof(int64_t) * 8) - 1;
  157. int64_t s = b >> bits_in_dword_m1; // s = b < 0 ? -1 : 0
  158. b = (b ^ s) - s; // negate if s == -1
  159. s = a >> bits_in_dword_m1; // s = a < 0 ? -1 : 0
  160. a = (a ^ s) - s; // negate if s == -1
  161. uint64_t r;
  162. __udivmoddi4(a, b, &r);
  163. return ((int64_t)r ^ s) - s; // negate if s == -1
  164. }
  165. }