convert.py revision 10427
11736SN/A# Copyright (c) 2005 The Regents of The University of Michigan 27778Sgblack@eecs.umich.edu# Copyright (c) 2010 Advanced Micro Devices, Inc. 31736SN/A# All rights reserved. 41736SN/A# 51736SN/A# Redistribution and use in source and binary forms, with or without 61736SN/A# modification, are permitted provided that the following conditions are 71736SN/A# met: redistributions of source code must retain the above copyright 81736SN/A# notice, this list of conditions and the following disclaimer; 91736SN/A# redistributions in binary form must reproduce the above copyright 101736SN/A# notice, this list of conditions and the following disclaimer in the 111736SN/A# documentation and/or other materials provided with the distribution; 121736SN/A# neither the name of the copyright holders nor the names of its 131736SN/A# contributors may be used to endorse or promote products derived from 141736SN/A# this software without specific prior written permission. 151736SN/A# 161736SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171736SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181736SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191736SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201736SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211736SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221736SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231736SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241736SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251736SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261736SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665SN/A# 282665SN/A# Authors: Nathan Binkert 297778Sgblack@eecs.umich.edu# Gabe Black 301736SN/A 311519SN/A# metric prefixes 321519SN/Aexa = 1.0e18 331519SN/Apeta = 1.0e15 341519SN/Atera = 1.0e12 351519SN/Agiga = 1.0e9 361519SN/Amega = 1.0e6 371519SN/Akilo = 1.0e3 381519SN/A 391519SN/Amilli = 1.0e-3 401519SN/Amicro = 1.0e-6 411519SN/Anano = 1.0e-9 421519SN/Apico = 1.0e-12 431519SN/Afemto = 1.0e-15 441519SN/Aatto = 1.0e-18 451519SN/A 461519SN/A# power of 2 prefixes 471519SN/Akibi = 1024 481519SN/Amebi = kibi * 1024 491519SN/Agibi = mebi * 1024 501519SN/Atebi = gibi * 1024 511519SN/Apebi = tebi * 1024 521519SN/Aexbi = pebi * 1024 531519SN/A 541519SN/A# memory size configuration stuff 551606SN/Adef toFloat(value): 561519SN/A if not isinstance(value, str): 571606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 581606SN/A 591606SN/A if value.endswith('Ei'): 601606SN/A return float(value[:-2]) * exbi 611519SN/A elif value.endswith('Pi'): 621606SN/A return float(value[:-2]) * pebi 631519SN/A elif value.endswith('Ti'): 641606SN/A return float(value[:-2]) * tebi 651519SN/A elif value.endswith('Gi'): 661606SN/A return float(value[:-2]) * gibi 671519SN/A elif value.endswith('Mi'): 681606SN/A return float(value[:-2]) * mebi 691519SN/A elif value.endswith('ki'): 701606SN/A return float(value[:-2]) * kibi 711519SN/A elif value.endswith('E'): 721606SN/A return float(value[:-1]) * exa 731519SN/A elif value.endswith('P'): 741606SN/A return float(value[:-1]) * peta 751519SN/A elif value.endswith('T'): 761606SN/A return float(value[:-1]) * tera 771519SN/A elif value.endswith('G'): 781606SN/A return float(value[:-1]) * giga 791519SN/A elif value.endswith('M'): 801606SN/A return float(value[:-1]) * mega 811519SN/A elif value.endswith('k'): 821606SN/A return float(value[:-1]) * kilo 831519SN/A elif value.endswith('m'): 841606SN/A return float(value[:-1]) * milli 851519SN/A elif value.endswith('u'): 861606SN/A return float(value[:-1]) * micro 871519SN/A elif value.endswith('n'): 881606SN/A return float(value[:-1]) * nano 891519SN/A elif value.endswith('p'): 901606SN/A return float(value[:-1]) * pico 911519SN/A elif value.endswith('f'): 921606SN/A return float(value[:-1]) * femto 931519SN/A else: 941606SN/A return float(value) 951606SN/A 961606SN/Adef toInteger(value): 971606SN/A value = toFloat(value) 981944SN/A result = long(value) 991606SN/A if value != result: 1001606SN/A raise ValueError, "cannot convert '%s' to integer" % value 1011519SN/A 1021606SN/A return result 1031606SN/A 1041858SN/A_bool_dict = { 1051858SN/A 'true' : True, 't' : True, 'yes' : True, 'y' : True, '1' : True, 1061858SN/A 'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False 1071858SN/A } 1081858SN/A 1091606SN/Adef toBool(value): 1101606SN/A if not isinstance(value, str): 1111606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 1121606SN/A 1131606SN/A value = value.lower() 1141858SN/A result = _bool_dict.get(value, None) 1151858SN/A if result == None: 1161858SN/A raise ValueError, "cannot convert '%s' to bool" % value 1171858SN/A return result 1181519SN/A 1191589SN/Adef toFrequency(value): 1201519SN/A if not isinstance(value, str): 1211606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 1221606SN/A 1231606SN/A if value.endswith('THz'): 1241606SN/A return float(value[:-3]) * tera 1251519SN/A elif value.endswith('GHz'): 1261606SN/A return float(value[:-3]) * giga 1271519SN/A elif value.endswith('MHz'): 1281606SN/A return float(value[:-3]) * mega 1291519SN/A elif value.endswith('kHz'): 1301606SN/A return float(value[:-3]) * kilo 1311519SN/A elif value.endswith('Hz'): 1321606SN/A return float(value[:-2]) 1331519SN/A 1341606SN/A raise ValueError, "cannot convert '%s' to frequency" % value 1351519SN/A 1361589SN/Adef toLatency(value): 1371519SN/A if not isinstance(value, str): 1381606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 1391606SN/A 1401606SN/A if value.endswith('ps'): 1411606SN/A return float(value[:-2]) * pico 1421519SN/A elif value.endswith('ns'): 1431606SN/A return float(value[:-2]) * nano 1441519SN/A elif value.endswith('us'): 1451606SN/A return float(value[:-2]) * micro 1461519SN/A elif value.endswith('ms'): 1471606SN/A return float(value[:-2]) * milli 1481519SN/A elif value.endswith('s'): 1491606SN/A return float(value[:-1]) 1501519SN/A 1511606SN/A raise ValueError, "cannot convert '%s' to latency" % value 1521606SN/A 1534167SN/Adef anyToLatency(value): 1541606SN/A """result is a clock period""" 1551606SN/A 1561606SN/A if not isinstance(value, str): 1571606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 1581606SN/A 1591606SN/A try: 1601606SN/A val = toFrequency(value) 1611606SN/A if val != 0: 1621606SN/A val = 1 / val 1631606SN/A return val 1641606SN/A except ValueError: 1651606SN/A pass 1661606SN/A 1671606SN/A try: 1681606SN/A val = toLatency(value) 1691606SN/A return val 1701606SN/A except ValueError: 1711606SN/A pass 1721606SN/A 1731606SN/A raise ValueError, "cannot convert '%s' to clock period" % value 1741606SN/A 1754167SN/Adef anyToFrequency(value): 1764167SN/A """result is a clock period""" 1774167SN/A 1784167SN/A if not isinstance(value, str): 1794167SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 1804167SN/A 1814167SN/A try: 1824167SN/A val = toFrequency(value) 1834167SN/A return val 1844167SN/A except ValueError: 1854167SN/A pass 1864167SN/A 1874167SN/A try: 1884167SN/A val = toLatency(value) 1894167SN/A if val != 0: 1904167SN/A val = 1 / val 1914167SN/A return val 1924167SN/A except ValueError: 1934167SN/A pass 1944167SN/A 1954167SN/A raise ValueError, "cannot convert '%s' to clock period" % value 1961519SN/A 1971589SN/Adef toNetworkBandwidth(value): 1981519SN/A if not isinstance(value, str): 1991606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 2001606SN/A 2011606SN/A if value.endswith('Tbps'): 2021627SN/A return float(value[:-4]) * tera 2031519SN/A elif value.endswith('Gbps'): 2041627SN/A return float(value[:-4]) * giga 2051519SN/A elif value.endswith('Mbps'): 2061627SN/A return float(value[:-4]) * mega 2071519SN/A elif value.endswith('kbps'): 2081627SN/A return float(value[:-4]) * kilo 2091519SN/A elif value.endswith('bps'): 2101627SN/A return float(value[:-3]) 2111519SN/A else: 2121606SN/A return float(value) 2131519SN/A 2141606SN/A raise ValueError, "cannot convert '%s' to network bandwidth" % value 2151519SN/A 2161589SN/Adef toMemoryBandwidth(value): 2171519SN/A if not isinstance(value, str): 2181606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 2191606SN/A 2201606SN/A if value.endswith('PB/s'): 2211606SN/A return float(value[:-4]) * pebi 2221519SN/A elif value.endswith('TB/s'): 2231606SN/A return float(value[:-4]) * tebi 2241519SN/A elif value.endswith('GB/s'): 2251606SN/A return float(value[:-4]) * gibi 2261519SN/A elif value.endswith('MB/s'): 2271606SN/A return float(value[:-4]) * mebi 2281519SN/A elif value.endswith('kB/s'): 2291606SN/A return float(value[:-4]) * kibi 2301519SN/A elif value.endswith('B/s'): 2311606SN/A return float(value[:-3]) 2321519SN/A 2331606SN/A raise ValueError, "cannot convert '%s' to memory bandwidth" % value 2341519SN/A 2351589SN/Adef toMemorySize(value): 2361519SN/A if not isinstance(value, str): 2371606SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 2381606SN/A 2391606SN/A if value.endswith('PB'): 2401944SN/A return long(value[:-2]) * pebi 2411519SN/A elif value.endswith('TB'): 2421944SN/A return long(value[:-2]) * tebi 2431519SN/A elif value.endswith('GB'): 2441944SN/A return long(value[:-2]) * gibi 2451519SN/A elif value.endswith('MB'): 2461944SN/A return long(value[:-2]) * mebi 2471519SN/A elif value.endswith('kB'): 2481944SN/A return long(value[:-2]) * kibi 2491519SN/A elif value.endswith('B'): 2501944SN/A return long(value[:-1]) 2511519SN/A 2521606SN/A raise ValueError, "cannot convert '%s' to memory size" % value 2537777Sgblack@eecs.umich.edu 2547777Sgblack@eecs.umich.edudef toIpAddress(value): 2557777Sgblack@eecs.umich.edu if not isinstance(value, str): 2567777Sgblack@eecs.umich.edu raise TypeError, "wrong type '%s' should be str" % type(value) 2577777Sgblack@eecs.umich.edu 2587777Sgblack@eecs.umich.edu bytes = value.split('.') 2597777Sgblack@eecs.umich.edu if len(bytes) != 4: 2607777Sgblack@eecs.umich.edu raise ValueError, 'invalid ip address %s' % value 2617777Sgblack@eecs.umich.edu 2627777Sgblack@eecs.umich.edu for byte in bytes: 2637777Sgblack@eecs.umich.edu if not 0 <= int(byte) <= 0xff: 2647777Sgblack@eecs.umich.edu raise ValueError, 'invalid ip address %s' % value 2657777Sgblack@eecs.umich.edu 2667777Sgblack@eecs.umich.edu return (int(bytes[0]) << 24) | (int(bytes[1]) << 16) | \ 2677777Sgblack@eecs.umich.edu (int(bytes[2]) << 8) | (int(bytes[3]) << 0) 2687777Sgblack@eecs.umich.edu 2697777Sgblack@eecs.umich.edudef toIpNetmask(value): 2707777Sgblack@eecs.umich.edu if not isinstance(value, str): 2717777Sgblack@eecs.umich.edu raise TypeError, "wrong type '%s' should be str" % type(value) 2727777Sgblack@eecs.umich.edu 2737777Sgblack@eecs.umich.edu (ip, netmask) = value.split('/') 2747777Sgblack@eecs.umich.edu ip = toIpAddress(ip) 2757777Sgblack@eecs.umich.edu netmaskParts = netmask.split('.') 2767777Sgblack@eecs.umich.edu if len(netmaskParts) == 1: 2777777Sgblack@eecs.umich.edu if not 0 <= int(netmask) <= 32: 2787777Sgblack@eecs.umich.edu raise ValueError, 'invalid netmask %s' % netmask 2797777Sgblack@eecs.umich.edu return (ip, int(netmask)) 2807777Sgblack@eecs.umich.edu elif len(netmaskParts) == 4: 2817777Sgblack@eecs.umich.edu netmaskNum = toIpAddress(netmask) 2827777Sgblack@eecs.umich.edu if netmaskNum == 0: 2837777Sgblack@eecs.umich.edu return (ip, 0) 2847777Sgblack@eecs.umich.edu testVal = 0 2857777Sgblack@eecs.umich.edu for i in range(32): 2867777Sgblack@eecs.umich.edu testVal |= (1 << (31 - i)) 2877777Sgblack@eecs.umich.edu if testVal == netmaskNum: 2887777Sgblack@eecs.umich.edu return (ip, i + 1) 2897777Sgblack@eecs.umich.edu raise ValueError, 'invalid netmask %s' % netmask 2907777Sgblack@eecs.umich.edu else: 2917777Sgblack@eecs.umich.edu raise ValueError, 'invalid netmask %s' % netmask 2927777Sgblack@eecs.umich.edu 2937777Sgblack@eecs.umich.edudef toIpWithPort(value): 2947777Sgblack@eecs.umich.edu if not isinstance(value, str): 2957777Sgblack@eecs.umich.edu raise TypeError, "wrong type '%s' should be str" % type(value) 2967777Sgblack@eecs.umich.edu 2977777Sgblack@eecs.umich.edu (ip, port) = value.split(':') 2987777Sgblack@eecs.umich.edu ip = toIpAddress(ip) 2997777Sgblack@eecs.umich.edu if not 0 <= int(port) <= 0xffff: 3007777Sgblack@eecs.umich.edu raise ValueError, 'invalid port %s' % port 3017777Sgblack@eecs.umich.edu return (ip, int(port)) 3029827Sakash.bagdia@arm.com 3039827Sakash.bagdia@arm.comdef toVoltage(value): 3049827Sakash.bagdia@arm.com if not isinstance(value, str): 3059827Sakash.bagdia@arm.com raise TypeError, "wrong type '%s' should be str" % type(value) 3069827Sakash.bagdia@arm.com 3079827Sakash.bagdia@arm.com if value.endswith('mV'): 3089827Sakash.bagdia@arm.com return float(value[:-2]) * milli 3099827Sakash.bagdia@arm.com elif value.endswith('V'): 3109827Sakash.bagdia@arm.com return float(value[:-1]) 3119827Sakash.bagdia@arm.com 3129827Sakash.bagdia@arm.com raise ValueError, "cannot convert '%s' to voltage" % value 3139827Sakash.bagdia@arm.com 31410427Sandreas.hansson@arm.comdef toCurrent(value): 31510427Sandreas.hansson@arm.com if not isinstance(value, str): 31610427Sandreas.hansson@arm.com raise TypeError, "wrong type '%s' should be str" % type(value) 31710427Sandreas.hansson@arm.com 31810427Sandreas.hansson@arm.com if value.endswith('A'): 31910427Sandreas.hansson@arm.com return toFloat(value[:-1]) 32010427Sandreas.hansson@arm.com 32110427Sandreas.hansson@arm.com raise ValueError, "cannot convert '%s' to current" % value 322