convert.py revision 1944
17513SN/A# Copyright (c) 2005 The Regents of The University of Michigan
27513SN/A# All rights reserved.
37513SN/A#
410036SAli.Saidi@ARM.com# Redistribution and use in source and binary forms, with or without
58835SAli.Saidi@ARM.com# modification, are permitted provided that the following conditions are
610036SAli.Saidi@ARM.com# met: redistributions of source code must retain the above copyright
77935SN/A# notice, this list of conditions and the following disclaimer;
87935SN/A# redistributions in binary form must reproduce the above copyright
97935SN/A# notice, this list of conditions and the following disclaimer in the
107513SN/A# documentation and/or other materials provided with the distribution;
117513SN/A# neither the name of the copyright holders nor the names of its
127513SN/A# contributors may be used to endorse or promote products derived from
139885Sstever@gmail.com# this software without specific prior written permission.
148835SAli.Saidi@ARM.com#
159885Sstever@gmail.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
169885Sstever@gmail.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1710036SAli.Saidi@ARM.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
188835SAli.Saidi@ARM.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
198835SAli.Saidi@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
208835SAli.Saidi@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
217513SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229481Snilay@cs.wisc.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238721SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248721SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
258835SAli.Saidi@ARM.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268835SAli.Saidi@ARM.com
277935SN/A# metric prefixes
287935SN/Aexa  = 1.0e18
297935SN/Apeta = 1.0e15
307935SN/Atera = 1.0e12
317935SN/Agiga = 1.0e9
327935SN/Amega = 1.0e6
337935SN/Akilo = 1.0e3
348893Ssaidi@eecs.umich.edu
357513SN/Amilli = 1.0e-3
369885Sstever@gmail.commicro = 1.0e-6
379885Sstever@gmail.comnano  = 1.0e-9
389885Sstever@gmail.compico  = 1.0e-12
3910036SAli.Saidi@ARM.comfemto = 1.0e-15
409885Sstever@gmail.comatto  = 1.0e-18
419885Sstever@gmail.com
427513SN/A# power of 2 prefixes
437513SN/Akibi = 1024
449481Snilay@cs.wisc.edumebi = kibi * 1024
457513SN/Agibi = mebi * 1024
469885Sstever@gmail.comtebi = gibi * 1024
477513SN/Apebi = tebi * 1024
487513SN/Aexbi = pebi * 1024
498835SAli.Saidi@ARM.com
507513SN/A# memory size configuration stuff
517513SN/Adef toFloat(value):
5210036SAli.Saidi@ARM.com    if not isinstance(value, str):
538983Snate@binkert.org        raise TypeError, "wrong type '%s' should be str" % type(value)
547513SN/A
557513SN/A    if value.endswith('Ei'):
568835SAli.Saidi@ARM.com        return float(value[:-2]) * exbi
579481Snilay@cs.wisc.edu    elif value.endswith('Pi'):
587513SN/A        return float(value[:-2]) * pebi
597513SN/A    elif value.endswith('Ti'):
607513SN/A        return float(value[:-2]) * tebi
617513SN/A    elif value.endswith('Gi'):
627513SN/A        return float(value[:-2]) * gibi
637513SN/A    elif value.endswith('Mi'):
648835SAli.Saidi@ARM.com        return float(value[:-2]) * mebi
657513SN/A    elif value.endswith('ki'):
669885Sstever@gmail.com        return float(value[:-2]) * kibi
679885Sstever@gmail.com    elif value.endswith('E'):
689885Sstever@gmail.com        return float(value[:-1]) * exa
699885Sstever@gmail.com    elif value.endswith('P'):
707513SN/A        return float(value[:-1]) * peta
717513SN/A    elif value.endswith('T'):
729481Snilay@cs.wisc.edu        return float(value[:-1]) * tera
737513SN/A    elif value.endswith('G'):
747513SN/A        return float(value[:-1]) * giga
757513SN/A    elif value.endswith('M'):
767513SN/A        return float(value[:-1]) * mega
778893Ssaidi@eecs.umich.edu    elif value.endswith('k'):
788893Ssaidi@eecs.umich.edu        return float(value[:-1]) * kilo
797513SN/A    elif value.endswith('m'):
807513SN/A        return float(value[:-1]) * milli
817513SN/A    elif value.endswith('u'):
828835SAli.Saidi@ARM.com        return float(value[:-1]) * micro
8310036SAli.Saidi@ARM.com    elif value.endswith('n'):
847513SN/A        return float(value[:-1]) * nano
858835SAli.Saidi@ARM.com    elif value.endswith('p'):
868835SAli.Saidi@ARM.com        return float(value[:-1]) * pico
878835SAli.Saidi@ARM.com    elif value.endswith('f'):
888835SAli.Saidi@ARM.com        return float(value[:-1]) * femto
899885Sstever@gmail.com    else:
9010036SAli.Saidi@ARM.com        return float(value)
919265SAli.Saidi@ARM.com
928835SAli.Saidi@ARM.comdef toInteger(value):
938893Ssaidi@eecs.umich.edu    value = toFloat(value)
948835SAli.Saidi@ARM.com    result = long(value)
958835SAli.Saidi@ARM.com    if value != result:
968835SAli.Saidi@ARM.com        raise ValueError, "cannot convert '%s' to integer" % value
9710036SAli.Saidi@ARM.com
987513SN/A    return result
999481Snilay@cs.wisc.edu
1009481Snilay@cs.wisc.edu_bool_dict = {
10110036SAli.Saidi@ARM.com    'true' : True,   't' : True,  'yes' : True, 'y' : True,  '1' : True,
1029481Snilay@cs.wisc.edu    'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False
1039481Snilay@cs.wisc.edu    }
1049481Snilay@cs.wisc.edu
1059481Snilay@cs.wisc.edudef toBool(value):
1069481Snilay@cs.wisc.edu    if not isinstance(value, str):
1079481Snilay@cs.wisc.edu        raise TypeError, "wrong type '%s' should be str" % type(value)
1089481Snilay@cs.wisc.edu
1099481Snilay@cs.wisc.edu    value = value.lower()
1109481Snilay@cs.wisc.edu    result = _bool_dict.get(value, None)
1119481Snilay@cs.wisc.edu    if result == None:
1129481Snilay@cs.wisc.edu        raise ValueError, "cannot convert '%s' to bool" % value
1139481Snilay@cs.wisc.edu    return result
1149481Snilay@cs.wisc.edu
1159481Snilay@cs.wisc.edudef toFrequency(value):
1169481Snilay@cs.wisc.edu    if not isinstance(value, str):
1177513SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1187513SN/A
1198835SAli.Saidi@ARM.com    if value.endswith('THz'):
12010036SAli.Saidi@ARM.com        return float(value[:-3]) * tera
1217513SN/A    elif value.endswith('GHz'):
1228835SAli.Saidi@ARM.com        return float(value[:-3]) * giga
1238835SAli.Saidi@ARM.com    elif value.endswith('MHz'):
1248835SAli.Saidi@ARM.com        return float(value[:-3]) * mega
1258835SAli.Saidi@ARM.com    elif value.endswith('kHz'):
1269885Sstever@gmail.com        return float(value[:-3]) * kilo
12710036SAli.Saidi@ARM.com    elif value.endswith('Hz'):
1289265SAli.Saidi@ARM.com        return float(value[:-2])
1298835SAli.Saidi@ARM.com
1308893Ssaidi@eecs.umich.edu    raise ValueError, "cannot convert '%s' to frequency" % value
1317513SN/A
1327513SN/Adef toLatency(value):
1337513SN/A    if not isinstance(value, str):
13410036SAli.Saidi@ARM.com        raise TypeError, "wrong type '%s' should be str" % type(value)
1357513SN/A
1367513SN/A    if value.endswith('ps'):
1377513SN/A        return float(value[:-2]) * pico
1387513SN/A    elif value.endswith('ns'):
1399265SAli.Saidi@ARM.com        return float(value[:-2]) * nano
1407513SN/A    elif value.endswith('us'):
1417513SN/A        return float(value[:-2]) * micro
1427513SN/A    elif value.endswith('ms'):
1437513SN/A        return float(value[:-2]) * milli
14410036SAli.Saidi@ARM.com    elif value.endswith('s'):
14510036SAli.Saidi@ARM.com        return float(value[:-1])
1467513SN/A
14710036SAli.Saidi@ARM.com    raise ValueError, "cannot convert '%s' to latency" % value
1487513SN/A
1497513SN/Adef toClockPeriod(value):
1507513SN/A    """result is a clock period"""
1517513SN/A
1527513SN/A    if not isinstance(value, str):
1537513SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1547513SN/A
1557513SN/A    try:
1569885Sstever@gmail.com        val = toFrequency(value)
1579885Sstever@gmail.com        if val != 0:
1589885Sstever@gmail.com            val = 1 / val
15910036SAli.Saidi@ARM.com        return val
1609885Sstever@gmail.com    except ValueError:
1619885Sstever@gmail.com        pass
1627513SN/A
1639055Ssaidi@eecs.umich.edu    try:
1649885Sstever@gmail.com        val = toLatency(value)
16510036SAli.Saidi@ARM.com        return val
1667513SN/A    except ValueError:
1679885Sstever@gmail.com        pass
1687524SN/A
1699265SAli.Saidi@ARM.com    raise ValueError, "cannot convert '%s' to clock period" % value
1709265SAli.Saidi@ARM.com
1718893Ssaidi@eecs.umich.edu
1727513SN/Adef toNetworkBandwidth(value):
1737513SN/A    if not isinstance(value, str):
1748983Snate@binkert.org        raise TypeError, "wrong type '%s' should be str" % type(value)
1759265SAli.Saidi@ARM.com
1769885Sstever@gmail.com    if value.endswith('Tbps'):
1779885Sstever@gmail.com        return float(value[:-4]) * tera
17810036SAli.Saidi@ARM.com    elif value.endswith('Gbps'):
1798983Snate@binkert.org        return float(value[:-4]) * giga
1807513SN/A    elif value.endswith('Mbps'):
1817513SN/A        return float(value[:-4]) * mega
1827513SN/A    elif value.endswith('kbps'):
1837513SN/A        return float(value[:-4]) * kilo
1848893Ssaidi@eecs.umich.edu    elif value.endswith('bps'):
1857513SN/A        return float(value[:-3])
1869885Sstever@gmail.com    else:
1879885Sstever@gmail.com        return float(value)
18810036SAli.Saidi@ARM.com
1899885Sstever@gmail.com    raise ValueError, "cannot convert '%s' to network bandwidth" % value
1909885Sstever@gmail.com
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