| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * OpenSPARC T2 Processor File: BL_BitUtility.h |
| 5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. |
| 7 | * |
| 8 | * The above named program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public |
| 10 | * License version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * The above named program is distributed in the hope that it will be |
| 13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this work; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * ========== Copyright Header End ============================================ |
| 22 | */ |
| 23 | /************************************************************************ |
| 24 | ** |
| 25 | ** Copyright (C) 2006, Sun Microsystems, Inc. |
| 26 | ** |
| 27 | ** Sun considers its source code as an unpublished, proprietary |
| 28 | ** trade secret and it is available only under strict license provisions. |
| 29 | ** This copyright notice is placed here only to protect Sun in the event |
| 30 | ** the source is deemed a published work. Disassembly, decompilation, |
| 31 | ** or other means of reducing the object code to human readable form |
| 32 | ** is prohibited by the license agreement under which this code is |
| 33 | ** provided to the user or company in possession of this copy. |
| 34 | ** |
| 35 | *************************************************************************/ |
| 36 | #ifndef __BL_BitUtility_h__ |
| 37 | #define __BL_BitUtility_h__ |
| 38 | |
| 39 | #include <unistd.h> |
| 40 | #include <iostream.h> |
| 41 | |
| 42 | #include "BL_Types.h" |
| 43 | |
| 44 | class BL_BitUtility |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | BL_BitUtility() {} |
| 49 | |
| 50 | ~BL_BitUtility() {} |
| 51 | |
| 52 | // This method calculates the parity value for up to 64 bits of |
| 53 | // data using 4 XORs with a clever shift and returns a parity |
| 54 | // value of either 0 or 1. |
| 55 | // Data should be right justified and unused bits must be set to zero. |
| 56 | static uint32_t calc_parity(uint64_t data) |
| 57 | { |
| 58 | uint64_t p; |
| 59 | p = data ^ (data >> 32); |
| 60 | p = p ^ (p >> 16); |
| 61 | p = p ^ (p >> 8); |
| 62 | p = p ^ (p >> 4); |
| 63 | p &= 0xf; |
| 64 | return (0x6996 >> p) & 1; |
| 65 | } |
| 66 | |
| 67 | // This method calculates the parity value for a BL_Uint256 (or |
| 68 | // 256 bits of data). |
| 69 | static uint32_t calc_parity(BL_Uint256 data) |
| 70 | { |
| 71 | return calc_parity(data.get(0)) ^ calc_parity(data.get(1)) ^ |
| 72 | calc_parity(data.get(2)) ^ calc_parity(data.get(3)); |
| 73 | } |
| 74 | |
| 75 | // This method calculates the byte-by_byte parity values for a |
| 76 | // 64-bit value. |
| 77 | // The parities are returned packed in 8 bits. |
| 78 | static uint8_t calc_byte_parities(uint64_t data) |
| 79 | { |
| 80 | uint64_t p; |
| 81 | p = data ^ (data >> 4); |
| 82 | p = p ^ (p >> 2); |
| 83 | p = p ^ (p >> 1); |
| 84 | p &= 0x0101010101010101ULL; |
| 85 | p |= (p >> 28); |
| 86 | p |= (p >> 14); |
| 87 | p |= (p >> 7); |
| 88 | return p; |
| 89 | } |
| 90 | |
| 91 | // This routine can be used to set a certain number of bits within a 64 bit |
| 92 | // value. The routine takes the super set, subset, starting index and |
| 93 | // ending index as its four input parameters. |
| 94 | static uint64_t set_subfield(uint64_t data, uint64_t subfield, uint64_t start_offset, uint64_t end_offset) |
| 95 | { |
| 96 | uint64_t width = (end_offset - start_offset) + 1; |
| 97 | assert(width < 64); |
| 98 | uint64_t mask = (((uint64_t(1) << width) - 1) << start_offset); |
| 99 | data = (data &~ mask) | ((subfield << start_offset) & mask); |
| 100 | return data; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | #endif |