Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

  1. /***
  2. *ldiv.c - contains the ldiv routine
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Performs a signed divide on longs and returns quotient
  8. * and remainder.
  9. *
  10. *Revision History:
  11. * 06-02-89 PHG module created
  12. * 03-14-90 GJF Made calling type _CALLTYPE1 and added #include
  13. * <cruntime.h>. Also, fixed the copyright.
  14. * 10-04-90 GJF New-style function declarator.
  15. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  16. *
  17. *******************************************************************************/
  18. #include <cruntime.h>
  19. #include <stdlib.h>
  20. /***
  21. *ldiv_t div(long numer, long denom) - do signed divide
  22. *
  23. *Purpose:
  24. * This routine does an long divide and returns the results.
  25. * Since we don't know how the Intel 860 does division, we'd
  26. * better make sure that we have done it right.
  27. *
  28. *Entry:
  29. * long numer - Numerator passed in on stack
  30. * long denom - Denominator passed in on stack
  31. *
  32. *Exit:
  33. * returns quotient and remainder in structure
  34. *
  35. *Exceptions:
  36. * No validation is done on [denom]* thus, if [denom] is 0,
  37. * this routine will trap.
  38. *
  39. *******************************************************************************/
  40. ldiv_t __cdecl ldiv (
  41. long numer,
  42. long denom
  43. )
  44. {
  45. ldiv_t result;
  46. result.quot = numer / denom;
  47. result.rem = numer % denom;
  48. if (numer < 0 && result.rem > 0) {
  49. /* did division wrong; must fix up */
  50. ++result.quot;
  51. result.rem -= denom;
  52. }
  53. return result;
  54. }