convert.py revision 1606
17760SGiacomo.Gabrielli@arm.com# metric prefixes 27760SGiacomo.Gabrielli@arm.comexa = 1.0e18 37760SGiacomo.Gabrielli@arm.competa = 1.0e15 47760SGiacomo.Gabrielli@arm.comtera = 1.0e12 57760SGiacomo.Gabrielli@arm.comgiga = 1.0e9 67760SGiacomo.Gabrielli@arm.commega = 1.0e6 77760SGiacomo.Gabrielli@arm.comkilo = 1.0e3 87760SGiacomo.Gabrielli@arm.com 97760SGiacomo.Gabrielli@arm.commilli = 1.0e-3 107760SGiacomo.Gabrielli@arm.commicro = 1.0e-6 117760SGiacomo.Gabrielli@arm.comnano = 1.0e-9 127760SGiacomo.Gabrielli@arm.compico = 1.0e-12 134486Sbinkertn@umich.edufemto = 1.0e-15 144486Sbinkertn@umich.eduatto = 1.0e-18 154486Sbinkertn@umich.edu 164486Sbinkertn@umich.edu# power of 2 prefixes 174486Sbinkertn@umich.edukibi = 1024 184486Sbinkertn@umich.edumebi = kibi * 1024 194486Sbinkertn@umich.edugibi = mebi * 1024 204486Sbinkertn@umich.edutebi = gibi * 1024 214486Sbinkertn@umich.edupebi = tebi * 1024 224486Sbinkertn@umich.eduexbi = pebi * 1024 234486Sbinkertn@umich.edu 244486Sbinkertn@umich.edu# memory size configuration stuff 254486Sbinkertn@umich.edudef toFloat(value): 264486Sbinkertn@umich.edu if not isinstance(value, str): 274486Sbinkertn@umich.edu raise TypeError, "wrong type '%s' should be str" % type(value) 284486Sbinkertn@umich.edu 294486Sbinkertn@umich.edu if value.endswith('Ei'): 304486Sbinkertn@umich.edu return float(value[:-2]) * exbi 314486Sbinkertn@umich.edu elif value.endswith('Pi'): 324486Sbinkertn@umich.edu return float(value[:-2]) * pebi 334486Sbinkertn@umich.edu elif value.endswith('Ti'): 344486Sbinkertn@umich.edu return float(value[:-2]) * tebi 354486Sbinkertn@umich.edu elif value.endswith('Gi'): 364486Sbinkertn@umich.edu return float(value[:-2]) * gibi 374486Sbinkertn@umich.edu elif value.endswith('Mi'): 384486Sbinkertn@umich.edu return float(value[:-2]) * mebi 394486Sbinkertn@umich.edu elif value.endswith('ki'): 404486Sbinkertn@umich.edu return float(value[:-2]) * kibi 413102SN/A elif value.endswith('E'): 423102SN/A return float(value[:-1]) * exa 432736SN/A elif value.endswith('P'): 444556Sbinkertn@umich.edu return float(value[:-1]) * peta 454556Sbinkertn@umich.edu elif value.endswith('T'): 462736SN/A return float(value[:-1]) * tera 477760SGiacomo.Gabrielli@arm.com elif value.endswith('G'): 487760SGiacomo.Gabrielli@arm.com return float(value[:-1]) * giga 497760SGiacomo.Gabrielli@arm.com elif value.endswith('M'): 507760SGiacomo.Gabrielli@arm.com return float(value[:-1]) * mega 517760SGiacomo.Gabrielli@arm.com elif value.endswith('k'): 522736SN/A return float(value[:-1]) * kilo 532736SN/A elif value.endswith('m'): 542736SN/A return float(value[:-1]) * milli 552736SN/A elif value.endswith('u'): 569338SAndreas.Sandberg@arm.com return float(value[:-1]) * micro 574556Sbinkertn@umich.edu elif value.endswith('n'): 589184Sandreas.hansson@arm.com return float(value[:-1]) * nano 5910807Snilay@cs.wisc.edu elif value.endswith('p'): 6010807Snilay@cs.wisc.edu return float(value[:-1]) * pico 612736SN/A elif value.endswith('f'): 622736SN/A return float(value[:-1]) * femto 632736SN/A else: 649338SAndreas.Sandberg@arm.com return float(value) 652736SN/A 662736SN/Adef toLong(value): 67 value = toFloat(value) 68 result = int(value) 69 if value != result: 70 raise ValueError, "cannot convert '%s' to long" % value 71 72 return result 73 74def toInteger(value): 75 value = toFloat(value) 76 result = int(value) 77 if value != result: 78 raise ValueError, "cannot convert '%s' to integer" % value 79 80 return result 81 82def toBool(value): 83 if not isinstance(value, str): 84 raise TypeError, "wrong type '%s' should be str" % type(value) 85 86 value = value.lower() 87 if value == "true" or value == "t" or value == "yes" or value == "y": 88 return True 89 elif value == "false" or value == "f" or value == "no" or value == "n": 90 return False 91 92 raise ValueError, "cannot convert '%s' to bool" % value 93 94def toFrequency(value): 95 if not isinstance(value, str): 96 raise TypeError, "wrong type '%s' should be str" % type(value) 97 98 if value.endswith('THz'): 99 return float(value[:-3]) * tera 100 elif value.endswith('GHz'): 101 return float(value[:-3]) * giga 102 elif value.endswith('MHz'): 103 return float(value[:-3]) * mega 104 elif value.endswith('kHz'): 105 return float(value[:-3]) * kilo 106 elif value.endswith('Hz'): 107 return float(value[:-2]) 108 109 raise ValueError, "cannot convert '%s' to frequency" % value 110 111def toLatency(value): 112 if not isinstance(value, str): 113 raise TypeError, "wrong type '%s' should be str" % type(value) 114 115 if value.endswith('ps'): 116 return float(value[:-2]) * pico 117 elif value.endswith('ns'): 118 return float(value[:-2]) * nano 119 elif value.endswith('us'): 120 return float(value[:-2]) * micro 121 elif value.endswith('ms'): 122 return float(value[:-2]) * milli 123 elif value.endswith('s'): 124 return float(value[:-1]) 125 126 raise ValueError, "cannot convert '%s' to latency" % value 127 128def toClockPeriod(value): 129 """result is a clock period""" 130 131 if not isinstance(value, str): 132 raise TypeError, "wrong type '%s' should be str" % type(value) 133 134 try: 135 val = toFrequency(value) 136 if val != 0: 137 val = 1 / val 138 return val 139 except ValueError: 140 pass 141 142 try: 143 val = toLatency(value) 144 return val 145 except ValueError: 146 pass 147 148 raise ValueError, "cannot convert '%s' to clock period" % value 149 150 151def toNetworkBandwidth(value): 152 if not isinstance(value, str): 153 raise TypeError, "wrong type '%s' should be str" % type(value) 154 155 if value.endswith('Tbps'): 156 return float(value[:-3]) * tera 157 elif value.endswith('Gbps'): 158 return float(value[:-3]) * giga 159 elif value.endswith('Mbps'): 160 return float(value[:-3]) * mega 161 elif value.endswith('kbps'): 162 return float(value[:-3]) * kilo 163 elif value.endswith('bps'): 164 return float(value[:-2]) 165 else: 166 return float(value) 167 168 raise ValueError, "cannot convert '%s' to network bandwidth" % value 169 170def toMemoryBandwidth(value): 171 if not isinstance(value, str): 172 raise TypeError, "wrong type '%s' should be str" % type(value) 173 174 if value.endswith('PB/s'): 175 return float(value[:-4]) * pebi 176 elif value.endswith('TB/s'): 177 return float(value[:-4]) * tebi 178 elif value.endswith('GB/s'): 179 return float(value[:-4]) * gibi 180 elif value.endswith('MB/s'): 181 return float(value[:-4]) * mebi 182 elif value.endswith('kB/s'): 183 return float(value[:-4]) * kibi 184 elif value.endswith('B/s'): 185 return float(value[:-3]) 186 187 raise ValueError, "cannot convert '%s' to memory bandwidth" % value 188 189def toMemorySize(value): 190 if not isinstance(value, str): 191 raise TypeError, "wrong type '%s' should be str" % type(value) 192 193 if value.endswith('PB'): 194 return float(value[:-2]) * pebi 195 elif value.endswith('TB'): 196 return float(value[:-2]) * tebi 197 elif value.endswith('GB'): 198 return float(value[:-2]) * gibi 199 elif value.endswith('MB'): 200 return float(value[:-2]) * mebi 201 elif value.endswith('kB'): 202 return float(value[:-2]) * kibi 203 elif value.endswith('B'): 204 return float(value[:-1]) 205 206 raise ValueError, "cannot convert '%s' to memory size" % value 207