1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //! Bit mask. 15 16 /// Mask, representing a segment of consecutive binary bits, e.g. 17 /// 18 /// When using the mask with a binary number for **and** operations, the value 19 /// at the mask position of the binary number can be obtained. 20 /// 21 /// For example: 0000_1111(mask) & 1010_1010(target number) = 0000_1010 22 /// 23 /// # Example 24 /// ```no run 25 /// use ylong_runtime::util::bit::Mask; 26 /// 27 /// // On a 64-bit machine, you can get the mask 0xf 28 /// let mask = Mask::new(4, 0); 29 /// ``` 30 #[derive(Debug, Copy, Clone, PartialEq, Eq)] 31 pub struct Mask { 32 mask: usize, 33 shift: u32, 34 } 35 36 impl Mask { 37 /// Creates a mask. Generates a mask based on the length of consecutive 38 /// binary bits + an offset. 39 /// 40 /// # Parameter 41 /// width: Length of consecutive binary bits 42 /// 43 /// shift: Offset of consecutive binary bits to the **left** 44 /// 45 /// # Example 46 /// When width + shift < Machine word length time 47 /// ```no run 48 /// use ylong_runtime::util::bit::Mask; 49 /// 50 /// let width = 4; 51 /// let shift = 0; 52 /// let mask = Mask::new(width, shift); 53 /// // Get mask as 0xf 54 /// 55 /// let width = 4; 56 /// let shift = 4; 57 /// let mask = Mask::new(width, shift); 58 /// // Get mask as 0xf0 59 /// ``` 60 /// When width >= machine word length, a mask of all 1's is returned 61 /// regardless of the shift. 62 /// ```no run 63 /// use ylong_runtime::util::bit::Mask; 64 /// 65 /// let width = 128; 66 /// let shift = 0; 67 /// let mask = Mask::new(width, shift); 68 /// // On a 64-bit machine, the mask is 0xffff_ffff_ffff_ffff 69 /// ``` 70 /// When width == 0, an all-0 mask is returned, regardless of the shift. 71 /// ```no run 72 /// use ylong_runtime::util::bit::Mask; 73 /// 74 /// let width = 0; 75 /// let shift = 0; 76 /// let mask = Mask::new(width, shift); 77 /// // On a 64-bit machine, the mask is 0x0 78 /// ``` 79 /// When width < machine word length and width + shift > machine word 80 /// length, it will ensure that width remains unchanged and shift becomes 81 /// machine word length - width. 82 /// ```no run 83 /// use ylong_runtime::util::bit::Mask; 84 /// 85 /// let width = 32; 86 /// let shift = 64; 87 /// let mask = Mask::new(width, shift); 88 /// // On a 64-bit machine, the mask is 0xffff_ffff_0000_0000, the offset 89 /// // becomes 32. 90 /// ``` new(width: u32, shift: u32) -> Self91 pub const fn new(width: u32, shift: u32) -> Self { 92 const USIZE_LEN: u32 = 0usize.wrapping_sub(1).count_ones(); 93 if width >= USIZE_LEN { 94 return Mask { 95 mask: 0usize.wrapping_sub(1), 96 shift: 0, 97 }; 98 } 99 if width == 0 { 100 return Mask { mask: 0, shift: 0 }; 101 } 102 let wb = 1usize.wrapping_shl(width) - 1; 103 let left = USIZE_LEN - width; 104 let sh = if left > shift { shift } else { left }; 105 Mask { 106 mask: wb.wrapping_shl(sh), 107 shift: sh, 108 } 109 } 110 } 111 112 /// Bit is used for some binary processing. A usize type can be converted to a 113 /// Bit type to get a part of it by concatenating it with a Mask type. 114 /// 115 /// Uses the get_by_mask method to get the value of the specified bit from the 116 /// Bit. Uses set_by_mask to add the value of the specified bit to the bit. 117 /// 118 /// For example, divides a usize type into different meanings according to each 119 /// bit, uses Mask to get the mask in the corresponding position, and then uses 120 /// Bit set_by_mask\get_by_mask to modify the specified bit. 121 /// 122 /// # Example 123 /// ```not run 124 /// use ylong_runtime::util::bit::{Mask, Bit}; 125 /// 126 /// // Assign the following meaning to a portion of a usize bit, using a 64-bit machine word length as an example 127 /// // |---- A ---|---- B ---|---- C ---|---- D ---| 128 /// // |- 16bits -|- 16bits -|- 16bits -|- 16bits -| 129 /// 130 /// const A: Mask = Mask::new(16, 48); 131 /// const B: Mask = Mask::new(16, 32); 132 /// const C: Mask = Mask::new(16, 16); 133 /// const D: Mask = Mask::new(16, 0); 134 /// 135 /// // Create a Bit type instance 136 /// let base = 0usize; 137 /// let mut bits = Bit::from_usize(base); 138 /// 139 /// // Get the value of A 140 /// let a = bits.get_by_mask(A); 141 /// assert_eq!(a, 0x0); 142 /// 143 /// // Modify the value of A 144 /// let new_a = 0x1234usize; 145 /// bits.set_by_mask(A, new_a); 146 /// assert_eq!(bits.as_usize(), 0x1234_0000_0000_0000); 147 /// ``` 148 #[derive(Debug, Copy, Clone, PartialEq, Eq)] 149 pub struct Bit(usize); 150 151 impl Bit { 152 /// Converts a usize type to a Bit type 153 /// 154 /// # Example 155 /// ```no run 156 /// use ylong_runtime::util::bit::Bit; 157 /// 158 /// let base = 0usize; 159 /// let bits = Bit::from_usize(base); 160 /// ``` from_usize(val: usize) -> Self161 pub fn from_usize(val: usize) -> Self { 162 Bit(val) 163 } 164 165 /// Converts a Bit type to a usize type 166 /// 167 /// # Example 168 /// ```no run 169 /// use ylong_runtime::util::bit::Bit; 170 /// 171 /// let base = 0usize; 172 /// let bits = Bit::from_usize(base); 173 /// let val = bits.as_usize(); 174 /// assert_eq!(val, 0); 175 /// ``` as_usize(&self) -> usize176 pub fn as_usize(&self) -> usize { 177 self.0 178 } 179 180 /// Overwrites a usize data to the specified mask position of the bit. 181 /// 182 /// If the binary length of the given usize data is greater than the binary 183 /// bit length of the mask, it will be truncated. 184 /// 185 /// # Example 186 /// When Mask binary length >= val binary length (excluding leading zeros) 187 /// ```no run 188 /// use ylong_runtime::util::bit::{Bit, Mask}; 189 /// 190 /// // mask's length 16 191 /// const MASK: Mask = Mask::new(16, 0); 192 /// let mut bits = Bit::from_usize(0); 193 /// 194 /// // val's length 16(dividing the leading 0) 195 /// let val = 0x1234usize; 196 /// bits.set_by_mask(MASK, val); 197 /// assert_eq!(bits, Bit::from_usize(0x1234)); 198 /// ``` 199 /// When Mask binary length < val binary length (excluding leading zeros), 200 /// val is truncated and only the low bit is retained. 201 /// ```no run 202 /// use ylong_runtime::util::bit::{Bit, Mask}; 203 /// 204 /// // mask's length 16 205 /// const MASK: Mask = Mask::new(16, 0); 206 /// let mut bits = Bit::from_usize(0); 207 /// 208 /// // val's length 32(dividing the leading 0) 209 /// let val = 0x1234_5678usize; 210 /// bits.set_by_mask(MASK, val); 211 /// // val is truncated, leaving only the lower 16 bits. 212 /// assert_eq!(bits, Bit::from_usize(0x5678)); 213 /// ``` set_by_mask(&mut self, mask: Mask, val: usize)214 pub fn set_by_mask(&mut self, mask: Mask, val: usize) { 215 self.0 = (self.0 & !mask.mask) | ((val << mask.shift) & mask.mask); 216 } 217 218 /// Gets the binary value at the specified mask position from the bit. 219 /// 220 /// # Example 221 /// ```not run 222 /// use ylong_runtime::util::bit::{Mask, Bit}; 223 /// 224 /// const MASK: Mask = Mask::new(16, 0); 225 /// 226 /// let base = 0x0123_4567_89ab_cdefusize; 227 /// let bits = Bit::from_usize(base); 228 /// 229 /// let val = bits.get_by_mask(MASK); 230 /// assert_eq!(val, 0xcdef); 231 /// ``` get_by_mask(&self, mask: Mask) -> usize232 pub fn get_by_mask(&self, mask: Mask) -> usize { 233 (self.0 & mask.mask) >> mask.shift 234 } 235 } 236 237 #[cfg(test)] 238 mod test { 239 use super::*; 240 impl Bit { set(&mut self, val: usize)241 fn set(&mut self, val: usize) { 242 self.0 = val; 243 } 244 clear(&mut self)245 fn clear(&mut self) { 246 self.0 = 0; 247 } 248 } 249 /// UT test cases for mask new function 250 /// 251 /// # Brief 252 /// 1. pass in the parameters, and create the Mask. 253 /// 2. Check return value. 254 #[cfg(target_pointer_width = "32")] 255 #[test] ut_mask_new_bit32()256 fn ut_mask_new_bit32() { 257 assert_eq!( 258 Mask::new(0, 0), 259 Mask { 260 mask: 0x0, 261 shift: 0 262 } 263 ); 264 assert_eq!( 265 Mask::new(0, 16), 266 Mask { 267 mask: 0x0, 268 shift: 0 269 } 270 ); 271 assert_eq!( 272 Mask::new(0, 32), 273 Mask { 274 mask: 0x0, 275 shift: 0 276 } 277 ); 278 assert_eq!( 279 Mask::new(0, 64), 280 Mask { 281 mask: 0x0, 282 shift: 0 283 } 284 ); 285 assert_eq!( 286 Mask::new(1, 0), 287 Mask { 288 mask: 0x1, 289 shift: 0 290 } 291 ); 292 assert_eq!( 293 Mask::new(1, 16), 294 Mask { 295 mask: 0x1_0000, 296 shift: 16 297 } 298 ); 299 assert_eq!( 300 Mask::new(1, 31), 301 Mask { 302 mask: 0x8000_0000, 303 shift: 31 304 } 305 ); 306 assert_eq!( 307 Mask::new(1, 32), 308 Mask { 309 mask: 0x8000_0000, 310 shift: 31 311 } 312 ); 313 assert_eq!( 314 Mask::new(1, 128), 315 Mask { 316 mask: 0x8000_0000, 317 shift: 31 318 } 319 ); 320 assert_eq!( 321 Mask::new(4, 0), 322 Mask { 323 mask: 0xf, 324 shift: 0 325 } 326 ); 327 assert_eq!( 328 Mask::new(4, 16), 329 Mask { 330 mask: 0xf_0000, 331 shift: 16 332 } 333 ); 334 assert_eq!( 335 Mask::new(4, 28), 336 Mask { 337 mask: 0xf000_0000, 338 shift: 28 339 } 340 ); 341 assert_eq!( 342 Mask::new(4, 32), 343 Mask { 344 mask: 0xf000_0000, 345 shift: 28 346 } 347 ); 348 assert_eq!( 349 Mask::new(4, 64), 350 Mask { 351 mask: 0xf000_0000, 352 shift: 28 353 } 354 ); 355 assert_eq!( 356 Mask::new(16, 0), 357 Mask { 358 mask: 0xffff, 359 shift: 0 360 } 361 ); 362 assert_eq!( 363 Mask::new(16, 16), 364 Mask { 365 mask: 0xffff_0000, 366 shift: 16 367 } 368 ); 369 assert_eq!( 370 Mask::new(16, 32), 371 Mask { 372 mask: 0xffff_0000, 373 shift: 16 374 } 375 ); 376 assert_eq!( 377 Mask::new(16, 64), 378 Mask { 379 mask: 0xffff_0000, 380 shift: 16 381 } 382 ); 383 assert_eq!( 384 Mask::new(32, 0), 385 Mask { 386 mask: 0xffff_ffff, 387 shift: 0 388 } 389 ); 390 assert_eq!( 391 Mask::new(32, 16), 392 Mask { 393 mask: 0xffff_ffff, 394 shift: 0 395 } 396 ); 397 assert_eq!( 398 Mask::new(32, 32), 399 Mask { 400 mask: 0xffff_ffff, 401 shift: 0 402 } 403 ); 404 assert_eq!( 405 Mask::new(32, 64), 406 Mask { 407 mask: 0xffff_ffff, 408 shift: 0 409 } 410 ); 411 assert_eq!( 412 Mask::new(64, 0), 413 Mask { 414 mask: 0xffff_ffff, 415 shift: 0 416 } 417 ); 418 assert_eq!( 419 Mask::new(64, 16), 420 Mask { 421 mask: 0xffff_ffff, 422 shift: 0 423 } 424 ); 425 assert_eq!( 426 Mask::new(64, 32), 427 Mask { 428 mask: 0xffff_ffff, 429 shift: 0 430 } 431 ); 432 assert_eq!( 433 Mask::new(64, 64), 434 Mask { 435 mask: 0xffff_ffff, 436 shift: 0 437 } 438 ); 439 } 440 441 /// UT test cases for mask new function 442 /// 443 /// # Brief 444 /// 1. pass in the parameters, and create the Mask. 445 /// 2. Check return value. 446 #[cfg(target_pointer_width = "64")] 447 #[test] ut_mask_new_bit64()448 fn ut_mask_new_bit64() { 449 assert_eq!( 450 Mask::new(0, 0), 451 Mask { 452 mask: 0x0, 453 shift: 0 454 } 455 ); 456 assert_eq!( 457 Mask::new(0, 32), 458 Mask { 459 mask: 0x0, 460 shift: 0 461 } 462 ); 463 assert_eq!( 464 Mask::new(0, 64), 465 Mask { 466 mask: 0x0, 467 shift: 0 468 } 469 ); 470 assert_eq!( 471 Mask::new(0, 128), 472 Mask { 473 mask: 0x0, 474 shift: 0 475 } 476 ); 477 478 assert_eq!( 479 Mask::new(1, 0), 480 Mask { 481 mask: 0x1, 482 shift: 0 483 } 484 ); 485 assert_eq!( 486 Mask::new(1, 32), 487 Mask { 488 mask: 0x1_0000_0000, 489 shift: 32 490 } 491 ); 492 assert_eq!( 493 Mask::new(1, 63), 494 Mask { 495 mask: 0x8000_0000_0000_0000, 496 shift: 63 497 } 498 ); 499 assert_eq!( 500 Mask::new(1, 64), 501 Mask { 502 mask: 0x8000_0000_0000_0000, 503 shift: 63 504 } 505 ); 506 assert_eq!( 507 Mask::new(1, 128), 508 Mask { 509 mask: 0x8000_0000_0000_0000, 510 shift: 63 511 } 512 ); 513 514 assert_eq!( 515 Mask::new(4, 0), 516 Mask { 517 mask: 0xf, 518 shift: 0 519 } 520 ); 521 assert_eq!( 522 Mask::new(4, 32), 523 Mask { 524 mask: 0xf_0000_0000, 525 shift: 32 526 } 527 ); 528 assert_eq!( 529 Mask::new(4, 60), 530 Mask { 531 mask: 0xf000_0000_0000_0000, 532 shift: 60 533 } 534 ); 535 assert_eq!( 536 Mask::new(4, 64), 537 Mask { 538 mask: 0xf000_0000_0000_0000, 539 shift: 60 540 } 541 ); 542 assert_eq!( 543 Mask::new(4, 128), 544 Mask { 545 mask: 0xf000_0000_0000_0000, 546 shift: 60 547 } 548 ); 549 550 assert_eq!( 551 Mask::new(32, 0), 552 Mask { 553 mask: 0xffff_ffff, 554 shift: 0 555 } 556 ); 557 assert_eq!( 558 Mask::new(32, 32), 559 Mask { 560 mask: 0xffff_ffff_0000_0000, 561 shift: 32 562 } 563 ); 564 assert_eq!( 565 Mask::new(32, 64), 566 Mask { 567 mask: 0xffff_ffff_0000_0000, 568 shift: 32 569 } 570 ); 571 assert_eq!( 572 Mask::new(32, 128), 573 Mask { 574 mask: 0xffff_ffff_0000_0000, 575 shift: 32 576 } 577 ); 578 579 assert_eq!( 580 Mask::new(64, 0), 581 Mask { 582 mask: 0xffff_ffff_ffff_ffff, 583 shift: 0 584 } 585 ); 586 assert_eq!( 587 Mask::new(64, 32), 588 Mask { 589 mask: 0xffff_ffff_ffff_ffff, 590 shift: 0 591 } 592 ); 593 assert_eq!( 594 Mask::new(64, 64), 595 Mask { 596 mask: 0xffff_ffff_ffff_ffff, 597 shift: 0 598 } 599 ); 600 assert_eq!( 601 Mask::new(64, 128), 602 Mask { 603 mask: 0xffff_ffff_ffff_ffff, 604 shift: 0 605 } 606 ); 607 608 assert_eq!( 609 Mask::new(128, 0), 610 Mask { 611 mask: 0xffff_ffff_ffff_ffff, 612 shift: 0 613 } 614 ); 615 assert_eq!( 616 Mask::new(128, 32), 617 Mask { 618 mask: 0xffff_ffff_ffff_ffff, 619 shift: 0 620 } 621 ); 622 assert_eq!( 623 Mask::new(128, 64), 624 Mask { 625 mask: 0xffff_ffff_ffff_ffff, 626 shift: 0 627 } 628 ); 629 assert_eq!( 630 Mask::new(128, 128), 631 Mask { 632 mask: 0xffff_ffff_ffff_ffff, 633 shift: 0 634 } 635 ); 636 } 637 638 /// UT test cases for bit from_usize function 639 /// 640 /// # Brief 641 /// 1. Pass in any usize, call from_usize, and check the return value. 642 #[test] ut_bit_from_usize()643 fn ut_bit_from_usize() { 644 const USIZE_MAX: usize = 0usize.wrapping_sub(1); 645 const USIZE_MAX_HALF: usize = 0usize.wrapping_sub(1) / 2; 646 647 assert_eq!(Bit::from_usize(0), Bit(0)); 648 assert_eq!(Bit::from_usize(USIZE_MAX_HALF), Bit(USIZE_MAX_HALF)); 649 assert_eq!(Bit::from_usize(USIZE_MAX), Bit(USIZE_MAX)); 650 } 651 652 /// UT test cases for bit as_usize function 653 /// 654 /// # Brief 655 /// 1. Creating a Bit Instance. 656 /// 2. Call as_usize. 657 /// 3. Check return value. 658 #[test] ut_bit_as_usize()659 fn ut_bit_as_usize() { 660 const USIZE_MAX: usize = 0usize.wrapping_sub(1); 661 const USIZE_MAX_HALF: usize = 0usize.wrapping_sub(1) / 2; 662 663 assert_eq!(Bit::from_usize(0).as_usize(), 0); 664 assert_eq!(Bit::from_usize(USIZE_MAX_HALF).as_usize(), USIZE_MAX_HALF); 665 assert_eq!(Bit::from_usize(USIZE_MAX).as_usize(), USIZE_MAX); 666 } 667 668 /// UT test cases for bit set function 669 /// 670 /// # Brief 671 /// 1. Creating a Bit Instance. 672 /// 2. Call the set function and pass in a new usize. 673 /// 3. Check return value. 674 #[test] ut_bit_set()675 fn ut_bit_set() { 676 const USIZE_MAX: usize = 0usize.wrapping_sub(1); 677 const USIZE_MAX_HALF: usize = 0usize.wrapping_sub(1) / 2; 678 679 let mut b = Bit::from_usize(0); 680 b.set(0xf0f0); 681 assert_eq!(b.as_usize(), 0xf0f0); 682 683 let mut b = Bit::from_usize(USIZE_MAX_HALF); 684 b.set(0xf0f0); 685 assert_eq!(b.as_usize(), 0xf0f0); 686 687 let mut b = Bit::from_usize(USIZE_MAX); 688 b.set(0xf0f0); 689 assert_eq!(b.as_usize(), 0xf0f0); 690 } 691 692 /// UT test cases for bit clear function 693 /// 694 /// # Brief 695 /// 1. Creating a Bit Instance. 696 /// 2. Call clear(). 697 /// 3. Calibrate the instance. 698 #[test] ut_bit_clear()699 fn ut_bit_clear() { 700 const USIZE_MAX: usize = 0usize.wrapping_sub(1); 701 const USIZE_MAX_HALF: usize = 0usize.wrapping_sub(1) / 2; 702 703 let mut b = Bit::from_usize(0); 704 b.clear(); 705 assert_eq!(b.as_usize(), 0); 706 707 let mut b = Bit::from_usize(USIZE_MAX_HALF); 708 b.clear(); 709 assert_eq!(b.as_usize(), 0); 710 711 let mut b = Bit::from_usize(USIZE_MAX); 712 b.clear(); 713 assert_eq!(b.as_usize(), 0); 714 } 715 716 /// UT test cases for bit set_by_mask function 717 /// 718 /// # Brief 719 /// 1. Create a Bit instance, create a Mask instance. 720 /// 2. Call set_by_mask(). 721 /// 3. Verify the Bit instance. 722 #[cfg(target_pointer_width = "32")] 723 #[test] ut_bit_set_by_mask_bit32()724 fn ut_bit_set_by_mask_bit32() { 725 let val = 0x0usize; 726 let mut bit = Bit::from_usize(val); 727 bit.set_by_mask(Mask::new(0, 0), 0x0); 728 assert_eq!(bit, Bit::from_usize(0x0)); 729 730 bit.clear(); 731 bit.set_by_mask(Mask::new(4, 0), 0xf); 732 assert_eq!(bit, Bit::from_usize(0xf)); 733 734 bit.clear(); 735 bit.set_by_mask(Mask::new(8, 0), 0xff); 736 assert_eq!(bit, Bit::from_usize(0xff)); 737 738 bit.clear(); 739 bit.set_by_mask(Mask::new(4, 4), 0xf); 740 assert_eq!(bit, Bit::from_usize(0xf0)); 741 742 bit.clear(); 743 bit.set_by_mask(Mask::new(4, 28), 0xf); 744 assert_eq!(bit, Bit::from_usize(0xf000_0000)); 745 746 bit.clear(); 747 bit.set_by_mask(Mask::new(0, 0), 0xffff_ffff); 748 assert_eq!(bit, Bit::from_usize(0x0)); 749 750 bit.clear(); 751 bit.set_by_mask(Mask::new(4, 0), 0xffff_ffff); 752 assert_eq!(bit, Bit::from_usize(0xf)); 753 754 bit.clear(); 755 bit.set_by_mask(Mask::new(4, 4), 0xffff_ffff); 756 assert_eq!(bit, Bit::from_usize(0xf0)); 757 758 let val = 0xffff_ffff; 759 let mut bit = Bit::from_usize(val); 760 bit.set_by_mask(Mask::new(0, 0), 0x0); 761 assert_eq!(bit, Bit::from_usize(0xffff_ffff)); 762 763 bit.set(val); 764 bit.set_by_mask(Mask::new(4, 0), 0x0); 765 assert_eq!(bit, Bit::from_usize(0xffff_fff0)); 766 767 bit.set(val); 768 bit.set_by_mask(Mask::new(4, 4), 0x0); 769 assert_eq!(bit, Bit::from_usize(0xffff_ff0f)); 770 771 bit.set(val); 772 bit.set_by_mask(Mask::new(4, 8), 0x0); 773 assert_eq!(bit, Bit::from_usize(0xffff_f0ff)); 774 775 bit.set(val); 776 bit.set_by_mask(Mask::new(4, 0), 0xabcd); 777 assert_eq!(bit, Bit::from_usize(0xffff_fffd)); 778 779 bit.set(val); 780 bit.set_by_mask(Mask::new(8, 0), 0xabcd); 781 assert_eq!(bit, Bit::from_usize(0xffff_ffcd)); 782 783 bit.set(val); 784 bit.set_by_mask(Mask::new(16, 0), 0xabcd); 785 assert_eq!(bit, Bit::from_usize(0xffff_abcd)); 786 787 bit.set(val); 788 bit.set_by_mask(Mask::new(16, 16), 0xabcd); 789 assert_eq!(bit, Bit::from_usize(0xabcd_ffff)); 790 791 bit.set(val); 792 bit.set_by_mask(Mask::new(32, 0), 0xabcd); 793 assert_eq!(bit, Bit::from_usize(0x0000_abcd)); 794 } 795 796 /// UT test cases for bit set_by_mask function 797 /// 798 /// # Brief 799 /// 1. Create a Bit instance, create a Mask instance. 800 /// 2. Call set_by_mask(). 801 /// 3. Verify the Bit instance. 802 #[cfg(target_pointer_width = "64")] 803 #[test] ut_bit_set_by_mask_bit64()804 fn ut_bit_set_by_mask_bit64() { 805 let val = 0x0usize; 806 let mut bit = Bit::from_usize(val); 807 bit.set_by_mask(Mask::new(0, 0), 0x0); 808 assert_eq!(bit, Bit::from_usize(0x0)); 809 810 bit.clear(); 811 bit.set_by_mask(Mask::new(4, 0), 0xf); 812 assert_eq!(bit, Bit::from_usize(0xf)); 813 814 bit.clear(); 815 bit.set_by_mask(Mask::new(8, 0), 0xff); 816 assert_eq!(bit, Bit::from_usize(0xff)); 817 818 bit.clear(); 819 bit.set_by_mask(Mask::new(4, 4), 0xf); 820 assert_eq!(bit, Bit::from_usize(0xf0)); 821 822 bit.clear(); 823 bit.set_by_mask(Mask::new(4, 32), 0xf); 824 assert_eq!(bit, Bit::from_usize(0xf_0000_0000)); 825 826 bit.clear(); 827 bit.set_by_mask(Mask::new(4, 0), 0xffff_ffff_ffff_ffff); 828 assert_eq!(bit, Bit::from_usize(0xf)); 829 830 bit.clear(); 831 bit.set_by_mask(Mask::new(4, 4), 0xffff_ffff_ffff_ffff); 832 assert_eq!(bit, Bit::from_usize(0xf0)); 833 834 bit.clear(); 835 bit.set_by_mask(Mask::new(0, 0), 0xffff_ffff_ffff_ffff); 836 assert_eq!(bit, Bit::from_usize(0)); 837 838 let val = 0xffff_ffff_ffff_ffffusize; 839 let mut bit = Bit::from_usize(val); 840 bit.set_by_mask(Mask::new(0, 0), 0x0); 841 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffff_ffff)); 842 843 bit.set(0xffff_ffff_ffff_ffff); 844 bit.set_by_mask(Mask::new(4, 0), 0x0); 845 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffff_fff0)); 846 847 bit.set(0xffff_ffff_ffff_ffff); 848 bit.set_by_mask(Mask::new(4, 4), 0x0); 849 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffff_ff0f)); 850 851 bit.set(0xffff_ffff_ffff_ffff); 852 bit.set_by_mask(Mask::new(4, 32), 0x0); 853 assert_eq!(bit, Bit::from_usize(0xffff_fff0_ffff_ffff)); 854 855 bit.set(0xffff_ffff_ffff_ffff); 856 bit.set_by_mask(Mask::new(4, 60), 0x0); 857 assert_eq!(bit, Bit::from_usize(0x0fff_ffff_ffff_ffff)); 858 859 bit.set(0xffff_ffff_ffff_ffff); 860 bit.set_by_mask(Mask::new(16, 0), 0xabcd); 861 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffff_abcd)); 862 863 bit.set(0xffff_ffff_ffff_ffff); 864 bit.set_by_mask(Mask::new(32, 0), 0xabcd); 865 assert_eq!(bit, Bit::from_usize(0xffff_ffff_0000_abcd)); 866 867 bit.set(0xffff_ffff_ffff_ffff); 868 bit.set_by_mask(Mask::new(12, 0), 0xabcd); 869 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffff_fbcd)); 870 871 bit.set(0xffff_ffff_ffff_ffff); 872 bit.set_by_mask(Mask::new(16, 8), 0xabcd); 873 assert_eq!(bit, Bit::from_usize(0xffff_ffff_ffab_cdff)); 874 } 875 876 /// UT test cases for bit get_by_mask function 877 /// 878 /// # Brief 879 /// 1. Create a Bit instance, create a Mask instance. 880 /// 2. Call get_by_mask(). 881 /// 3. Check return value. 882 #[cfg(target_pointer_width = "32")] 883 #[test] ut_bit_get_by_mask_bit32()884 fn ut_bit_get_by_mask_bit32() { 885 let val = 0x0usize; 886 let bit = Bit::from_usize(val); 887 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 888 assert_eq!(bit.get_by_mask(Mask::new(1, 0)), 0x0); 889 assert_eq!(bit.get_by_mask(Mask::new(16, 0)), 0x0); 890 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0x0); 891 892 let val = 0xffff_ffffusize; 893 let bit = Bit::from_usize(val); 894 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 895 assert_eq!(bit.get_by_mask(Mask::new(1, 0)), 0x1); 896 assert_eq!(bit.get_by_mask(Mask::new(16, 0)), 0xffff); 897 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0xffff_ffff); 898 899 let val = 0x1234_cdefusize; 900 let bit = Bit::from_usize(val); 901 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 902 assert_eq!(bit.get_by_mask(Mask::new(4, 0)), 0xf); 903 assert_eq!(bit.get_by_mask(Mask::new(8, 0)), 0xef); 904 assert_eq!(bit.get_by_mask(Mask::new(12, 0)), 0xdef); 905 assert_eq!(bit.get_by_mask(Mask::new(16, 0)), 0xcdef); 906 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0x1234_cdef); 907 assert_eq!(bit.get_by_mask(Mask::new(4, 4)), 0xe); 908 assert_eq!(bit.get_by_mask(Mask::new(8, 4)), 0xde); 909 assert_eq!(bit.get_by_mask(Mask::new(12, 4)), 0xcde); 910 assert_eq!(bit.get_by_mask(Mask::new(4, 16)), 0x4); 911 assert_eq!(bit.get_by_mask(Mask::new(8, 16)), 0x34); 912 assert_eq!(bit.get_by_mask(Mask::new(12, 16)), 0x234); 913 assert_eq!(bit.get_by_mask(Mask::new(3, 0)), 0x7); 914 assert_eq!(bit.get_by_mask(Mask::new(3, 4)), 0x6); 915 assert_eq!(bit.get_by_mask(Mask::new(3, 8)), 0x5); 916 assert_eq!(bit.get_by_mask(Mask::new(3, 16)), 0x4); 917 assert_eq!(bit.get_by_mask(Mask::new(3, 20)), 0x3); 918 assert_eq!(bit.get_by_mask(Mask::new(3, 24)), 0x2); 919 assert_eq!(bit.get_by_mask(Mask::new(3, 1)), 0x7); 920 assert_eq!(bit.get_by_mask(Mask::new(3, 5)), 0x7); 921 assert_eq!(bit.get_by_mask(Mask::new(3, 9)), 0x6); 922 } 923 924 /// UT test cases for bit get_by_mask function 925 /// 926 /// # Brief 927 /// 1. Create a Bit instance, create a Mask instance. 928 /// 2. Call get_by_mask(). 929 /// 3. Check return value. 930 #[cfg(target_pointer_width = "64")] 931 #[test] ut_bit_get_by_mask_bit64()932 fn ut_bit_get_by_mask_bit64() { 933 let val = 0x0usize; 934 let bit = Bit::from_usize(val); 935 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 936 assert_eq!(bit.get_by_mask(Mask::new(1, 0)), 0x0); 937 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0x0); 938 assert_eq!(bit.get_by_mask(Mask::new(64, 0)), 0x0); 939 940 let val = 0xffff_ffff_ffff_ffffusize; 941 let bit = Bit::from_usize(val); 942 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 943 assert_eq!(bit.get_by_mask(Mask::new(1, 0)), 0x1); 944 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0xffff_ffff); 945 assert_eq!(bit.get_by_mask(Mask::new(64, 0)), 0xffff_ffff_ffff_ffff); 946 947 let val = 0x0123_4567_89ab_cdefusize; 948 let bit = Bit::from_usize(val); 949 assert_eq!(bit.get_by_mask(Mask::new(0, 0)), 0x0); 950 assert_eq!(bit.get_by_mask(Mask::new(4, 0)), 0xf); 951 assert_eq!(bit.get_by_mask(Mask::new(8, 0)), 0xef); 952 assert_eq!(bit.get_by_mask(Mask::new(32, 0)), 0x89ab_cdef); 953 assert_eq!(bit.get_by_mask(Mask::new(64, 0)), 0x0123_4567_89ab_cdef); 954 assert_eq!(bit.get_by_mask(Mask::new(4, 4)), 0xe); 955 assert_eq!(bit.get_by_mask(Mask::new(8, 4)), 0xde); 956 assert_eq!(bit.get_by_mask(Mask::new(12, 4)), 0xcde); 957 assert_eq!(bit.get_by_mask(Mask::new(60, 4)), 0x0012_3456_789a_bcde); 958 assert_eq!(bit.get_by_mask(Mask::new(4, 32)), 0x7); 959 assert_eq!(bit.get_by_mask(Mask::new(8, 32)), 0x67); 960 assert_eq!(bit.get_by_mask(Mask::new(12, 32)), 0x567); 961 assert_eq!(bit.get_by_mask(Mask::new(3, 0)), 0x7); 962 assert_eq!(bit.get_by_mask(Mask::new(3, 4)), 0x6); 963 assert_eq!(bit.get_by_mask(Mask::new(3, 8)), 0x5); 964 assert_eq!(bit.get_by_mask(Mask::new(3, 1)), 0x7); 965 assert_eq!(bit.get_by_mask(Mask::new(3, 5)), 0x7); 966 assert_eq!(bit.get_by_mask(Mask::new(3, 9)), 0x6); 967 } 968 } 969