fixed.odin 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 << Fraction_Width)
  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. Integer_Width :: 8*size_of(Backing) - Fraction_Width
  91. x := x
  92. buf: [48]byte
  93. i := 0
  94. if !intrinsics.type_is_unsigned(Backing) && x.i == min(Backing) {
  95. // edge case handling for signed numbers
  96. buf[i] = '-'
  97. i += 1
  98. i += copy(buf[i:], _power_of_two_table[Integer_Width])
  99. } else {
  100. if x.i < 0 {
  101. buf[i] = '-'
  102. i += 1
  103. x.i = -x.i
  104. }
  105. when size_of(Backing) < 16 {
  106. T :: u64
  107. append_uint :: strconv.append_uint
  108. } else {
  109. T :: u128
  110. append_uint :: strconv.append_u128
  111. }
  112. integer := T(x.i) >> Fraction_Width
  113. fraction := T(x.i) & (1<<Fraction_Width - 1)
  114. s := append_uint(buf[i:], integer, 10)
  115. i += len(s)
  116. if fraction != 0 {
  117. buf[i] = '.'
  118. i += 1
  119. for fraction > 0 {
  120. fraction *= 10
  121. buf[i] = byte('0' + (fraction>>Fraction_Width) % 10)
  122. i += 1
  123. fraction &= 1<<Fraction_Width - 1
  124. }
  125. }
  126. }
  127. n := copy(dst, buf[:i])
  128. return string(dst[:i])
  129. }
  130. @(require_results)
  131. to_string :: proc(x: $T/Fixed($Backing, $Fraction_Width), allocator := context.allocator) -> string {
  132. buf: [48]byte
  133. s := append(buf[:], x)
  134. str := make([]byte, len(s), allocator)
  135. copy(str, s)
  136. return string(str)
  137. }
  138. @(private)
  139. _power_of_two_table := [129]string{
  140. "0.5",
  141. "1",
  142. "2",
  143. "4",
  144. "8",
  145. "16",
  146. "32",
  147. "64",
  148. "128",
  149. "256",
  150. "512",
  151. "1024",
  152. "2048",
  153. "4096",
  154. "8192",
  155. "16384",
  156. "32768",
  157. "65536",
  158. "131072",
  159. "262144",
  160. "524288",
  161. "1048576",
  162. "2097152",
  163. "4194304",
  164. "8388608",
  165. "16777216",
  166. "33554432",
  167. "67108864",
  168. "134217728",
  169. "268435456",
  170. "536870912",
  171. "1073741824",
  172. "2147483648",
  173. "4294967296",
  174. "8589934592",
  175. "17179869184",
  176. "34359738368",
  177. "68719476736",
  178. "137438953472",
  179. "274877906944",
  180. "549755813888",
  181. "1099511627776",
  182. "2199023255552",
  183. "4398046511104",
  184. "8796093022208",
  185. "17592186044416",
  186. "35184372088832",
  187. "70368744177664",
  188. "140737488355328",
  189. "281474976710656",
  190. "562949953421312",
  191. "1125899906842624",
  192. "2251799813685248",
  193. "4503599627370496",
  194. "9007199254740992",
  195. "18014398509481984",
  196. "36028797018963968",
  197. "72057594037927936",
  198. "144115188075855872",
  199. "288230376151711744",
  200. "576460752303423488",
  201. "1152921504606846976",
  202. "2305843009213693952",
  203. "4611686018427387904",
  204. "9223372036854775808",
  205. "18446744073709551616",
  206. "36893488147419103232",
  207. "73786976294838206464",
  208. "147573952589676412928",
  209. "295147905179352825856",
  210. "590295810358705651712",
  211. "1180591620717411303424",
  212. "2361183241434822606848",
  213. "4722366482869645213696",
  214. "9444732965739290427392",
  215. "18889465931478580854784",
  216. "37778931862957161709568",
  217. "75557863725914323419136",
  218. "151115727451828646838272",
  219. "302231454903657293676544",
  220. "604462909807314587353088",
  221. "1208925819614629174706176",
  222. "2417851639229258349412352",
  223. "4835703278458516698824704",
  224. "9671406556917033397649408",
  225. "19342813113834066795298816",
  226. "38685626227668133590597632",
  227. "77371252455336267181195264",
  228. "154742504910672534362390528",
  229. "309485009821345068724781056",
  230. "618970019642690137449562112",
  231. "1237940039285380274899124224",
  232. "2475880078570760549798248448",
  233. "4951760157141521099596496896",
  234. "9903520314283042199192993792",
  235. "19807040628566084398385987584",
  236. "39614081257132168796771975168",
  237. "79228162514264337593543950336",
  238. "158456325028528675187087900672",
  239. "316912650057057350374175801344",
  240. "633825300114114700748351602688",
  241. "1267650600228229401496703205376",
  242. "2535301200456458802993406410752",
  243. "5070602400912917605986812821504",
  244. "10141204801825835211973625643008",
  245. "20282409603651670423947251286016",
  246. "40564819207303340847894502572032",
  247. "81129638414606681695789005144064",
  248. "162259276829213363391578010288128",
  249. "324518553658426726783156020576256",
  250. "649037107316853453566312041152512",
  251. "1298074214633706907132624082305024",
  252. "2596148429267413814265248164610048",
  253. "5192296858534827628530496329220096",
  254. "10384593717069655257060992658440192",
  255. "20769187434139310514121985316880384",
  256. "41538374868278621028243970633760768",
  257. "83076749736557242056487941267521536",
  258. "166153499473114484112975882535043072",
  259. "332306998946228968225951765070086144",
  260. "664613997892457936451903530140172288",
  261. "1329227995784915872903807060280344576",
  262. "2658455991569831745807614120560689152",
  263. "5316911983139663491615228241121378304",
  264. "10633823966279326983230456482242756608",
  265. "21267647932558653966460912964485513216",
  266. "42535295865117307932921825928971026432",
  267. "85070591730234615865843651857942052864",
  268. "170141183460469231731687303715884105728",
  269. }