11758Ssaidi@eecs.umich.edu# Copyright (c) 2003-2004 The Regents of The University of Michigan
21758Ssaidi@eecs.umich.edu# All rights reserved.
31758Ssaidi@eecs.umich.edu#
41758Ssaidi@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
51758Ssaidi@eecs.umich.edu# modification, are permitted provided that the following conditions are
61758Ssaidi@eecs.umich.edu# met: redistributions of source code must retain the above copyright
71758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
81758Ssaidi@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
91758Ssaidi@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
101758Ssaidi@eecs.umich.edu# documentation and/or other materials provided with the distribution;
111758Ssaidi@eecs.umich.edu# neither the name of the copyright holders nor the names of its
121758Ssaidi@eecs.umich.edu# contributors may be used to endorse or promote products derived from
131758Ssaidi@eecs.umich.edu# this software without specific prior written permission.
141758Ssaidi@eecs.umich.edu#
151758Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161758Ssaidi@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171758Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
181758Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
191758Ssaidi@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201758Ssaidi@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
211758Ssaidi@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221758Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231758Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241758Ssaidi@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
251758Ssaidi@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262665Ssaidi@eecs.umich.edu#
272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
281758Ssaidi@eecs.umich.edu
291049Sbinkertn@umich.eduall = False
301049Sbinkertn@umich.edudescriptions = False
311049Sbinkertn@umich.edu
321049Sbinkertn@umich.educlass Value:
331049Sbinkertn@umich.edu    def __init__(self, value, precision, percent = False):
341049Sbinkertn@umich.edu        self.value = value
351049Sbinkertn@umich.edu        self.precision = precision
361049Sbinkertn@umich.edu        self.percent = percent
371049Sbinkertn@umich.edu    def __str__(self):
381049Sbinkertn@umich.edu        if isinstance(self.value, str):
391049Sbinkertn@umich.edu            if self.value.lower() == 'nan':
401049Sbinkertn@umich.edu                value = 'NaN'
411049Sbinkertn@umich.edu            if self.value.lower() == 'inf':
421049Sbinkertn@umich.edu                value = 'Inf'
431049Sbinkertn@umich.edu        else:
441049Sbinkertn@umich.edu            if self.precision >= 0:
451049Sbinkertn@umich.edu                format = "%%.%df" % self.precision
461049Sbinkertn@umich.edu            elif self.value == 0.0:
471049Sbinkertn@umich.edu                format = "%.0f"
481049Sbinkertn@umich.edu            elif self.value % 1.0 == 0.0:
491049Sbinkertn@umich.edu                format = "%.0f"
501049Sbinkertn@umich.edu            else:
511049Sbinkertn@umich.edu                format = "%f"
521049Sbinkertn@umich.edu            value = self.value
531049Sbinkertn@umich.edu            if self.percent:
541049Sbinkertn@umich.edu                value = value * 100.0
551049Sbinkertn@umich.edu            value = format % value
561049Sbinkertn@umich.edu
571049Sbinkertn@umich.edu        if self.percent:
581049Sbinkertn@umich.edu            value = value + "%"
591049Sbinkertn@umich.edu
601049Sbinkertn@umich.edu        return value
611049Sbinkertn@umich.edu
621049Sbinkertn@umich.educlass Print:
631049Sbinkertn@umich.edu    def __init__(self, **vals):
641049Sbinkertn@umich.edu        self.__dict__.update(vals)
651049Sbinkertn@umich.edu
661049Sbinkertn@umich.edu    def __str__(self):
671049Sbinkertn@umich.edu        value = Value(self.value, self.precision)
681049Sbinkertn@umich.edu        pdf = ''
691049Sbinkertn@umich.edu        cdf = ''
701049Sbinkertn@umich.edu        if self.__dict__.has_key('pdf'):
711049Sbinkertn@umich.edu            pdf = Value(self.pdf, 2, True)
721049Sbinkertn@umich.edu        if self.__dict__.has_key('cdf'):
731049Sbinkertn@umich.edu            cdf = Value(self.cdf, 2, True)
741049Sbinkertn@umich.edu
751049Sbinkertn@umich.edu        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
761049Sbinkertn@umich.edu
771049Sbinkertn@umich.edu        if descriptions and self.__dict__.has_key('desc') and self.desc:
781049Sbinkertn@umich.edu            output = "%s # %s" % (output, self.desc)
791049Sbinkertn@umich.edu
801049Sbinkertn@umich.edu        return output
811049Sbinkertn@umich.edu
821049Sbinkertn@umich.edu    def doprint(self):
831049Sbinkertn@umich.edu        if display_all:
841049Sbinkertn@umich.edu            return True
851049Sbinkertn@umich.edu        if self.value == 0.0 and (self.flags & flags_nozero):
861049Sbinkertn@umich.edu            return False
871049Sbinkertn@umich.edu        if isinstance(self.value, str):
881049Sbinkertn@umich.edu            if self.value == 'NaN' and (self.flags & flags_nonan):
891049Sbinkertn@umich.edu                return False
901049Sbinkertn@umich.edu        return True
911049Sbinkertn@umich.edu
921049Sbinkertn@umich.edu    def display(self):
931049Sbinkertn@umich.edu        if self.doprint():
941049Sbinkertn@umich.edu            print self
951049Sbinkertn@umich.edu
961049Sbinkertn@umich.educlass VectorDisplay:
971049Sbinkertn@umich.edu    def display(self):
981049Sbinkertn@umich.edu        p = Print()
991049Sbinkertn@umich.edu        p.flags = self.flags
1001049Sbinkertn@umich.edu        p.precision = self.precision
1011049Sbinkertn@umich.edu
1021547Sbinkertn@umich.edu        if isinstance(self.value, (list, tuple)):
1031049Sbinkertn@umich.edu            if not len(self.value):
1041049Sbinkertn@umich.edu                return
1051049Sbinkertn@umich.edu
1061049Sbinkertn@umich.edu            mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
1071049Sbinkertn@umich.edu            mycdf = 0.0
1081049Sbinkertn@umich.edu
1091049Sbinkertn@umich.edu            value = self.value
1101049Sbinkertn@umich.edu
1111049Sbinkertn@umich.edu            if display_all:
1121049Sbinkertn@umich.edu                subnames = [ '[%d]' % i for i in range(len(value)) ]
1131049Sbinkertn@umich.edu            else:
1141049Sbinkertn@umich.edu                subnames = [''] * len(value)
1151049Sbinkertn@umich.edu
1161049Sbinkertn@umich.edu            if self.__dict__.has_key('subnames'):
1171049Sbinkertn@umich.edu                for i,each in enumerate(self.subnames):
1181049Sbinkertn@umich.edu                    if len(each) > 0:
1191049Sbinkertn@umich.edu                        subnames[i] = '.%s' % each
1201049Sbinkertn@umich.edu
1211049Sbinkertn@umich.edu            subdescs = [self.desc]*len(value)
1221049Sbinkertn@umich.edu            if self.__dict__.has_key('subdescs'):
1231049Sbinkertn@umich.edu                for i in xrange(min(len(value), len(self.subdescs))):
1241049Sbinkertn@umich.edu                    subdescs[i] = self.subdescs[i]
1251049Sbinkertn@umich.edu
1261049Sbinkertn@umich.edu            for val,sname,sdesc in map(None, value, subnames, subdescs):
1271049Sbinkertn@umich.edu                if mytotal > 0.0:
1281049Sbinkertn@umich.edu                    mypdf = float(val) / float(mytotal)
1291049Sbinkertn@umich.edu                    mycdf += mypdf
1301049Sbinkertn@umich.edu                    if (self.flags & flags_pdf):
1311049Sbinkertn@umich.edu                        p.pdf = mypdf
1321049Sbinkertn@umich.edu                        p.cdf = mycdf
1331049Sbinkertn@umich.edu
1341049Sbinkertn@umich.edu                if len(sname) == 0:
1351049Sbinkertn@umich.edu                    continue
1361049Sbinkertn@umich.edu
1371049Sbinkertn@umich.edu                p.name = self.name + sname
1381049Sbinkertn@umich.edu                p.desc = sdesc
1391049Sbinkertn@umich.edu                p.value = val
1401049Sbinkertn@umich.edu                p.display()
1411049Sbinkertn@umich.edu
1421049Sbinkertn@umich.edu            if (self.flags & flags_total):
1431049Sbinkertn@umich.edu                if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
1441049Sbinkertn@umich.edu                if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
1451049Sbinkertn@umich.edu                p.name = self.name + '.total'
1461049Sbinkertn@umich.edu                p.desc = self.desc
1471049Sbinkertn@umich.edu                p.value = mytotal
1481049Sbinkertn@umich.edu                p.display()
1491049Sbinkertn@umich.edu
1501049Sbinkertn@umich.edu        else:
1511049Sbinkertn@umich.edu            p.name = self.name
1521049Sbinkertn@umich.edu            p.desc = self.desc
1531049Sbinkertn@umich.edu            p.value = self.value
1541049Sbinkertn@umich.edu            p.display()
1551049Sbinkertn@umich.edu
156