fixed_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fixed
  5. import (
  6. "math"
  7. "math/rand"
  8. "testing"
  9. )
  10. var testCases = []struct {
  11. x float64
  12. s26_6 string
  13. s52_12 string
  14. floor int
  15. round int
  16. ceil int
  17. }{{
  18. x: 0,
  19. s26_6: "0:00",
  20. s52_12: "0:0000",
  21. floor: 0,
  22. round: 0,
  23. ceil: 0,
  24. }, {
  25. x: 1,
  26. s26_6: "1:00",
  27. s52_12: "1:0000",
  28. floor: 1,
  29. round: 1,
  30. ceil: 1,
  31. }, {
  32. x: 1.25,
  33. s26_6: "1:16",
  34. s52_12: "1:1024",
  35. floor: 1,
  36. round: 1,
  37. ceil: 2,
  38. }, {
  39. x: 2.5,
  40. s26_6: "2:32",
  41. s52_12: "2:2048",
  42. floor: 2,
  43. round: 3,
  44. ceil: 3,
  45. }, {
  46. x: 63 / 64.0,
  47. s26_6: "0:63",
  48. s52_12: "0:4032",
  49. floor: 0,
  50. round: 1,
  51. ceil: 1,
  52. }, {
  53. x: -0.5,
  54. s26_6: "-0:32",
  55. s52_12: "-0:2048",
  56. floor: -1,
  57. round: +0,
  58. ceil: +0,
  59. }, {
  60. x: -4.125,
  61. s26_6: "-4:08",
  62. s52_12: "-4:0512",
  63. floor: -5,
  64. round: -4,
  65. ceil: -4,
  66. }, {
  67. x: -7.75,
  68. s26_6: "-7:48",
  69. s52_12: "-7:3072",
  70. floor: -8,
  71. round: -8,
  72. ceil: -7,
  73. }}
  74. func TestInt26_6(t *testing.T) {
  75. const one = Int26_6(1 << 6)
  76. for _, tc := range testCases {
  77. x := Int26_6(tc.x * (1 << 6))
  78. if got, want := x.String(), tc.s26_6; got != want {
  79. t.Errorf("tc.x=%v: String: got %q, want %q", tc.x, got, want)
  80. }
  81. if got, want := x.Floor(), tc.floor; got != want {
  82. t.Errorf("tc.x=%v: Floor: got %v, want %v", tc.x, got, want)
  83. }
  84. if got, want := x.Round(), tc.round; got != want {
  85. t.Errorf("tc.x=%v: Round: got %v, want %v", tc.x, got, want)
  86. }
  87. if got, want := x.Ceil(), tc.ceil; got != want {
  88. t.Errorf("tc.x=%v: Ceil: got %v, want %v", tc.x, got, want)
  89. }
  90. if got, want := x.Mul(one), x; got != want {
  91. t.Errorf("tc.x=%v: Mul by one: got %v, want %v", tc.x, got, want)
  92. }
  93. if got, want := x.mul(one), x; got != want {
  94. t.Errorf("tc.x=%v: mul by one: got %v, want %v", tc.x, got, want)
  95. }
  96. }
  97. }
  98. func TestInt52_12(t *testing.T) {
  99. const one = Int52_12(1 << 12)
  100. for _, tc := range testCases {
  101. x := Int52_12(tc.x * (1 << 12))
  102. if got, want := x.String(), tc.s52_12; got != want {
  103. t.Errorf("tc.x=%v: String: got %q, want %q", tc.x, got, want)
  104. }
  105. if got, want := x.Floor(), tc.floor; got != want {
  106. t.Errorf("tc.x=%v: Floor: got %v, want %v", tc.x, got, want)
  107. }
  108. if got, want := x.Round(), tc.round; got != want {
  109. t.Errorf("tc.x=%v: Round: got %v, want %v", tc.x, got, want)
  110. }
  111. if got, want := x.Ceil(), tc.ceil; got != want {
  112. t.Errorf("tc.x=%v: Ceil: got %v, want %v", tc.x, got, want)
  113. }
  114. if got, want := x.Mul(one), x; got != want {
  115. t.Errorf("tc.x=%v: Mul by one: got %v, want %v", tc.x, got, want)
  116. }
  117. }
  118. }
  119. var mulTestCases = []struct {
  120. x float64
  121. y float64
  122. z26_6 float64 // Equals truncate26_6(x)*truncate26_6(y).
  123. z52_12 float64 // Equals truncate52_12(x)*truncate52_12(y).
  124. s26_6 string
  125. s52_12 string
  126. }{{
  127. x: 0,
  128. y: 1.5,
  129. z26_6: 0,
  130. z52_12: 0,
  131. s26_6: "0:00",
  132. s52_12: "0:0000",
  133. }, {
  134. x: +1.25,
  135. y: +4,
  136. z26_6: +5,
  137. z52_12: +5,
  138. s26_6: "5:00",
  139. s52_12: "5:0000",
  140. }, {
  141. x: +1.25,
  142. y: -4,
  143. z26_6: -5,
  144. z52_12: -5,
  145. s26_6: "-5:00",
  146. s52_12: "-5:0000",
  147. }, {
  148. x: -1.25,
  149. y: +4,
  150. z26_6: -5,
  151. z52_12: -5,
  152. s26_6: "-5:00",
  153. s52_12: "-5:0000",
  154. }, {
  155. x: -1.25,
  156. y: -4,
  157. z26_6: +5,
  158. z52_12: +5,
  159. s26_6: "5:00",
  160. s52_12: "5:0000",
  161. }, {
  162. x: 1.25,
  163. y: 1.5,
  164. z26_6: 1.875,
  165. z52_12: 1.875,
  166. s26_6: "1:56",
  167. s52_12: "1:3584",
  168. }, {
  169. x: 1234.5,
  170. y: -8888.875,
  171. z26_6: -10973316.1875,
  172. z52_12: -10973316.1875,
  173. s26_6: "-10973316:12",
  174. s52_12: "-10973316:0768",
  175. }, {
  176. x: 1.515625, // 1 + 33/64 = 97/64
  177. y: 1.531250, // 1 + 34/64 = 98/64
  178. z26_6: 2.32080078125, // 2 + 1314/4096 = 9506/4096
  179. z52_12: 2.32080078125, // 2 + 1314/4096 = 9506/4096
  180. s26_6: "2:21", // 2.32812500000, which is closer than 2:20 (in decimal, 2.3125)
  181. s52_12: "2:1314", // 2.32080078125
  182. }, {
  183. x: 0.500244140625, // 2049/4096, approximately 32/64
  184. y: 0.500732421875, // 2051/4096, approximately 32/64
  185. z26_6: 0.25, // 4194304/16777216, or 1024/4096
  186. z52_12: 0.2504884600639343, // 4202499/16777216
  187. s26_6: "0:16", // 0.25000000000
  188. s52_12: "0:1026", // 0.25048828125, which is closer than 0:1027 (in decimal, 0.250732421875)
  189. }, {
  190. x: 0.015625, // 1/64
  191. y: 0.000244140625, // 1/4096, approximately 0/64
  192. z26_6: 0.0, // 0
  193. z52_12: 0.000003814697265625, // 1/262144
  194. s26_6: "0:00", // 0
  195. s52_12: "0:0000", // 0, which is closer than 0:0001 (in decimal, 0.000244140625)
  196. }, {
  197. // Round the Int52_12 calculation down.
  198. x: 1.44140625, // 1 + 1808/4096 = 5904/4096, approximately 92/64
  199. y: 1.44140625, // 1 + 1808/4096 = 5904/4096, approximately 92/64
  200. z26_6: 2.06640625, // 2 + 272/4096 = 8464/4096
  201. z52_12: 2.0776519775390625, // 2 + 318/4096 + 256/16777216 = 34857216/16777216
  202. s26_6: "2:04", // 2.06250000000, which is closer than 2:05 (in decimal, 2.078125000000)
  203. s52_12: "2:0318", // 2.07763671875, which is closer than 2:0319 (in decimal, 2.077880859375)
  204. }, {
  205. // Round the Int52_12 calculation up.
  206. x: 1.44140625, // 1 + 1808/4096 = 5904/4096, approximately 92/64
  207. y: 1.441650390625, // 1 + 1809/4096 = 5905/4096, approximately 92/64
  208. z26_6: 2.06640625, // 2 + 272/4096 = 8464/4096
  209. z52_12: 2.0780038833618164, // 2 + 319/4096 + 2064/16777216 = 34863120/16777216
  210. s26_6: "2:04", // 2.06250000000, which is closer than 2:05 (in decimal, 2.078125000000)
  211. s52_12: "2:0320", // 2.07812500000, which is closer than 2:0319 (in decimal, 2.077880859375)
  212. }}
  213. func TestInt26_6Mul(t *testing.T) {
  214. for _, tc := range mulTestCases {
  215. x := Int26_6(tc.x * (1 << 6))
  216. y := Int26_6(tc.y * (1 << 6))
  217. if z := float64(x) * float64(y) / (1 << 12); z != tc.z26_6 {
  218. t.Errorf("tc.x=%v, tc.y=%v: z: got %v, want %v", tc.x, tc.y, z, tc.z26_6)
  219. continue
  220. }
  221. if got, want := x.Mul(y).String(), tc.s26_6; got != want {
  222. t.Errorf("tc.x=%v: Mul: got %q, want %q", tc.x, got, want)
  223. }
  224. }
  225. }
  226. func TestInt52_12Mul(t *testing.T) {
  227. for _, tc := range mulTestCases {
  228. x := Int52_12(tc.x * (1 << 12))
  229. y := Int52_12(tc.y * (1 << 12))
  230. if z := float64(x) * float64(y) / (1 << 24); z != tc.z52_12 {
  231. t.Errorf("tc.x=%v, tc.y=%v: z: got %v, want %v", tc.x, tc.y, z, tc.z52_12)
  232. continue
  233. }
  234. if got, want := x.Mul(y).String(), tc.s52_12; got != want {
  235. t.Errorf("tc.x=%v: Mul: got %q, want %q", tc.x, got, want)
  236. }
  237. }
  238. }
  239. func TestInt26_6MulByOneMinusIota(t *testing.T) {
  240. const (
  241. totalBits = 32
  242. fracBits = 6
  243. oneMinusIota = Int26_6(1<<fracBits) - 1
  244. oneMinusIotaF = float64(oneMinusIota) / (1 << fracBits)
  245. )
  246. for _, neg := range []bool{false, true} {
  247. for i := uint(0); i < totalBits; i++ {
  248. x := Int26_6(1 << i)
  249. if neg {
  250. x = -x
  251. } else if i == totalBits-1 {
  252. // A signed int32 can't represent 1<<31.
  253. continue
  254. }
  255. // want equals x * oneMinusIota, rounded to nearest.
  256. want := Int26_6(0)
  257. if -1<<fracBits < x && x < 1<<fracBits {
  258. // (x * oneMinusIota) isn't exactly representable as an
  259. // Int26_6. Calculate the rounded value using float64 math.
  260. xF := float64(x) / (1 << fracBits)
  261. wantF := xF * oneMinusIotaF * (1 << fracBits)
  262. want = Int26_6(math.Floor(wantF + 0.5))
  263. } else {
  264. // (x * oneMinusIota) is exactly representable.
  265. want = oneMinusIota << (i - fracBits)
  266. if neg {
  267. want = -want
  268. }
  269. }
  270. if got := x.Mul(oneMinusIota); got != want {
  271. t.Errorf("neg=%t, i=%d, x=%v, Mul: got %v, want %v", neg, i, x, got, want)
  272. }
  273. if got := x.mul(oneMinusIota); got != want {
  274. t.Errorf("neg=%t, i=%d, x=%v, mul: got %v, want %v", neg, i, x, got, want)
  275. }
  276. }
  277. }
  278. }
  279. func TestInt52_12MulByOneMinusIota(t *testing.T) {
  280. const (
  281. totalBits = 64
  282. fracBits = 12
  283. oneMinusIota = Int52_12(1<<fracBits) - 1
  284. oneMinusIotaF = float64(oneMinusIota) / (1 << fracBits)
  285. )
  286. for _, neg := range []bool{false, true} {
  287. for i := uint(0); i < totalBits; i++ {
  288. x := Int52_12(1 << i)
  289. if neg {
  290. x = -x
  291. } else if i == totalBits-1 {
  292. // A signed int64 can't represent 1<<63.
  293. continue
  294. }
  295. // want equals x * oneMinusIota, rounded to nearest.
  296. want := Int52_12(0)
  297. if -1<<fracBits < x && x < 1<<fracBits {
  298. // (x * oneMinusIota) isn't exactly representable as an
  299. // Int52_12. Calculate the rounded value using float64 math.
  300. xF := float64(x) / (1 << fracBits)
  301. wantF := xF * oneMinusIotaF * (1 << fracBits)
  302. want = Int52_12(math.Floor(wantF + 0.5))
  303. } else {
  304. // (x * oneMinusIota) is exactly representable.
  305. want = oneMinusIota << (i - fracBits)
  306. if neg {
  307. want = -want
  308. }
  309. }
  310. if got := x.Mul(oneMinusIota); got != want {
  311. t.Errorf("neg=%t, i=%d, x=%v, Mul: got %v, want %v", neg, i, x, got, want)
  312. }
  313. }
  314. }
  315. }
  316. func TestInt26_6MulVsMul(t *testing.T) {
  317. rng := rand.New(rand.NewSource(1))
  318. for i := 0; i < 10000; i++ {
  319. u := Int26_6(rng.Uint32())
  320. v := Int26_6(rng.Uint32())
  321. Mul := u.Mul(v)
  322. mul := u.mul(v)
  323. if Mul != mul {
  324. t.Errorf("u=%#08x, v=%#08x: Mul=%#08x and mul=%#08x differ",
  325. uint32(u), uint32(v), uint32(Mul), uint32(mul))
  326. }
  327. }
  328. }
  329. func TestMuli32(t *testing.T) {
  330. rng := rand.New(rand.NewSource(2))
  331. for i := 0; i < 10000; i++ {
  332. u := int32(rng.Uint32())
  333. v := int32(rng.Uint32())
  334. lo, hi := muli32(u, v)
  335. got := uint64(lo) | uint64(hi)<<32
  336. want := uint64(int64(u) * int64(v))
  337. if got != want {
  338. t.Errorf("u=%#08x, v=%#08x: got %#016x, want %#016x", uint32(u), uint32(v), got, want)
  339. }
  340. }
  341. }
  342. func TestMulu32(t *testing.T) {
  343. rng := rand.New(rand.NewSource(3))
  344. for i := 0; i < 10000; i++ {
  345. u := rng.Uint32()
  346. v := rng.Uint32()
  347. lo, hi := mulu32(u, v)
  348. got := uint64(lo) | uint64(hi)<<32
  349. want := uint64(u) * uint64(v)
  350. if got != want {
  351. t.Errorf("u=%#08x, v=%#08x: got %#016x, want %#016x", u, v, got, want)
  352. }
  353. }
  354. }
  355. // mul (with a lower case 'm') is an alternative implementation of Int26_6.Mul
  356. // (with an upper case 'M'). It has the same structure as the Int52_12.Mul
  357. // implementation, but Int26_6.mul is easier to test since Go has built-in
  358. // 64-bit integers.
  359. func (x Int26_6) mul(y Int26_6) Int26_6 {
  360. const M, N = 26, 6
  361. lo, hi := muli32(int32(x), int32(y))
  362. ret := Int26_6(hi<<M | lo>>N)
  363. ret += Int26_6((lo >> (N - 1)) & 1) // Round to nearest, instead of rounding down.
  364. return ret
  365. }
  366. // muli32 multiplies two int32 values, returning the 64-bit signed integer
  367. // result as two uint32 values.
  368. //
  369. // muli32 isn't used directly by this package, but it has the same structure as
  370. // muli64, and muli32 is easier to test since Go has built-in 64-bit integers.
  371. func muli32(u, v int32) (lo, hi uint32) {
  372. const (
  373. s = 16
  374. mask = 1<<s - 1
  375. )
  376. u1 := uint32(u >> s)
  377. u0 := uint32(u & mask)
  378. v1 := uint32(v >> s)
  379. v0 := uint32(v & mask)
  380. w0 := u0 * v0
  381. t := u1*v0 + w0>>s
  382. w1 := t & mask
  383. w2 := uint32(int32(t) >> s)
  384. w1 += u0 * v1
  385. return uint32(u) * uint32(v), u1*v1 + w2 + uint32(int32(w1)>>s)
  386. }
  387. // mulu32 is like muli32, except that it multiplies unsigned instead of signed
  388. // values.
  389. //
  390. // This implementation comes from $GOROOT/src/runtime/softfloat64.go's mullu
  391. // function, which is in turn adapted from Hacker's Delight.
  392. //
  393. // mulu32 (and its corresponding test, TestMulu32) isn't used directly by this
  394. // package. It is provided in this test file as a reference point to compare
  395. // the muli32 (and TestMuli32) implementations against.
  396. func mulu32(u, v uint32) (lo, hi uint32) {
  397. const (
  398. s = 16
  399. mask = 1<<s - 1
  400. )
  401. u0 := u & mask
  402. u1 := u >> s
  403. v0 := v & mask
  404. v1 := v >> s
  405. w0 := u0 * v0
  406. t := u1*v0 + w0>>s
  407. w1 := t & mask
  408. w2 := t >> s
  409. w1 += u0 * v1
  410. return u * v, u1*v1 + w2 + w1>>s
  411. }