__udivsi3.c 748 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-10-09 Bernard the first version for i386
  9. */
  10. #include <stdint.h>
  11. uint32_t __udivsi3(uint32_t num, uint32_t den)
  12. {
  13. uint32_t quot = 0, qbit = 1;
  14. if (den == 0)
  15. {
  16. asm volatile ("int $0");
  17. return 0; /* If trap returns... */
  18. }
  19. /* Left-justify denominator and count shift */
  20. while ((int32_t) den >= 0)
  21. {
  22. den <<= 1;
  23. qbit <<= 1;
  24. }
  25. while (qbit)
  26. {
  27. if (den <= num)
  28. {
  29. num -= den;
  30. quot += qbit;
  31. }
  32. den >>= 1;
  33. qbit >>= 1;
  34. }
  35. return quot;
  36. }