Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / cpus / vonk / bl / api / pfe / src / Pfe_HexDec.h
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: Pfe_HexDec.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 ============================================
*/
#include "longintrepr.h"
PyObject* long_format( PyObject* aa, int base, int addL )/*{{{*/
{
#define ABS(x) ((x) < 0 ? -(x) : (x))
register PyLongObject *a = (PyLongObject *)aa;
PyStringObject *str;
int i;
const int size_a = ABS(a->ob_size);
char *p;
int bits;
char sign = '\0';
if (a == NULL || !PyLong_Check(a)) {
PyErr_BadInternalCall();
return NULL;
}
assert(base >= 2 && base <= 36);
/* Compute a rough upper bound for the length of the string */
i = base;
bits = 0;
while (i > 1) {
++bits;
i >>= 1;
}
i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits;
str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
if (str == NULL)
return NULL;
p = PyString_AS_STRING(str) + i;
*p = '\0';
if (addL)
*--p = 'L';
if (a->ob_size < 0)
sign = '-';
if (a->ob_size == 0) {
*--p = '0';
}
else if ((base & (base - 1)) == 0) {
/* JRH: special case for power-of-2 bases */
twodigits accum = 0;
int accumbits = 0; /* # of bits in accum */
int basebits = 1; /* # of bits in base-1 */
i = base;
while ((i >>= 1) > 1)
++basebits;
for (i = 0; i < size_a; ++i) {
accum |= (twodigits)a->ob_digit[i] << accumbits;
accumbits += SHIFT;
assert(accumbits >= basebits);
do {
char cdigit = (char)(accum & (base - 1));
cdigit += (cdigit < 10) ? '0' : 'a'-10;
assert(p > PyString_AS_STRING(str));
*--p = cdigit;
accumbits -= basebits;
accum >>= basebits;
} while (i < size_a-1 ? accumbits >= basebits :
accum > 0);
}
}
if (base == 8) {
if (size_a != 0)
*--p = '0';
}
else if (base == 16) {
*--p = 'x';
*--p = '0';
}
else if (base != 10) {
*--p = '#';
*--p = '0' + base%10;
if (base > 10)
*--p = '0' + base/10;
}
if (sign)
*--p = sign;
if (p != PyString_AS_STRING(str)) {
char *q = PyString_AS_STRING(str);
assert(p > q);
do {
} while ((*q++ = *p++) != '\0');
q--;
_PyString_Resize((PyObject **)&str,
(int) (q - PyString_AS_STRING(str)));
}
return (PyObject *)str;
}
/*}}}*/
PyObject* long_hex_repr( PyObject* v )/*{{{*/
{
return long_format(v,16,1);
}
/*}}}*/
PyObject* long_hex_str( PyObject* v )/*{{{*/
{
return long_format(v,16,0);
}
/*}}}*/
int_hex_print( PyIntObject* v, FILE* fp, int flags )/*{{{*/
{
fprintf(fp,"0x%lx",v->ob_ival);
return 0;
}
/*}}}*/
PyObject* int_hex_repr( PyIntObject* v )/*{{{*/
{
char buf[64];
PyOS_snprintf(buf, sizeof(buf), "0x%lx", v->ob_ival);
return PyString_FromString(buf);
}
/*}}}*/
extern PyTypeObject PyInt_Type;
extern PyTypeObject PyLong_Type;
static printfunc int_dec_print = 0;
static reprfunc long_dec_repr;
static reprfunc long_dec_str;
static reprfunc int_dec_repr;
static reprfunc int_dec_str;
void hexmode()/*{{{*/
{
if (int_dec_print == 0)
{
long_dec_repr = PyLong_Type.tp_repr;
long_dec_str = PyLong_Type.tp_str;
int_dec_repr = PyInt_Type.tp_repr;
int_dec_str = PyInt_Type.tp_str;
int_dec_print = PyInt_Type.tp_print;
}
PyLong_Type.tp_repr = (reprfunc)long_hex_repr;
PyLong_Type.tp_str = (reprfunc)long_hex_str;
PyInt_Type.tp_repr = (reprfunc)int_hex_repr;
PyInt_Type.tp_str = (reprfunc)int_hex_repr;
PyInt_Type.tp_print = (printfunc)int_hex_print;
}
/*}}}*/
void decmode()/*{{{*/
{
PyLong_Type.tp_repr = (reprfunc)long_dec_repr;
PyLong_Type.tp_str = (reprfunc)long_dec_str;
PyInt_Type.tp_repr = (reprfunc)int_dec_repr;
PyInt_Type.tp_str = (reprfunc)int_dec_repr;
PyInt_Type.tp_print = (printfunc)int_dec_print;
}
/*}}}*/