bigint_bitwise.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. use num_bigint::{BigInt, Sign, ToBigInt};
  2. use num_traits::ToPrimitive;
  3. use std::{i32, i64, u32};
  4. enum ValueVec {
  5. N,
  6. P(&'static [u32]),
  7. M(&'static [u32]),
  8. }
  9. use crate::ValueVec::*;
  10. impl ToBigInt for ValueVec {
  11. fn to_bigint(&self) -> Option<BigInt> {
  12. match self {
  13. &N => Some(BigInt::from_slice(Sign::NoSign, &[])),
  14. &P(s) => Some(BigInt::from_slice(Sign::Plus, s)),
  15. &M(s) => Some(BigInt::from_slice(Sign::Minus, s)),
  16. }
  17. }
  18. }
  19. // a, !a
  20. const NOT_VALUES: &[(ValueVec, ValueVec)] = &[
  21. (N, M(&[1])),
  22. (P(&[1]), M(&[2])),
  23. (P(&[2]), M(&[3])),
  24. (P(&[!0 - 2]), M(&[!0 - 1])),
  25. (P(&[!0 - 1]), M(&[!0])),
  26. (P(&[!0]), M(&[0, 1])),
  27. (P(&[0, 1]), M(&[1, 1])),
  28. (P(&[1, 1]), M(&[2, 1])),
  29. ];
  30. // a, b, a & b, a | b, a ^ b
  31. const BITWISE_VALUES: &[(ValueVec, ValueVec, ValueVec, ValueVec, ValueVec)] = &[
  32. (N, N, N, N, N),
  33. (N, P(&[1]), N, P(&[1]), P(&[1])),
  34. (N, P(&[!0]), N, P(&[!0]), P(&[!0])),
  35. (N, P(&[0, 1]), N, P(&[0, 1]), P(&[0, 1])),
  36. (N, M(&[1]), N, M(&[1]), M(&[1])),
  37. (N, M(&[!0]), N, M(&[!0]), M(&[!0])),
  38. (N, M(&[0, 1]), N, M(&[0, 1]), M(&[0, 1])),
  39. (P(&[1]), P(&[!0]), P(&[1]), P(&[!0]), P(&[!0 - 1])),
  40. (P(&[!0]), P(&[!0]), P(&[!0]), P(&[!0]), N),
  41. (P(&[!0]), P(&[1, 1]), P(&[1]), P(&[!0, 1]), P(&[!0 - 1, 1])),
  42. (P(&[1]), M(&[!0]), P(&[1]), M(&[!0]), M(&[0, 1])),
  43. (P(&[!0]), M(&[1]), P(&[!0]), M(&[1]), M(&[0, 1])),
  44. (P(&[!0]), M(&[!0]), P(&[1]), M(&[1]), M(&[2])),
  45. (P(&[!0]), M(&[1, 1]), P(&[!0]), M(&[1, 1]), M(&[0, 2])),
  46. (P(&[1, 1]), M(&[!0]), P(&[1, 1]), M(&[!0]), M(&[0, 2])),
  47. (M(&[1]), M(&[!0]), M(&[!0]), M(&[1]), P(&[!0 - 1])),
  48. (M(&[!0]), M(&[!0]), M(&[!0]), M(&[!0]), N),
  49. (M(&[!0]), M(&[1, 1]), M(&[!0, 1]), M(&[1]), P(&[!0 - 1, 1])),
  50. ];
  51. const I32_MIN: i64 = i32::MIN as i64;
  52. const I32_MAX: i64 = i32::MAX as i64;
  53. const U32_MAX: i64 = u32::MAX as i64;
  54. // some corner cases
  55. const I64_VALUES: &[i64] = &[
  56. i64::MIN,
  57. i64::MIN + 1,
  58. i64::MIN + 2,
  59. i64::MIN + 3,
  60. -U32_MAX - 3,
  61. -U32_MAX - 2,
  62. -U32_MAX - 1,
  63. -U32_MAX,
  64. -U32_MAX + 1,
  65. -U32_MAX + 2,
  66. -U32_MAX + 3,
  67. I32_MIN - 3,
  68. I32_MIN - 2,
  69. I32_MIN - 1,
  70. I32_MIN,
  71. I32_MIN + 1,
  72. I32_MIN + 2,
  73. I32_MIN + 3,
  74. -3,
  75. -2,
  76. -1,
  77. 0,
  78. 1,
  79. 2,
  80. 3,
  81. I32_MAX - 3,
  82. I32_MAX - 2,
  83. I32_MAX - 1,
  84. I32_MAX,
  85. I32_MAX + 1,
  86. I32_MAX + 2,
  87. I32_MAX + 3,
  88. U32_MAX - 3,
  89. U32_MAX - 2,
  90. U32_MAX - 1,
  91. U32_MAX,
  92. U32_MAX + 1,
  93. U32_MAX + 2,
  94. U32_MAX + 3,
  95. i64::MAX - 3,
  96. i64::MAX - 2,
  97. i64::MAX - 1,
  98. i64::MAX,
  99. ];
  100. #[test]
  101. fn test_not() {
  102. for &(ref a, ref not) in NOT_VALUES.iter() {
  103. let a = a.to_bigint().unwrap();
  104. let not = not.to_bigint().unwrap();
  105. // sanity check for tests that fit in i64
  106. if let (Some(prim_a), Some(prim_not)) = (a.to_i64(), not.to_i64()) {
  107. assert_eq!(!prim_a, prim_not);
  108. }
  109. assert_eq!(!a.clone(), not, "!{:x}", a);
  110. assert_eq!(!not.clone(), a, "!{:x}", not);
  111. }
  112. }
  113. #[test]
  114. fn test_not_i64() {
  115. for &prim_a in I64_VALUES.iter() {
  116. let a = prim_a.to_bigint().unwrap();
  117. let not = (!prim_a).to_bigint().unwrap();
  118. assert_eq!(!a.clone(), not, "!{:x}", a);
  119. }
  120. }
  121. #[test]
  122. fn test_bitwise() {
  123. for &(ref a, ref b, ref and, ref or, ref xor) in BITWISE_VALUES.iter() {
  124. let a = a.to_bigint().unwrap();
  125. let b = b.to_bigint().unwrap();
  126. let and = and.to_bigint().unwrap();
  127. let or = or.to_bigint().unwrap();
  128. let xor = xor.to_bigint().unwrap();
  129. // sanity check for tests that fit in i64
  130. if let (Some(prim_a), Some(prim_b)) = (a.to_i64(), b.to_i64()) {
  131. if let Some(prim_and) = and.to_i64() {
  132. assert_eq!(prim_a & prim_b, prim_and);
  133. }
  134. if let Some(prim_or) = or.to_i64() {
  135. assert_eq!(prim_a | prim_b, prim_or);
  136. }
  137. if let Some(prim_xor) = xor.to_i64() {
  138. assert_eq!(prim_a ^ prim_b, prim_xor);
  139. }
  140. }
  141. assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
  142. assert_eq!(b.clone() & &a, and, "{:x} & {:x}", b, a);
  143. assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
  144. assert_eq!(b.clone() | &a, or, "{:x} | {:x}", b, a);
  145. assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
  146. assert_eq!(b.clone() ^ &a, xor, "{:x} ^ {:x}", b, a);
  147. }
  148. }
  149. #[test]
  150. fn test_bitwise_i64() {
  151. for &prim_a in I64_VALUES.iter() {
  152. let a = prim_a.to_bigint().unwrap();
  153. for &prim_b in I64_VALUES.iter() {
  154. let b = prim_b.to_bigint().unwrap();
  155. let and = (prim_a & prim_b).to_bigint().unwrap();
  156. let or = (prim_a | prim_b).to_bigint().unwrap();
  157. let xor = (prim_a ^ prim_b).to_bigint().unwrap();
  158. assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
  159. assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
  160. assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
  161. }
  162. }
  163. }