Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / bl / lib / ecc / src / BL_BitUtility.h
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: BL_BitUtility.h
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
/************************************************************************
**
** Copyright (C) 2006, Sun Microsystems, Inc.
**
** Sun considers its source code as an unpublished, proprietary
** trade secret and it is available only under strict license provisions.
** This copyright notice is placed here only to protect Sun in the event
** the source is deemed a published work. Disassembly, decompilation,
** or other means of reducing the object code to human readable form
** is prohibited by the license agreement under which this code is
** provided to the user or company in possession of this copy.
**
*************************************************************************/
#ifndef __BL_BitUtility_h__
#define __BL_BitUtility_h__
#include <unistd.h>
#include <iostream.h>
#include "BL_Types.h"
class BL_BitUtility
{
public:
BL_BitUtility() {}
~BL_BitUtility() {}
// This method calculates the parity value for up to 64 bits of
// data using 4 XORs with a clever shift and returns a parity
// value of either 0 or 1.
// Data should be right justified and unused bits must be set to zero.
static uint32_t calc_parity(uint64_t data)
{
uint64_t p;
p = data ^ (data >> 32);
p = p ^ (p >> 16);
p = p ^ (p >> 8);
p = p ^ (p >> 4);
p &= 0xf;
return (0x6996 >> p) & 1;
}
// This method calculates the parity value for a BL_Uint256 (or
// 256 bits of data).
static uint32_t calc_parity(BL_Uint256 data)
{
return calc_parity(data.get(0)) ^ calc_parity(data.get(1)) ^
calc_parity(data.get(2)) ^ calc_parity(data.get(3));
}
// This method calculates the byte-by_byte parity values for a
// 64-bit value.
// The parities are returned packed in 8 bits.
static uint8_t calc_byte_parities(uint64_t data)
{
uint64_t p;
p = data ^ (data >> 4);
p = p ^ (p >> 2);
p = p ^ (p >> 1);
p &= 0x0101010101010101ULL;
p |= (p >> 28);
p |= (p >> 14);
p |= (p >> 7);
return p;
}
// This routine can be used to set a certain number of bits within a 64 bit
// value. The routine takes the super set, subset, starting index and
// ending index as its four input parameters.
static uint64_t set_subfield(uint64_t data, uint64_t subfield, uint64_t start_offset, uint64_t end_offset)
{
uint64_t width = (end_offset - start_offset) + 1;
assert(width < 64);
uint64_t mask = (((uint64_t(1) << width) - 1) << start_offset);
data = (data &~ mask) | ((subfield << start_offset) & mask);
return data;
}
};
#endif