convert.py revision 1519
16157Snate@binkert.org# metric prefixes
26157Snate@binkert.orgexa  = 1.0e18
36157Snate@binkert.orgpeta = 1.0e15
46157Snate@binkert.orgtera = 1.0e12
56157Snate@binkert.orggiga = 1.0e9
66157Snate@binkert.orgmega = 1.0e6
76157Snate@binkert.orgkilo = 1.0e3
86157Snate@binkert.org
96157Snate@binkert.orgmilli = 1.0e-3
106157Snate@binkert.orgmicro = 1.0e-6
116157Snate@binkert.orgnano  = 1.0e-9
126157Snate@binkert.orgpico  = 1.0e-12
136157Snate@binkert.orgfemto = 1.0e-15
146157Snate@binkert.orgatto  = 1.0e-18
156157Snate@binkert.org
166157Snate@binkert.org# power of 2 prefixes
176157Snate@binkert.orgkibi = 1024
186157Snate@binkert.orgmebi = kibi * 1024
196157Snate@binkert.orggibi = mebi * 1024
206157Snate@binkert.orgtebi = gibi * 1024
216157Snate@binkert.orgpebi = tebi * 1024
226157Snate@binkert.orgexbi = pebi * 1024
236157Snate@binkert.org
246157Snate@binkert.org# memory size configuration stuff
256157Snate@binkert.orgdef to_integer(value):
266157Snate@binkert.org    if not isinstance(value, str):
276157Snate@binkert.org        result = int(value)
286157Snate@binkert.org    elif value.endswith('Ei'):
296157Snate@binkert.org        result = int(value[:-2]) * exbi
306157Snate@binkert.org    elif value.endswith('Pi'):
316157Snate@binkert.org        result = int(value[:-2]) * pebi
326157Snate@binkert.org    elif value.endswith('Ti'):
336157Snate@binkert.org        result = int(value[:-2]) * tebi
346157Snate@binkert.org    elif value.endswith('Gi'):
356157Snate@binkert.org        result = int(value[:-2]) * gibi
366157Snate@binkert.org    elif value.endswith('Mi'):
376157Snate@binkert.org        result = int(value[:-2]) * mebi
3812246Sgabeblack@google.com    elif value.endswith('ki'):
3912246Sgabeblack@google.com        result = int(value[:-2]) * kibi
406157Snate@binkert.org    elif value.endswith('E'):
416157Snate@binkert.org        result = int(value[:-1]) * exa
4210133Sandreas.hansson@arm.com    elif value.endswith('P'):
4310133Sandreas.hansson@arm.com        result = int(value[:-1]) * peta
4410133Sandreas.hansson@arm.com    elif value.endswith('T'):
4510133Sandreas.hansson@arm.com        result = int(value[:-1]) * tera
4610133Sandreas.hansson@arm.com    elif value.endswith('G'):
4710133Sandreas.hansson@arm.com        result = int(value[:-1]) * giga
4810133Sandreas.hansson@arm.com    elif value.endswith('M'):
4910133Sandreas.hansson@arm.com        result = int(value[:-1]) * mega
5010133Sandreas.hansson@arm.com    elif value.endswith('k'):
5110133Sandreas.hansson@arm.com        result = int(value[:-1]) * kilo
5210133Sandreas.hansson@arm.com    elif value.endswith('m'):
5310133Sandreas.hansson@arm.com        result = int(value[:-1]) * milli
5410133Sandreas.hansson@arm.com    elif value.endswith('u'):
5510133Sandreas.hansson@arm.com        result = int(value[:-1]) * micro
5610133Sandreas.hansson@arm.com    elif value.endswith('n'):
5710133Sandreas.hansson@arm.com        result = int(value[:-1]) * nano
5810133Sandreas.hansson@arm.com    elif value.endswith('p'):
5910133Sandreas.hansson@arm.com        result = int(value[:-1]) * pico
6011755Sandreas.hansson@arm.com    elif value.endswith('f'):
6110133Sandreas.hansson@arm.com        result = int(value[:-1]) * femto
6210133Sandreas.hansson@arm.com    else:
638492Snilay@cs.wisc.edu        result = int(value)
646168Snate@binkert.org
656168Snate@binkert.org    return result
666157Snate@binkert.org
676157Snate@binkert.orgdef to_bool(val):
686157Snate@binkert.org    t = type(val)
696157Snate@binkert.org    if t == bool:
706157Snate@binkert.org        return val
716157Snate@binkert.org
726157Snate@binkert.org    if t == None:
736157Snate@binkert.org        return False
746157Snate@binkert.org
756157Snate@binkert.org    if t == int or t == long:
766157Snate@binkert.org        return bool(val)
776157Snate@binkert.org
786157Snate@binkert.org    if t == str:
796157Snate@binkert.org        val = val.lower()
806157Snate@binkert.org        if val == "true" or val == "t" or val == "yes" or val == "y":
816157Snate@binkert.org            return True
826157Snate@binkert.org        elif val == "false" or val == "f" or val == "no" or val == "n":
836157Snate@binkert.org            return False
846157Snate@binkert.org
856157Snate@binkert.org    return to_integer(val) != 0
866157Snate@binkert.org
876157Snate@binkert.orgdef to_frequency(value):
886157Snate@binkert.org    if not isinstance(value, str):
896157Snate@binkert.org        result = float(value)
906157Snate@binkert.org    elif value.endswith('THz'):
916157Snate@binkert.org        result = float(value[:-3]) * tera
926157Snate@binkert.org    elif value.endswith('GHz'):
936157Snate@binkert.org        result = float(value[:-3]) * giga
946157Snate@binkert.org    elif value.endswith('MHz'):
956157Snate@binkert.org        result = float(value[:-3]) * mega
966157Snate@binkert.org    elif value.endswith('kHz'):
976157Snate@binkert.org        result = float(value[:-3]) * kilo
986157Snate@binkert.org    elif value.endswith('Hz'):
996157Snate@binkert.org        result = float(value[:-2])
1006157Snate@binkert.org    else:
1016157Snate@binkert.org        result = float(value)
1026157Snate@binkert.org
1036157Snate@binkert.org    return result
1046157Snate@binkert.org
1056157Snate@binkert.orgdef to_latency(value):
1066157Snate@binkert.org    if not isinstance(value, str):
1076157Snate@binkert.org        result = float(value)
1086157Snate@binkert.org    elif value.endswith('c'):
1098483Sgblack@eecs.umich.edu        result = float(value[:-1])
1108483Sgblack@eecs.umich.edu    elif value.endswith('ps'):
1116157Snate@binkert.org        result = float(value[:-2]) * pico
1126882SBrad.Beckmann@amd.com    elif value.endswith('ns'):
1136286Snate@binkert.org        result = float(value[:-2]) * nano
1146286Snate@binkert.org    elif value.endswith('us'):
1158092Snilay@cs.wisc.edu        result = float(value[:-2]) * micro
1166286Snate@binkert.org    elif value.endswith('ms'):
1176286Snate@binkert.org        result = float(value[:-2]) * milli
1186157Snate@binkert.org    elif value.endswith('s'):
11911208Sjoseph.gross@amd.com        result = float(value[:-1])
1206157Snate@binkert.org    else:
12111210SBrad.Beckmann@amd.com        result = float(value)
12210301Snilay@cs.wisc.edu
1236157Snate@binkert.org    return result;
1246157Snate@binkert.org
12511307Santhony.gutierrez@amd.comdef to_network_bandwidth(value):
12611122Snilay@cs.wisc.edu    if not isinstance(value, str):
12710301Snilay@cs.wisc.edu        result = float(value)
12810301Snilay@cs.wisc.edu    elif value.endswith('Tbps'):
12910301Snilay@cs.wisc.edu        result = float(value[:-3]) * tera
13010301Snilay@cs.wisc.edu    elif value.endswith('Gbps'):
13110301Snilay@cs.wisc.edu        result = float(value[:-3]) * giga
13211308Santhony.gutierrez@amd.com    elif value.endswith('Mbps'):
13310301Snilay@cs.wisc.edu        result = float(value[:-3]) * mega
13410301Snilay@cs.wisc.edu    elif value.endswith('kbps'):
13511308Santhony.gutierrez@amd.com        result = float(value[:-3]) * kilo
13611308Santhony.gutierrez@amd.com    elif value.endswith('bps'):
13711308Santhony.gutierrez@amd.com        result = float(value[:-2])
13811308Santhony.gutierrez@amd.com    else:
13911308Santhony.gutierrez@amd.com        result = float(value)
14011308Santhony.gutierrez@amd.com
14111308Santhony.gutierrez@amd.com    return result
14211308Santhony.gutierrez@amd.com
14311308Santhony.gutierrez@amd.comdef to_memory_bandwidth(value):
14411308Santhony.gutierrez@amd.com    if not isinstance(value, str):
145        result = int(value)
146    elif value.endswith('PB/s'):
147        result = int(value[:-4]) * pebi
148    elif value.endswith('TB/s'):
149        result = int(value[:-4]) * tebi
150    elif value.endswith('GB/s'):
151        result = int(value[:-4]) * gibi
152    elif value.endswith('MB/s'):
153        result = int(value[:-4]) * mebi
154    elif value.endswith('kB/s'):
155        result = int(value[:-4]) * kibi
156    elif value.endswith('B/s'):
157        result = int(value[:-3])
158    else:
159        result = int(value)
160
161    return result
162
163def to_memory_size(value):
164    if not isinstance(value, str):
165        result = int(value)
166    elif value.endswith('PB'):
167        result = int(value[:-2]) * pebi
168    elif value.endswith('TB'):
169        result = int(value[:-2]) * tebi
170    elif value.endswith('GB'):
171        result = int(value[:-2]) * gibi
172    elif value.endswith('MB'):
173        result = int(value[:-2]) * mebi
174    elif value.endswith('kB'):
175        result = int(value[:-2]) * kibi
176    elif value.endswith('B'):
177        result = int(value[:-1])
178    else:
179        result = int(value)
180
181    return result
182