condition_code.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 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 s390x
  5. import (
  6. "fmt"
  7. )
  8. // CCMask represents a 4-bit condition code mask. Bits that
  9. // are not part of the mask should be 0.
  10. //
  11. // Condition code masks represent the 4 possible values of
  12. // the 2-bit condition code as individual bits. Since IBM Z
  13. // is a big-endian platform bits are numbered from left to
  14. // right. The lowest value, 0, is represented by 8 (0b1000)
  15. // and the highest value, 3, is represented by 1 (0b0001).
  16. //
  17. // Note that condition code values have different semantics
  18. // depending on the instruction that set the condition code.
  19. // The names given here assume that the condition code was
  20. // set by an integer or floating point comparison. Other
  21. // instructions may use these same codes to indicate
  22. // different results such as a carry or overflow.
  23. type CCMask uint8
  24. const (
  25. Never CCMask = 0 // no-op
  26. // 1-bit masks
  27. Equal CCMask = 1 << 3
  28. Less CCMask = 1 << 2
  29. Greater CCMask = 1 << 1
  30. Unordered CCMask = 1 << 0
  31. // 2-bit masks
  32. EqualOrUnordered CCMask = Equal | Unordered // not less and not greater
  33. LessOrEqual CCMask = Less | Equal // ordered and not greater
  34. LessOrGreater CCMask = Less | Greater // ordered and not equal
  35. LessOrUnordered CCMask = Less | Unordered // not greater and not equal
  36. GreaterOrEqual CCMask = Greater | Equal // ordered and not less
  37. GreaterOrUnordered CCMask = Greater | Unordered // not less and not equal
  38. // 3-bit masks
  39. NotEqual CCMask = Always ^ Equal
  40. NotLess CCMask = Always ^ Less
  41. NotGreater CCMask = Always ^ Greater
  42. NotUnordered CCMask = Always ^ Unordered
  43. // 4-bit mask
  44. Always CCMask = Equal | Less | Greater | Unordered
  45. // useful aliases
  46. Carry CCMask = GreaterOrUnordered
  47. NoCarry CCMask = LessOrEqual
  48. Borrow CCMask = NoCarry
  49. NoBorrow CCMask = Carry
  50. )
  51. // Inverse returns the complement of the condition code mask.
  52. func (c CCMask) Inverse() CCMask {
  53. return c ^ Always
  54. }
  55. // ReverseComparison swaps the bits at 0b0100 and 0b0010 in the mask,
  56. // reversing the behavior of greater than and less than conditions.
  57. func (c CCMask) ReverseComparison() CCMask {
  58. r := c & EqualOrUnordered
  59. if c&Less != 0 {
  60. r |= Greater
  61. }
  62. if c&Greater != 0 {
  63. r |= Less
  64. }
  65. return r
  66. }
  67. func (c CCMask) String() string {
  68. switch c {
  69. // 0-bit mask
  70. case Never:
  71. return "Never"
  72. // 1-bit masks
  73. case Equal:
  74. return "Equal"
  75. case Less:
  76. return "Less"
  77. case Greater:
  78. return "Greater"
  79. case Unordered:
  80. return "Unordered"
  81. // 2-bit masks
  82. case EqualOrUnordered:
  83. return "EqualOrUnordered"
  84. case LessOrEqual:
  85. return "LessOrEqual"
  86. case LessOrGreater:
  87. return "LessOrGreater"
  88. case LessOrUnordered:
  89. return "LessOrUnordered"
  90. case GreaterOrEqual:
  91. return "GreaterOrEqual"
  92. case GreaterOrUnordered:
  93. return "GreaterOrUnordered"
  94. // 3-bit masks
  95. case NotEqual:
  96. return "NotEqual"
  97. case NotLess:
  98. return "NotLess"
  99. case NotGreater:
  100. return "NotGreater"
  101. case NotUnordered:
  102. return "NotUnordered"
  103. // 4-bit mask
  104. case Always:
  105. return "Always"
  106. }
  107. // invalid
  108. return fmt.Sprintf("Invalid (%#x)", c)
  109. }