timeconst.bc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. scale=0
  2. define gcd(a,b) {
  3. auto t;
  4. while (b) {
  5. t = b;
  6. b = a % b;
  7. a = t;
  8. }
  9. return a;
  10. }
  11. /* Division by reciprocal multiplication. */
  12. define fmul(b,n,d) {
  13. return (2^b*n+d-1)/d;
  14. }
  15. /* Adjustment factor when a ceiling value is used. Use as:
  16. (imul * n) + (fmulxx * n + fadjxx) >> xx) */
  17. define fadj(b,n,d) {
  18. auto v;
  19. d = d/gcd(n,d);
  20. v = 2^b*(d-1)/d;
  21. return v;
  22. }
  23. /* Compute the appropriate mul/adj values as well as a shift count,
  24. which brings the mul value into the range 2^b-1 <= x < 2^b. Such
  25. a shift value will be correct in the signed integer range and off
  26. by at most one in the upper half of the unsigned range. */
  27. define fmuls(b,n,d) {
  28. auto s, m;
  29. for (s = 0; 1; s++) {
  30. m = fmul(s,n,d);
  31. if (m >= 2^(b-1))
  32. return s;
  33. }
  34. return 0;
  35. }
  36. define timeconst(hz) {
  37. print "/* Automatically generated by kernel/time/timeconst.bc */\n"
  38. print "/* Time conversion constants for HZ == ", hz, " */\n"
  39. print "\n"
  40. print "#ifndef KERNEL_TIMECONST_H\n"
  41. print "#define KERNEL_TIMECONST_H\n\n"
  42. print "#include <linux/param.h>\n"
  43. print "#include <linux/types.h>\n\n"
  44. print "#if HZ != ", hz, "\n"
  45. print "#error \qinclude/generated/timeconst.h has the wrong HZ value!\q\n"
  46. print "#endif\n\n"
  47. if (hz < 2) {
  48. print "#error Totally bogus HZ value!\n"
  49. } else {
  50. s=fmuls(32,1000,hz)
  51. obase=16
  52. print "#define HZ_TO_MSEC_MUL32\tU64_C(0x", fmul(s,1000,hz), ")\n"
  53. print "#define HZ_TO_MSEC_ADJ32\tU64_C(0x", fadj(s,1000,hz), ")\n"
  54. obase=10
  55. print "#define HZ_TO_MSEC_SHR32\t", s, "\n"
  56. s=fmuls(32,hz,1000)
  57. obase=16
  58. print "#define MSEC_TO_HZ_MUL32\tU64_C(0x", fmul(s,hz,1000), ")\n"
  59. print "#define MSEC_TO_HZ_ADJ32\tU64_C(0x", fadj(s,hz,1000), ")\n"
  60. obase=10
  61. print "#define MSEC_TO_HZ_SHR32\t", s, "\n"
  62. obase=10
  63. cd=gcd(hz,1000)
  64. print "#define HZ_TO_MSEC_NUM\t\t", 1000/cd, "\n"
  65. print "#define HZ_TO_MSEC_DEN\t\t", hz/cd, "\n"
  66. print "#define MSEC_TO_HZ_NUM\t\t", hz/cd, "\n"
  67. print "#define MSEC_TO_HZ_DEN\t\t", 1000/cd, "\n"
  68. print "\n"
  69. s=fmuls(32,1000000,hz)
  70. obase=16
  71. print "#define HZ_TO_USEC_MUL32\tU64_C(0x", fmul(s,1000000,hz), ")\n"
  72. print "#define HZ_TO_USEC_ADJ32\tU64_C(0x", fadj(s,1000000,hz), ")\n"
  73. obase=10
  74. print "#define HZ_TO_USEC_SHR32\t", s, "\n"
  75. s=fmuls(32,hz,1000000)
  76. obase=16
  77. print "#define USEC_TO_HZ_MUL32\tU64_C(0x", fmul(s,hz,1000000), ")\n"
  78. print "#define USEC_TO_HZ_ADJ32\tU64_C(0x", fadj(s,hz,1000000), ")\n"
  79. obase=10
  80. print "#define USEC_TO_HZ_SHR32\t", s, "\n"
  81. obase=10
  82. cd=gcd(hz,1000000)
  83. print "#define HZ_TO_USEC_NUM\t\t", 1000000/cd, "\n"
  84. print "#define HZ_TO_USEC_DEN\t\t", hz/cd, "\n"
  85. print "#define USEC_TO_HZ_NUM\t\t", hz/cd, "\n"
  86. print "#define USEC_TO_HZ_DEN\t\t", 1000000/cd, "\n"
  87. cd=gcd(hz,1000000000)
  88. print "#define HZ_TO_NSEC_NUM\t\t", 1000000000/cd, "\n"
  89. print "#define HZ_TO_NSEC_DEN\t\t", hz/cd, "\n"
  90. print "#define NSEC_TO_HZ_NUM\t\t", hz/cd, "\n"
  91. print "#define NSEC_TO_HZ_DEN\t\t", 1000000000/cd, "\n"
  92. print "\n"
  93. print "#endif /* KERNEL_TIMECONST_H */\n"
  94. }
  95. halt
  96. }
  97. hz = read();
  98. timeconst(hz)