fixed.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package math_fixed
  2. import "core:math"
  3. import "core:strconv"
  4. import "base:intrinsics"
  5. _, _, _ :: intrinsics, strconv, math
  6. Fixed :: struct($Backing: typeid, $Fraction_Width: uint)
  7. where
  8. intrinsics.type_is_integer(Backing),
  9. 0 <= Fraction_Width,
  10. Fraction_Width <= 8*size_of(Backing) {
  11. i: Backing,
  12. }
  13. Fixed4_4 :: distinct Fixed(i8, 4)
  14. Fixed5_3 :: distinct Fixed(i8, 3)
  15. Fixed6_2 :: distinct Fixed(i8, 2)
  16. Fixed7_1 :: distinct Fixed(i8, 1)
  17. Fixed8_8 :: distinct Fixed(i16, 8)
  18. Fixed13_3 :: distinct Fixed(i16, 3)
  19. Fixed16_16 :: distinct Fixed(i32, 16)
  20. Fixed26_6 :: distinct Fixed(i32, 6)
  21. Fixed32_32 :: distinct Fixed(i64, 32)
  22. Fixed52_12 :: distinct Fixed(i64, 12)
  23. init_from_f64 :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), val: f64) {
  24. i, f := math.modf(math.abs(val))
  25. x.i = Backing(f * (1<<Fraction_Width))
  26. x.i &= 1<<Fraction_Width - 1
  27. x.i |= Backing(i) << Fraction_Width
  28. if val < 0 {
  29. x.i *= -1
  30. }
  31. }
  32. init_from_parts :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), integer, fraction: Backing) {
  33. x.i = fraction
  34. x.i &= 1<<Fraction_Width - 1
  35. x.i |= integer
  36. }
  37. to_f64 :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> f64 {
  38. sign := -1.0 if x.i < 0 else 1.0
  39. num := math.abs(x.i)
  40. res := f64(num >> Fraction_Width)
  41. res += f64(num & (1<<Fraction_Width-1)) / f64(1<<Fraction_Width)
  42. return res * sign
  43. }
  44. @(require_results)
  45. add :: proc(x, y: $T/Fixed) -> T {
  46. return {x.i + y.i}
  47. }
  48. @(require_results)
  49. sub :: proc(x, y: $T/Fixed) -> T {
  50. return {x.i - y.i}
  51. }
  52. @(require_results)
  53. mul :: proc(x, y: $T/Fixed($Backing, $Fraction_Width)) -> (z: T) {
  54. z.i = intrinsics.fixed_point_mul(x.i, y.i, Fraction_Width)
  55. return
  56. }
  57. @(require_results)
  58. mul_sat :: proc(x, y: $T/Fixed($Backing, $Fraction_Width)) -> (z: T) {
  59. z.i = intrinsics.fixed_point_mul_sat(x.i, y.i, Fraction_Width)
  60. return
  61. }
  62. @(require_results)
  63. div :: proc(x, y: $T/Fixed($Backing, $Fraction_Width)) -> (z: T) {
  64. z.i = intrinsics.fixed_point_div(x.i, y.i, Fraction_Width)
  65. return
  66. }
  67. @(require_results)
  68. div_sat :: proc(x, y: $T/Fixed($Backing, $Fraction_Width)) -> (z: T) {
  69. z.i = intrinsics.fixed_point_div_sat(x.i, y.i, Fraction_Width)
  70. return
  71. }
  72. @(require_results)
  73. floor :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing {
  74. if x.i >= 0 {
  75. return x.i >> Fraction_Width
  76. } else {
  77. return (x.i - (1 << (Fraction_Width - 1)) + (1 << (Fraction_Width - 2))) >> Fraction_Width
  78. }
  79. }
  80. @(require_results)
  81. ceil :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing {
  82. return (x.i + (1 << Fraction_Width - 1)) >> Fraction_Width
  83. }
  84. @(require_results)
  85. round :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing {
  86. return (x.i + (1 << (Fraction_Width - 1))) >> Fraction_Width
  87. }
  88. @(require_results)
  89. append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
  90. x := x
  91. buf: [48]byte
  92. i := 0
  93. if x.i < 0 {
  94. buf[i] = '-'
  95. i += 1
  96. x.i = -x.i
  97. }
  98. integer := x.i >> Fraction_Width
  99. fraction := x.i & (1<<Fraction_Width - 1)
  100. s := strconv.append_uint(buf[i:], u64(integer), 10)
  101. i += len(s)
  102. if fraction != 0 {
  103. buf[i] = '.'
  104. i += 1
  105. for fraction > 0 {
  106. fraction *= 10
  107. buf[i] = byte('0' + (fraction>>Fraction_Width))
  108. i += 1
  109. fraction &= 1<<Fraction_Width - 1
  110. }
  111. }
  112. n := copy(dst, buf[:i])
  113. return string(dst[:i])
  114. }
  115. @(require_results)
  116. to_string :: proc(x: $T/Fixed($Backing, $Fraction_Width), allocator := context.allocator) -> string {
  117. buf: [48]byte
  118. s := append(buf[:], x)
  119. str := make([]byte, len(s), allocator)
  120. copy(str, s)
  121. return string(str)
  122. }