Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: Pfe_HexDec.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 | #include "longintrepr.h" | |
25 | ||
26 | PyObject* long_format( PyObject* aa, int base, int addL )/*{{{*/ | |
27 | { | |
28 | #define ABS(x) ((x) < 0 ? -(x) : (x)) | |
29 | ||
30 | register PyLongObject *a = (PyLongObject *)aa; | |
31 | PyStringObject *str; | |
32 | int i; | |
33 | const int size_a = ABS(a->ob_size); | |
34 | char *p; | |
35 | int bits; | |
36 | char sign = '\0'; | |
37 | ||
38 | if (a == NULL || !PyLong_Check(a)) { | |
39 | PyErr_BadInternalCall(); | |
40 | return NULL; | |
41 | } | |
42 | assert(base >= 2 && base <= 36); | |
43 | ||
44 | /* Compute a rough upper bound for the length of the string */ | |
45 | i = base; | |
46 | bits = 0; | |
47 | while (i > 1) { | |
48 | ++bits; | |
49 | i >>= 1; | |
50 | } | |
51 | i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits; | |
52 | str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); | |
53 | if (str == NULL) | |
54 | return NULL; | |
55 | p = PyString_AS_STRING(str) + i; | |
56 | *p = '\0'; | |
57 | if (addL) | |
58 | *--p = 'L'; | |
59 | if (a->ob_size < 0) | |
60 | sign = '-'; | |
61 | ||
62 | if (a->ob_size == 0) { | |
63 | *--p = '0'; | |
64 | } | |
65 | else if ((base & (base - 1)) == 0) { | |
66 | /* JRH: special case for power-of-2 bases */ | |
67 | twodigits accum = 0; | |
68 | int accumbits = 0; /* # of bits in accum */ | |
69 | int basebits = 1; /* # of bits in base-1 */ | |
70 | i = base; | |
71 | while ((i >>= 1) > 1) | |
72 | ++basebits; | |
73 | ||
74 | for (i = 0; i < size_a; ++i) { | |
75 | accum |= (twodigits)a->ob_digit[i] << accumbits; | |
76 | accumbits += SHIFT; | |
77 | assert(accumbits >= basebits); | |
78 | do { | |
79 | char cdigit = (char)(accum & (base - 1)); | |
80 | cdigit += (cdigit < 10) ? '0' : 'a'-10; | |
81 | assert(p > PyString_AS_STRING(str)); | |
82 | *--p = cdigit; | |
83 | accumbits -= basebits; | |
84 | accum >>= basebits; | |
85 | } while (i < size_a-1 ? accumbits >= basebits : | |
86 | accum > 0); | |
87 | } | |
88 | } | |
89 | ||
90 | if (base == 8) { | |
91 | if (size_a != 0) | |
92 | *--p = '0'; | |
93 | } | |
94 | else if (base == 16) { | |
95 | *--p = 'x'; | |
96 | *--p = '0'; | |
97 | } | |
98 | else if (base != 10) { | |
99 | *--p = '#'; | |
100 | *--p = '0' + base%10; | |
101 | if (base > 10) | |
102 | *--p = '0' + base/10; | |
103 | } | |
104 | if (sign) | |
105 | *--p = sign; | |
106 | if (p != PyString_AS_STRING(str)) { | |
107 | char *q = PyString_AS_STRING(str); | |
108 | assert(p > q); | |
109 | do { | |
110 | } while ((*q++ = *p++) != '\0'); | |
111 | q--; | |
112 | _PyString_Resize((PyObject **)&str, | |
113 | (int) (q - PyString_AS_STRING(str))); | |
114 | } | |
115 | return (PyObject *)str; | |
116 | } | |
117 | /*}}}*/ | |
118 | PyObject* long_hex_repr( PyObject* v )/*{{{*/ | |
119 | { | |
120 | return long_format(v,16,1); | |
121 | } | |
122 | /*}}}*/ | |
123 | PyObject* long_hex_str( PyObject* v )/*{{{*/ | |
124 | { | |
125 | return long_format(v,16,0); | |
126 | } | |
127 | /*}}}*/ | |
128 | ||
129 | int_hex_print( PyIntObject* v, FILE* fp, int flags )/*{{{*/ | |
130 | { | |
131 | fprintf(fp,"0x%lx",v->ob_ival); | |
132 | return 0; | |
133 | } | |
134 | /*}}}*/ | |
135 | PyObject* int_hex_repr( PyIntObject* v )/*{{{*/ | |
136 | { | |
137 | char buf[64]; | |
138 | PyOS_snprintf(buf, sizeof(buf), "0x%lx", v->ob_ival); | |
139 | return PyString_FromString(buf); | |
140 | } | |
141 | /*}}}*/ | |
142 | ||
143 | extern PyTypeObject PyInt_Type; | |
144 | extern PyTypeObject PyLong_Type; | |
145 | ||
146 | static printfunc int_dec_print = 0; | |
147 | static reprfunc long_dec_repr; | |
148 | static reprfunc long_dec_str; | |
149 | static reprfunc int_dec_repr; | |
150 | static reprfunc int_dec_str; | |
151 | ||
152 | void hexmode()/*{{{*/ | |
153 | { | |
154 | if (int_dec_print == 0) | |
155 | { | |
156 | long_dec_repr = PyLong_Type.tp_repr; | |
157 | long_dec_str = PyLong_Type.tp_str; | |
158 | int_dec_repr = PyInt_Type.tp_repr; | |
159 | int_dec_str = PyInt_Type.tp_str; | |
160 | int_dec_print = PyInt_Type.tp_print; | |
161 | } | |
162 | ||
163 | PyLong_Type.tp_repr = (reprfunc)long_hex_repr; | |
164 | PyLong_Type.tp_str = (reprfunc)long_hex_str; | |
165 | PyInt_Type.tp_repr = (reprfunc)int_hex_repr; | |
166 | PyInt_Type.tp_str = (reprfunc)int_hex_repr; | |
167 | PyInt_Type.tp_print = (printfunc)int_hex_print; | |
168 | } | |
169 | /*}}}*/ | |
170 | void decmode()/*{{{*/ | |
171 | { | |
172 | PyLong_Type.tp_repr = (reprfunc)long_dec_repr; | |
173 | PyLong_Type.tp_str = (reprfunc)long_dec_str; | |
174 | PyInt_Type.tp_repr = (reprfunc)int_dec_repr; | |
175 | PyInt_Type.tp_str = (reprfunc)int_dec_repr; | |
176 | PyInt_Type.tp_print = (printfunc)int_dec_print; | |
177 | } | |
178 | /*}}}*/ | |
179 | ||
180 | ||
181 | ||
182 | ||
183 |