convert.py revision 1944
18733SN/A# Copyright (c) 2005 The Regents of The University of Michigan
28733SN/A# All rights reserved.
38733SN/A#
48733SN/A# Redistribution and use in source and binary forms, with or without
58733SN/A# modification, are permitted provided that the following conditions are
68733SN/A# met: redistributions of source code must retain the above copyright
78733SN/A# notice, this list of conditions and the following disclaimer;
88733SN/A# redistributions in binary form must reproduce the above copyright
98733SN/A# notice, this list of conditions and the following disclaimer in the
108733SN/A# documentation and/or other materials provided with the distribution;
118733SN/A# neither the name of the copyright holders nor the names of its
128733SN/A# contributors may be used to endorse or promote products derived from
138733SN/A# this software without specific prior written permission.
148733SN/A#
158733SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
168733SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
178733SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
188733SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
198733SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
208733SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
218733SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228733SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238733SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248733SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
258733SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268733SN/A
278733SN/A# metric prefixes
288733SN/Aexa  = 1.0e18
298733SN/Apeta = 1.0e15
308733SN/Atera = 1.0e12
318733SN/Agiga = 1.0e9
328733SN/Amega = 1.0e6
338733SN/Akilo = 1.0e3
348733SN/A
358733SN/Amilli = 1.0e-3
368733SN/Amicro = 1.0e-6
378733SN/Anano  = 1.0e-9
388733SN/Apico  = 1.0e-12
398733SN/Afemto = 1.0e-15
409340SAndreas.Sandberg@arm.comatto  = 1.0e-18
418733SN/A
428733SN/A# power of 2 prefixes
438733SN/Akibi = 1024
448733SN/Amebi = kibi * 1024
458733SN/Agibi = mebi * 1024
468733SN/Atebi = gibi * 1024
478733SN/Apebi = tebi * 1024
488733SN/Aexbi = pebi * 1024
498733SN/A
508733SN/A# memory size configuration stuff
518733SN/Adef toFloat(value):
528733SN/A    if not isinstance(value, str):
538733SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
548733SN/A
558733SN/A    if value.endswith('Ei'):
568733SN/A        return float(value[:-2]) * exbi
578733SN/A    elif value.endswith('Pi'):
588733SN/A        return float(value[:-2]) * pebi
598733SN/A    elif value.endswith('Ti'):
608733SN/A        return float(value[:-2]) * tebi
618733SN/A    elif value.endswith('Gi'):
628733SN/A        return float(value[:-2]) * gibi
638733SN/A    elif value.endswith('Mi'):
648733SN/A        return float(value[:-2]) * mebi
658887SN/A    elif value.endswith('ki'):
668733SN/A        return float(value[:-2]) * kibi
678733SN/A    elif value.endswith('E'):
688733SN/A        return float(value[:-1]) * exa
698733SN/A    elif value.endswith('P'):
708733SN/A        return float(value[:-1]) * peta
718733SN/A    elif value.endswith('T'):
729384SAndreas.Sandberg@arm.com        return float(value[:-1]) * tera
738733SN/A    elif value.endswith('G'):
748733SN/A        return float(value[:-1]) * giga
758733SN/A    elif value.endswith('M'):
768733SN/A        return float(value[:-1]) * mega
778733SN/A    elif value.endswith('k'):
788733SN/A        return float(value[:-1]) * kilo
798733SN/A    elif value.endswith('m'):
808733SN/A        return float(value[:-1]) * milli
818733SN/A    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