fixed.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 implements fixed-point integer types.
  5. package fixed // import "golang.org/x/image/math/fixed"
  6. import (
  7. "fmt"
  8. )
  9. // TODO: implement fmt.Formatter for %f and %g.
  10. // I returns the integer value i as an Int26_6.
  11. //
  12. // For example, passing the integer value 2 yields Int26_6(128).
  13. func I(i int) Int26_6 {
  14. return Int26_6(i << 6)
  15. }
  16. // Int26_6 is a signed 26.6 fixed-point number.
  17. //
  18. // The integer part ranges from -33554432 to 33554431, inclusive. The
  19. // fractional part has 6 bits of precision.
  20. //
  21. // For example, the number one-and-a-quarter is Int26_6(1<<6 + 1<<4).
  22. type Int26_6 int32
  23. // String returns a human-readable representation of a 26.6 fixed-point number.
  24. //
  25. // For example, the number one-and-a-quarter becomes "1:16".
  26. func (x Int26_6) String() string {
  27. const shift, mask = 6, 1<<6 - 1
  28. if x >= 0 {
  29. return fmt.Sprintf("%d:%02d", int32(x>>shift), int32(x&mask))
  30. }
  31. x = -x
  32. if x >= 0 {
  33. return fmt.Sprintf("-%d:%02d", int32(x>>shift), int32(x&mask))
  34. }
  35. return "-33554432:00" // The minimum value is -(1<<25).
  36. }
  37. // Floor returns the greatest integer value less than or equal to x.
  38. //
  39. // Its return type is int, not Int26_6.
  40. func (x Int26_6) Floor() int { return int((x + 0x00) >> 6) }
  41. // Round returns the nearest integer value to x. Ties are rounded up.
  42. //
  43. // Its return type is int, not Int26_6.
  44. func (x Int26_6) Round() int { return int((x + 0x20) >> 6) }
  45. // Ceil returns the least integer value greater than or equal to x.
  46. //
  47. // Its return type is int, not Int26_6.
  48. func (x Int26_6) Ceil() int { return int((x + 0x3f) >> 6) }
  49. // Mul returns x*y in 26.6 fixed-point arithmetic.
  50. func (x Int26_6) Mul(y Int26_6) Int26_6 {
  51. return Int26_6((int64(x)*int64(y) + 1<<5) >> 6)
  52. }
  53. // Int52_12 is a signed 52.12 fixed-point number.
  54. //
  55. // The integer part ranges from -2251799813685248 to 2251799813685247,
  56. // inclusive. The fractional part has 12 bits of precision.
  57. //
  58. // For example, the number one-and-a-quarter is Int52_12(1<<12 + 1<<10).
  59. type Int52_12 int64
  60. // String returns a human-readable representation of a 52.12 fixed-point
  61. // number.
  62. //
  63. // For example, the number one-and-a-quarter becomes "1:1024".
  64. func (x Int52_12) String() string {
  65. const shift, mask = 12, 1<<12 - 1
  66. if x >= 0 {
  67. return fmt.Sprintf("%d:%04d", int64(x>>shift), int64(x&mask))
  68. }
  69. x = -x
  70. if x >= 0 {
  71. return fmt.Sprintf("-%d:%04d", int64(x>>shift), int64(x&mask))
  72. }
  73. return "-2251799813685248:0000" // The minimum value is -(1<<51).
  74. }
  75. // Floor returns the greatest integer value less than or equal to x.
  76. //
  77. // Its return type is int, not Int52_12.
  78. func (x Int52_12) Floor() int { return int((x + 0x000) >> 12) }
  79. // Round returns the nearest integer value to x. Ties are rounded up.
  80. //
  81. // Its return type is int, not Int52_12.
  82. func (x Int52_12) Round() int { return int((x + 0x800) >> 12) }
  83. // Ceil returns the least integer value greater than or equal to x.
  84. //
  85. // Its return type is int, not Int52_12.
  86. func (x Int52_12) Ceil() int { return int((x + 0xfff) >> 12) }
  87. // Mul returns x*y in 52.12 fixed-point arithmetic.
  88. func (x Int52_12) Mul(y Int52_12) Int52_12 {
  89. const M, N = 52, 12
  90. lo, hi := muli64(int64(x), int64(y))
  91. ret := Int52_12(hi<<M | lo>>N)
  92. ret += Int52_12((lo >> (N - 1)) & 1) // Round to nearest, instead of rounding down.
  93. return ret
  94. }
  95. // muli64 multiplies two int64 values, returning the 128-bit signed integer
  96. // result as two uint64 values.
  97. //
  98. // This implementation is similar to $GOROOT/src/runtime/softfloat64.go's mullu
  99. // function, which is in turn adapted from Hacker's Delight.
  100. func muli64(u, v int64) (lo, hi uint64) {
  101. const (
  102. s = 32
  103. mask = 1<<s - 1
  104. )
  105. u1 := uint64(u >> s)
  106. u0 := uint64(u & mask)
  107. v1 := uint64(v >> s)
  108. v0 := uint64(v & mask)
  109. w0 := u0 * v0
  110. t := u1*v0 + w0>>s
  111. w1 := t & mask
  112. w2 := uint64(int64(t) >> s)
  113. w1 += u0 * v1
  114. return uint64(u) * uint64(v), u1*v1 + w2 + uint64(int64(w1)>>s)
  115. }
  116. // P returns the integer values x and y as a Point26_6.
  117. //
  118. // For example, passing the integer values (2, -3) yields Point26_6{128, -192}.
  119. func P(x, y int) Point26_6 {
  120. return Point26_6{Int26_6(x << 6), Int26_6(y << 6)}
  121. }
  122. // Point26_6 is a 26.6 fixed-point coordinate pair.
  123. //
  124. // It is analogous to the image.Point type in the standard library.
  125. type Point26_6 struct {
  126. X, Y Int26_6
  127. }
  128. // Add returns the vector p+q.
  129. func (p Point26_6) Add(q Point26_6) Point26_6 {
  130. return Point26_6{p.X + q.X, p.Y + q.Y}
  131. }
  132. // Sub returns the vector p-q.
  133. func (p Point26_6) Sub(q Point26_6) Point26_6 {
  134. return Point26_6{p.X - q.X, p.Y - q.Y}
  135. }
  136. // Mul returns the vector p*k.
  137. func (p Point26_6) Mul(k Int26_6) Point26_6 {
  138. return Point26_6{p.X * k / 64, p.Y * k / 64}
  139. }
  140. // Div returns the vector p/k.
  141. func (p Point26_6) Div(k Int26_6) Point26_6 {
  142. return Point26_6{p.X * 64 / k, p.Y * 64 / k}
  143. }
  144. // In returns whether p is in r.
  145. func (p Point26_6) In(r Rectangle26_6) bool {
  146. return r.Min.X <= p.X && p.X < r.Max.X && r.Min.Y <= p.Y && p.Y < r.Max.Y
  147. }
  148. // Point52_12 is a 52.12 fixed-point coordinate pair.
  149. //
  150. // It is analogous to the image.Point type in the standard library.
  151. type Point52_12 struct {
  152. X, Y Int52_12
  153. }
  154. // Add returns the vector p+q.
  155. func (p Point52_12) Add(q Point52_12) Point52_12 {
  156. return Point52_12{p.X + q.X, p.Y + q.Y}
  157. }
  158. // Sub returns the vector p-q.
  159. func (p Point52_12) Sub(q Point52_12) Point52_12 {
  160. return Point52_12{p.X - q.X, p.Y - q.Y}
  161. }
  162. // Mul returns the vector p*k.
  163. func (p Point52_12) Mul(k Int52_12) Point52_12 {
  164. return Point52_12{p.X * k / 4096, p.Y * k / 4096}
  165. }
  166. // Div returns the vector p/k.
  167. func (p Point52_12) Div(k Int52_12) Point52_12 {
  168. return Point52_12{p.X * 4096 / k, p.Y * 4096 / k}
  169. }
  170. // In returns whether p is in r.
  171. func (p Point52_12) In(r Rectangle52_12) bool {
  172. return r.Min.X <= p.X && p.X < r.Max.X && r.Min.Y <= p.Y && p.Y < r.Max.Y
  173. }
  174. // R returns the integer values minX, minY, maxX, maxY as a Rectangle26_6.
  175. //
  176. // For example, passing the integer values (0, 1, 2, 3) yields
  177. // Rectangle26_6{Point26_6{0, 64}, Point26_6{128, 192}}.
  178. //
  179. // Like the image.Rect function in the standard library, the returned rectangle
  180. // has minimum and maximum coordinates swapped if necessary so that it is
  181. // well-formed.
  182. func R(minX, minY, maxX, maxY int) Rectangle26_6 {
  183. if minX > maxX {
  184. minX, maxX = maxX, minX
  185. }
  186. if minY > maxY {
  187. minY, maxY = maxY, minY
  188. }
  189. return Rectangle26_6{
  190. Point26_6{
  191. Int26_6(minX << 6),
  192. Int26_6(minY << 6),
  193. },
  194. Point26_6{
  195. Int26_6(maxX << 6),
  196. Int26_6(maxY << 6),
  197. },
  198. }
  199. }
  200. // Rectangle26_6 is a 26.6 fixed-point coordinate rectangle. The Min bound is
  201. // inclusive and the Max bound is exclusive. It is well-formed if Min.X <=
  202. // Max.X and likewise for Y.
  203. //
  204. // It is analogous to the image.Rectangle type in the standard library.
  205. type Rectangle26_6 struct {
  206. Min, Max Point26_6
  207. }
  208. // Add returns the rectangle r translated by p.
  209. func (r Rectangle26_6) Add(p Point26_6) Rectangle26_6 {
  210. return Rectangle26_6{
  211. Point26_6{r.Min.X + p.X, r.Min.Y + p.Y},
  212. Point26_6{r.Max.X + p.X, r.Max.Y + p.Y},
  213. }
  214. }
  215. // Sub returns the rectangle r translated by -p.
  216. func (r Rectangle26_6) Sub(p Point26_6) Rectangle26_6 {
  217. return Rectangle26_6{
  218. Point26_6{r.Min.X - p.X, r.Min.Y - p.Y},
  219. Point26_6{r.Max.X - p.X, r.Max.Y - p.Y},
  220. }
  221. }
  222. // Intersect returns the largest rectangle contained by both r and s. If the
  223. // two rectangles do not overlap then the zero rectangle will be returned.
  224. func (r Rectangle26_6) Intersect(s Rectangle26_6) Rectangle26_6 {
  225. if r.Min.X < s.Min.X {
  226. r.Min.X = s.Min.X
  227. }
  228. if r.Min.Y < s.Min.Y {
  229. r.Min.Y = s.Min.Y
  230. }
  231. if r.Max.X > s.Max.X {
  232. r.Max.X = s.Max.X
  233. }
  234. if r.Max.Y > s.Max.Y {
  235. r.Max.Y = s.Max.Y
  236. }
  237. // Letting r0 and s0 be the values of r and s at the time that the method
  238. // is called, this next line is equivalent to:
  239. //
  240. // if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
  241. if r.Empty() {
  242. return Rectangle26_6{}
  243. }
  244. return r
  245. }
  246. // Union returns the smallest rectangle that contains both r and s.
  247. func (r Rectangle26_6) Union(s Rectangle26_6) Rectangle26_6 {
  248. if r.Empty() {
  249. return s
  250. }
  251. if s.Empty() {
  252. return r
  253. }
  254. if r.Min.X > s.Min.X {
  255. r.Min.X = s.Min.X
  256. }
  257. if r.Min.Y > s.Min.Y {
  258. r.Min.Y = s.Min.Y
  259. }
  260. if r.Max.X < s.Max.X {
  261. r.Max.X = s.Max.X
  262. }
  263. if r.Max.Y < s.Max.Y {
  264. r.Max.Y = s.Max.Y
  265. }
  266. return r
  267. }
  268. // Empty returns whether the rectangle contains no points.
  269. func (r Rectangle26_6) Empty() bool {
  270. return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
  271. }
  272. // In returns whether every point in r is in s.
  273. func (r Rectangle26_6) In(s Rectangle26_6) bool {
  274. if r.Empty() {
  275. return true
  276. }
  277. // Note that r.Max is an exclusive bound for r, so that r.In(s)
  278. // does not require that r.Max.In(s).
  279. return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
  280. s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
  281. }
  282. // Rectangle52_12 is a 52.12 fixed-point coordinate rectangle. The Min bound is
  283. // inclusive and the Max bound is exclusive. It is well-formed if Min.X <=
  284. // Max.X and likewise for Y.
  285. //
  286. // It is analogous to the image.Rectangle type in the standard library.
  287. type Rectangle52_12 struct {
  288. Min, Max Point52_12
  289. }
  290. // Add returns the rectangle r translated by p.
  291. func (r Rectangle52_12) Add(p Point52_12) Rectangle52_12 {
  292. return Rectangle52_12{
  293. Point52_12{r.Min.X + p.X, r.Min.Y + p.Y},
  294. Point52_12{r.Max.X + p.X, r.Max.Y + p.Y},
  295. }
  296. }
  297. // Sub returns the rectangle r translated by -p.
  298. func (r Rectangle52_12) Sub(p Point52_12) Rectangle52_12 {
  299. return Rectangle52_12{
  300. Point52_12{r.Min.X - p.X, r.Min.Y - p.Y},
  301. Point52_12{r.Max.X - p.X, r.Max.Y - p.Y},
  302. }
  303. }
  304. // Intersect returns the largest rectangle contained by both r and s. If the
  305. // two rectangles do not overlap then the zero rectangle will be returned.
  306. func (r Rectangle52_12) Intersect(s Rectangle52_12) Rectangle52_12 {
  307. if r.Min.X < s.Min.X {
  308. r.Min.X = s.Min.X
  309. }
  310. if r.Min.Y < s.Min.Y {
  311. r.Min.Y = s.Min.Y
  312. }
  313. if r.Max.X > s.Max.X {
  314. r.Max.X = s.Max.X
  315. }
  316. if r.Max.Y > s.Max.Y {
  317. r.Max.Y = s.Max.Y
  318. }
  319. // Letting r0 and s0 be the values of r and s at the time that the method
  320. // is called, this next line is equivalent to:
  321. //
  322. // if max(r0.Min.X, s0.Min.X) >= min(r0.Max.X, s0.Max.X) || likewiseForY { etc }
  323. if r.Empty() {
  324. return Rectangle52_12{}
  325. }
  326. return r
  327. }
  328. // Union returns the smallest rectangle that contains both r and s.
  329. func (r Rectangle52_12) Union(s Rectangle52_12) Rectangle52_12 {
  330. if r.Empty() {
  331. return s
  332. }
  333. if s.Empty() {
  334. return r
  335. }
  336. if r.Min.X > s.Min.X {
  337. r.Min.X = s.Min.X
  338. }
  339. if r.Min.Y > s.Min.Y {
  340. r.Min.Y = s.Min.Y
  341. }
  342. if r.Max.X < s.Max.X {
  343. r.Max.X = s.Max.X
  344. }
  345. if r.Max.Y < s.Max.Y {
  346. r.Max.Y = s.Max.Y
  347. }
  348. return r
  349. }
  350. // Empty returns whether the rectangle contains no points.
  351. func (r Rectangle52_12) Empty() bool {
  352. return r.Min.X >= r.Max.X || r.Min.Y >= r.Max.Y
  353. }
  354. // In returns whether every point in r is in s.
  355. func (r Rectangle52_12) In(s Rectangle52_12) bool {
  356. if r.Empty() {
  357. return true
  358. }
  359. // Note that r.Max is an exclusive bound for r, so that r.In(s)
  360. // does not require that r.Max.In(s).
  361. return s.Min.X <= r.Min.X && r.Max.X <= s.Max.X &&
  362. s.Min.Y <= r.Min.Y && r.Max.Y <= s.Max.Y
  363. }