convert.py revision 2665
172SN/A# Copyright (c) 2005 The Regents of The University of Michigan
21762SN/A# All rights reserved.
372SN/A#
472SN/A# Redistribution and use in source and binary forms, with or without
572SN/A# modification, are permitted provided that the following conditions are
672SN/A# met: redistributions of source code must retain the above copyright
772SN/A# notice, this list of conditions and the following disclaimer;
872SN/A# redistributions in binary form must reproduce the above copyright
972SN/A# notice, this list of conditions and the following disclaimer in the
1072SN/A# documentation and/or other materials provided with the distribution;
1172SN/A# neither the name of the copyright holders nor the names of its
1272SN/A# contributors may be used to endorse or promote products derived from
1372SN/A# this software without specific prior written permission.
1472SN/A#
1572SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1672SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1772SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1872SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1972SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2072SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2172SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2272SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2372SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2472SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2572SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2672SN/A#
272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
282665Ssaidi@eecs.umich.edu
2972SN/A# metric prefixes
3072SN/Aexa  = 1.0e18
312SN/Apeta = 1.0e15
322SN/Atera = 1.0e12
332SN/Agiga = 1.0e9
342SN/Amega = 1.0e6
352SN/Akilo = 1.0e3
362SN/A
372SN/Amilli = 1.0e-3
382SN/Amicro = 1.0e-6
392SN/Anano  = 1.0e-9
402SN/Apico  = 1.0e-12
412SN/Afemto = 1.0e-15
422SN/Aatto  = 1.0e-18
432SN/A
442SN/A# power of 2 prefixes
452SN/Akibi = 1024
462SN/Amebi = kibi * 1024
472SN/Agibi = mebi * 1024
482SN/Atebi = gibi * 1024
492SN/Apebi = tebi * 1024
502SN/Aexbi = pebi * 1024
512SN/A
522SN/A# memory size configuration stuff
532SN/Adef toFloat(value):
542SN/A    if not isinstance(value, str):
552SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
562SN/A
572SN/A    if value.endswith('Ei'):
582SN/A        return float(value[:-2]) * exbi
592SN/A    elif value.endswith('Pi'):
602SN/A        return float(value[:-2]) * pebi
612SN/A    elif value.endswith('Ti'):
622SN/A        return float(value[:-2]) * tebi
632SN/A    elif value.endswith('Gi'):
642SN/A        return float(value[:-2]) * gibi
652SN/A    elif value.endswith('Mi'):
662SN/A        return float(value[:-2]) * mebi
672SN/A    elif value.endswith('ki'):
682SN/A        return float(value[:-2]) * kibi
692SN/A    elif value.endswith('E'):
702SN/A        return float(value[:-1]) * exa
712SN/A    elif value.endswith('P'):
722SN/A        return float(value[:-1]) * peta
732SN/A    elif value.endswith('T'):
742SN/A        return float(value[:-1]) * tera
752SN/A    elif value.endswith('G'):
762SN/A        return float(value[:-1]) * giga
772SN/A    elif value.endswith('M'):
782SN/A        return float(value[:-1]) * mega
792SN/A    elif value.endswith('k'):
802SN/A        return float(value[:-1]) * kilo
812SN/A    elif value.endswith('m'):
822SN/A        return float(value[:-1]) * milli
832SN/A    elif value.endswith('u'):
842SN/A        return float(value[:-1]) * micro
852SN/A    elif value.endswith('n'):
862SN/A        return float(value[:-1]) * nano
872SN/A    elif value.endswith('p'):
882SN/A        return float(value[:-1]) * pico
892SN/A    elif value.endswith('f'):
902SN/A        return float(value[:-1]) * femto
912SN/A    else:
922SN/A        return float(value)
932SN/A
942SN/Adef toInteger(value):
952SN/A    value = toFloat(value)
962SN/A    result = long(value)
972SN/A    if value != result:
982SN/A        raise ValueError, "cannot convert '%s' to integer" % value
992SN/A
1002SN/A    return result
1012SN/A
1022SN/A_bool_dict = {
1032SN/A    'true' : True,   't' : True,  'yes' : True, 'y' : True,  '1' : True,
1042SN/A    'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False
1052SN/A    }
1062SN/A
1072SN/Adef toBool(value):
1082SN/A    if not isinstance(value, str):
1092SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1102SN/A
1112SN/A    value = value.lower()
1122SN/A    result = _bool_dict.get(value, None)
1132SN/A    if result == None:
1142SN/A        raise ValueError, "cannot convert '%s' to bool" % value
1152SN/A    return result
1162SN/A
1172SN/Adef toFrequency(value):
1182SN/A    if not isinstance(value, str):
1192SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1202SN/A
1211717SN/A    if value.endswith('THz'):
1222SN/A        return float(value[:-3]) * tera
1232SN/A    elif value.endswith('GHz'):
1242521SN/A        return float(value[:-3]) * giga
12556SN/A    elif value.endswith('MHz'):
12656SN/A        return float(value[:-3]) * mega
12756SN/A    elif value.endswith('kHz'):
12856SN/A        return float(value[:-3]) * kilo
1292521SN/A    elif value.endswith('Hz'):
1302680Sktlim@umich.edu        return float(value[:-2])
1311717SN/A
1322521SN/A    raise ValueError, "cannot convert '%s' to frequency" % value
1332521SN/A
1341717SN/Adef toLatency(value):
1352SN/A    if not isinstance(value, str):
1362SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1372107SN/A
1382SN/A    if value.endswith('ps'):
1392009SN/A        return float(value[:-2]) * pico
1403536Sgblack@eecs.umich.edu    elif value.endswith('ns'):
1412SN/A        return float(value[:-2]) * nano
1422SN/A    elif value.endswith('us'):
1432SN/A        return float(value[:-2]) * micro
1442SN/A    elif value.endswith('ms'):
1453536Sgblack@eecs.umich.edu        return float(value[:-2]) * milli
1461910SN/A    elif value.endswith('s'):
1473536Sgblack@eecs.umich.edu        return float(value[:-1])
1481910SN/A
1491910SN/A    raise ValueError, "cannot convert '%s' to latency" % value
1501910SN/A
1513536Sgblack@eecs.umich.edudef toClockPeriod(value):
1521910SN/A    """result is a clock period"""
1532SN/A
1542SN/A    if not isinstance(value, str):
1552SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1562SN/A
1572SN/A    try:
1582SN/A        val = toFrequency(value)
1592SN/A        if val != 0:
1602SN/A            val = 1 / val
1612SN/A        return val
1622SN/A    except ValueError:
1632SN/A        pass
1642SN/A
1652SN/A    try:
1662SN/A        val = toLatency(value)
1672SN/A        return val
1682SN/A    except ValueError:
1692SN/A        pass
1702SN/A
1713536Sgblack@eecs.umich.edu    raise ValueError, "cannot convert '%s' to clock period" % value
1722SN/A
1731910SN/A
1741910SN/Adef toNetworkBandwidth(value):
1751910SN/A    if not isinstance(value, str):
1761910SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1772SN/A
1782SN/A    if value.endswith('Tbps'):
1792SN/A        return float(value[:-4]) * tera
1802SN/A    elif value.endswith('Gbps'):
1812SN/A        return float(value[:-4]) * giga
1822SN/A    elif value.endswith('Mbps'):
1832SN/A        return float(value[:-4]) * mega
184507SN/A    elif value.endswith('kbps'):
185507SN/A        return float(value[:-4]) * kilo
186507SN/A    elif value.endswith('bps'):
187507SN/A        return float(value[:-3])
188507SN/A    else:
189507SN/A        return float(value)
1902SN/A
1912SN/A    raise ValueError, "cannot convert '%s' to network bandwidth" % value
1922SN/A
1932SN/Adef toMemoryBandwidth(value):
194507SN/A    if not isinstance(value, str):
1952SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
1962SN/A
1972SN/A    if value.endswith('PB/s'):
1982SN/A        return float(value[:-4]) * pebi
1992SN/A    elif value.endswith('TB/s'):
2001910SN/A        return float(value[:-4]) * tebi
2012009SN/A    elif value.endswith('GB/s'):
2021910SN/A        return float(value[:-4]) * gibi
2031910SN/A    elif value.endswith('MB/s'):
2041910SN/A        return float(value[:-4]) * mebi
2051910SN/A    elif value.endswith('kB/s'):
2062009SN/A        return float(value[:-4]) * kibi
2071910SN/A    elif value.endswith('B/s'):
2081910SN/A        return float(value[:-3])
2091910SN/A
2101910SN/A    raise ValueError, "cannot convert '%s' to memory bandwidth" % value
2111910SN/A
2121910SN/Adef toMemorySize(value):
2132SN/A    if not isinstance(value, str):
2142SN/A        raise TypeError, "wrong type '%s' should be str" % type(value)
2152SN/A
2162SN/A    if value.endswith('PB'):
2172SN/A        return long(value[:-2]) * pebi
2182SN/A    elif value.endswith('TB'):
219507SN/A        return long(value[:-2]) * tebi
2202SN/A    elif value.endswith('GB'):
2212SN/A        return long(value[:-2]) * gibi
2222SN/A    elif value.endswith('MB'):
2232SN/A        return long(value[:-2]) * mebi
2242SN/A    elif value.endswith('kB'):
2252SN/A        return long(value[:-2]) * kibi
2262SN/A    elif value.endswith('B'):
2272SN/A        return long(value[:-1])
2282SN/A
2292SN/A    raise ValueError, "cannot convert '%s' to memory size" % value
2302SN/A