convert.py revision 1944
14202SN/A# Copyright (c) 2005 The Regents of The University of Michigan
24202SN/A# All rights reserved.
34202SN/A#
44202SN/A# Redistribution and use in source and binary forms, with or without
54202SN/A# modification, are permitted provided that the following conditions are
64202SN/A# met: redistributions of source code must retain the above copyright
74202SN/A# notice, this list of conditions and the following disclaimer;
84202SN/A# redistributions in binary form must reproduce the above copyright
94202SN/A# notice, this list of conditions and the following disclaimer in the
104202SN/A# documentation and/or other materials provided with the distribution;
114202SN/A# neither the name of the copyright holders nor the names of its
124202SN/A# contributors may be used to endorse or promote products derived from
134202SN/A# this software without specific prior written permission.
144202SN/A#
154202SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164202SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174202SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
184202SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
194202SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
204202SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
214202SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224202SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234202SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244202SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254202SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264202SN/A
274202SN/A# metric prefixes
284202SN/Aexa  = 1.0e18
294202SN/Apeta = 1.0e15
304202SN/Atera = 1.0e12
314202SN/Agiga = 1.0e9
324202SN/Amega = 1.0e6
336629SN/Akilo = 1.0e3
346629SN/A
354486SN/Amilli = 1.0e-3
366629SN/Amicro = 1.0e-6
375192SN/Anano  = 1.0e-9
388335Snate@binkert.orgpico  = 1.0e-12
39femto = 1.0e-15
40atto  = 1.0e-18
41
42# power of 2 prefixes
43kibi = 1024
44mebi = kibi * 1024
45gibi = mebi * 1024
46tebi = gibi * 1024
47pebi = tebi * 1024
48exbi = pebi * 1024
49
50# memory size configuration stuff
51def toFloat(value):
52    if not isinstance(value, str):
53        raise TypeError, "wrong type '%s' should be str" % type(value)
54
55    if value.endswith('Ei'):
56        return float(value[:-2]) * exbi
57    elif value.endswith('Pi'):
58        return float(value[:-2]) * pebi
59    elif value.endswith('Ti'):
60        return float(value[:-2]) * tebi
61    elif value.endswith('Gi'):
62        return float(value[:-2]) * gibi
63    elif value.endswith('Mi'):
64        return float(value[:-2]) * mebi
65    elif value.endswith('ki'):
66        return float(value[:-2]) * kibi
67    elif value.endswith('E'):
68        return float(value[:-1]) * exa
69    elif value.endswith('P'):
70        return float(value[:-1]) * peta
71    elif value.endswith('T'):
72        return float(value[:-1]) * tera
73    elif value.endswith('G'):
74        return float(value[:-1]) * giga
75    elif value.endswith('M'):
76        return float(value[:-1]) * mega
77    elif value.endswith('k'):
78        return float(value[:-1]) * kilo
79    elif value.endswith('m'):
80        return float(value[:-1]) * milli
81    elif value.endswith('u'):
82        return float(value[:-1]) * micro
83    elif value.endswith('n'):
84        return float(value[:-1]) * nano
85    elif value.endswith('p'):
86        return float(value[:-1]) * pico
87    elif value.endswith('f'):
88        return float(value[:-1]) * femto
89    else:
90        return float(value)
91
92def toInteger(value):
93    value = toFloat(value)
94    result = long(value)
95    if value != result:
96        raise ValueError, "cannot convert '%s' to integer" % value
97
98    return result
99
100_bool_dict = {
101    'true' : True,   't' : True,  'yes' : True, 'y' : True,  '1' : True,
102    'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False
103    }
104
105def toBool(value):
106    if not isinstance(value, str):
107        raise TypeError, "wrong type '%s' should be str" % type(value)
108
109    value = value.lower()
110    result = _bool_dict.get(value, None)
111    if result == None:
112        raise ValueError, "cannot convert '%s' to bool" % value
113    return result
114
115def toFrequency(value):
116    if not isinstance(value, str):
117        raise TypeError, "wrong type '%s' should be str" % type(value)
118
119    if value.endswith('THz'):
120        return float(value[:-3]) * tera
121    elif value.endswith('GHz'):
122        return float(value[:-3]) * giga
123    elif value.endswith('MHz'):
124        return float(value[:-3]) * mega
125    elif value.endswith('kHz'):
126        return float(value[:-3]) * kilo
127    elif value.endswith('Hz'):
128        return float(value[:-2])
129
130    raise ValueError, "cannot convert '%s' to frequency" % value
131
132def toLatency(value):
133    if not isinstance(value, str):
134        raise TypeError, "wrong type '%s' should be str" % type(value)
135
136    if value.endswith('ps'):
137        return float(value[:-2]) * pico
138    elif value.endswith('ns'):
139        return float(value[:-2]) * nano
140    elif value.endswith('us'):
141        return float(value[:-2]) * micro
142    elif value.endswith('ms'):
143        return float(value[:-2]) * milli
144    elif value.endswith('s'):
145        return float(value[:-1])
146
147    raise ValueError, "cannot convert '%s' to latency" % value
148
149def toClockPeriod(value):
150    """result is a clock period"""
151
152    if not isinstance(value, str):
153        raise TypeError, "wrong type '%s' should be str" % type(value)
154
155    try:
156        val = toFrequency(value)
157        if val != 0:
158            val = 1 / val
159        return val
160    except ValueError:
161        pass
162
163    try:
164        val = toLatency(value)
165        return val
166    except ValueError:
167        pass
168
169    raise ValueError, "cannot convert '%s' to clock period" % value
170
171
172def toNetworkBandwidth(value):
173    if not isinstance(value, str):
174        raise TypeError, "wrong type '%s' should be str" % type(value)
175
176    if value.endswith('Tbps'):
177        return float(value[:-4]) * tera
178    elif value.endswith('Gbps'):
179        return float(value[:-4]) * giga
180    elif value.endswith('Mbps'):
181        return float(value[:-4]) * mega
182    elif value.endswith('kbps'):
183        return float(value[:-4]) * kilo
184    elif value.endswith('bps'):
185        return float(value[:-3])
186    else:
187        return float(value)
188
189    raise ValueError, "cannot convert '%s' to network bandwidth" % value
190
191def toMemoryBandwidth(value):
192    if not isinstance(value, str):
193        raise TypeError, "wrong type '%s' should be str" % type(value)
194
195    if value.endswith('PB/s'):
196        return float(value[:-4]) * pebi
197    elif value.endswith('TB/s'):
198        return float(value[:-4]) * tebi
199    elif value.endswith('GB/s'):
200        return float(value[:-4]) * gibi
201    elif value.endswith('MB/s'):
202        return float(value[:-4]) * mebi
203    elif value.endswith('kB/s'):
204        return float(value[:-4]) * kibi
205    elif value.endswith('B/s'):
206        return float(value[:-3])
207
208    raise ValueError, "cannot convert '%s' to memory bandwidth" % value
209
210def toMemorySize(value):
211    if not isinstance(value, str):
212        raise TypeError, "wrong type '%s' should be str" % type(value)
213
214    if value.endswith('PB'):
215        return long(value[:-2]) * pebi
216    elif value.endswith('TB'):
217        return long(value[:-2]) * tebi
218    elif value.endswith('GB'):
219        return long(value[:-2]) * gibi
220    elif value.endswith('MB'):
221        return long(value[:-2]) * mebi
222    elif value.endswith('kB'):
223        return long(value[:-2]) * kibi
224    elif value.endswith('B'):
225        return long(value[:-1])
226
227    raise ValueError, "cannot convert '%s' to memory size" % value
228