int12_20.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright ©2021 The star-tex 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-STAR-TEX file.
  4. package fixed
  5. import (
  6. "fmt"
  7. "strconv"
  8. "golang.org/x/image/math/fixed"
  9. )
  10. // Int12_20 is a signed 12.20 fixed-point number.
  11. //
  12. // The integer part ranges from -2048 to 2047, inclusive. The
  13. // fractional part has 20 bits of precision.
  14. type Int12_20 uint32
  15. // I12_20 returns the integer value i as an Int12_20.
  16. //
  17. // For example, passing the integer value 2 yields Int12_20(2097152).
  18. func I12_20(v int) Int12_20 {
  19. return Int12_20(v << 20)
  20. }
  21. // ParseInt12_20 converts the string s to a signed 12.20 fixed-point number.
  22. func ParseInt12_20(s string) (Int12_20, error) {
  23. f, err := strconv.ParseFloat(s, 32)
  24. if err != nil {
  25. return 0, err
  26. }
  27. return Int12_20(int32(f * (1 << 20))), nil
  28. }
  29. // Float64 converts the 12.20 fixed-point number to a floating point one.
  30. func (x Int12_20) Float64() float64 {
  31. v := int32(x)
  32. return float64(v) / (1 << 20)
  33. }
  34. // String returns a human-readable representation of a 12.20 fixed-point number.
  35. func (x Int12_20) String() string {
  36. const (
  37. shift = 12
  38. mask = 1<<shift - 1
  39. )
  40. if x >= 0 {
  41. return fmt.Sprintf("%d:%02d", int32(x>>shift), int32(x&mask))
  42. }
  43. x = -x
  44. if x >= 0 {
  45. return fmt.Sprintf("-%d:%02d", int32(x>>shift), int32(x&mask))
  46. }
  47. return "-2048:00" // The minimum value is -(1<<(12-1)).
  48. }
  49. // ToInt26_6 converts the 12.20 fixed-point number to a 26.6 one.
  50. func (x Int12_20) ToInt26_6() fixed.Int26_6 {
  51. f := x.Float64()
  52. return fixed.Int26_6(f * (1 << 6))
  53. }