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.

92 lines
2.1 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. recip.c
  5. Abstract:
  6. This module generates reciprocol fractions for implementing integer
  7. division by multiplication.
  8. Author:
  9. David N. Cutler (davec) 13-May-1989
  10. Environment:
  11. User mode.
  12. Revision History:
  13. --*/
  14. #include <stdio.h>
  15. typedef struct _large_integer {
  16. unsigned long LowPart;
  17. long HighPart;
  18. } large_integer;
  19. //long Divisors[] = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0};
  20. long Divisors[] = {10, 10000, 10000000, 86400000, 0};
  21. void
  22. main (argc, argv)
  23. int argc;
  24. char *argv[];
  25. {
  26. large_integer Fraction;
  27. long Index;
  28. long NumberBits;
  29. long Remainder;
  30. long i;
  31. //
  32. // Compute first few reciprocols.
  33. //
  34. for (Index = Divisors[i = 0]; Index != 0L; Index = Divisors[++i]) {
  35. NumberBits = 0L;
  36. Remainder = 1L;
  37. Fraction.LowPart = 0L;
  38. Fraction.HighPart = 0L;
  39. while (Fraction.HighPart >= 0L) {
  40. NumberBits += 1L;
  41. Fraction.HighPart <<= 1L;
  42. if ((Fraction.LowPart & 0x80000000) != 0L) {
  43. Fraction.HighPart += 1L;
  44. }
  45. Fraction.LowPart <<= 1L;
  46. Remainder <<= 1L;
  47. if (Remainder >= Index) {
  48. Remainder -= Index;
  49. Fraction.LowPart |= 1L;
  50. }
  51. }
  52. if (Remainder) {
  53. if ((Fraction.LowPart == -1L) && (Fraction.HighPart == -1L)) {
  54. Fraction.LowPart = 0L;
  55. Fraction.HighPart = 0x80000000;
  56. NumberBits -= 1L;
  57. } else {
  58. if (Fraction.LowPart == -1L) {
  59. Fraction.LowPart = 0L;
  60. Fraction.HighPart += 1L;
  61. } else {
  62. Fraction.LowPart += 1L;
  63. }
  64. }
  65. }
  66. printf("Divisor %2ld, Fraction %8lx, %8lx Shift %ld\n", Index,
  67. Fraction.HighPart, Fraction.LowPart, NumberBits - 64L);
  68. }
  69. return;
  70. }