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.educlass Value:
301049Sbinkertn@umich.edu    def __init__(self, value, precision, percent = False):
311929Sbinkertn@umich.edu        self.value = float(value)
321049Sbinkertn@umich.edu        self.precision = precision
331049Sbinkertn@umich.edu        self.percent = percent
341049Sbinkertn@umich.edu    def __str__(self):
351049Sbinkertn@umich.edu        if isinstance(self.value, str):
361049Sbinkertn@umich.edu            if self.value.lower() == 'nan':
371049Sbinkertn@umich.edu                value = 'NaN'
381049Sbinkertn@umich.edu            if self.value.lower() == 'inf':
391049Sbinkertn@umich.edu                value = 'Inf'
401049Sbinkertn@umich.edu        else:
411049Sbinkertn@umich.edu            if self.precision >= 0:
421049Sbinkertn@umich.edu                format = "%%.%df" % self.precision
431049Sbinkertn@umich.edu            elif self.value == 0.0:
441049Sbinkertn@umich.edu                format = "%.0f"
451049Sbinkertn@umich.edu            elif self.value % 1.0 == 0.0:
461049Sbinkertn@umich.edu                format = "%.0f"
471049Sbinkertn@umich.edu            else:
481049Sbinkertn@umich.edu                format = "%f"
491049Sbinkertn@umich.edu            value = self.value
501049Sbinkertn@umich.edu            if self.percent:
511049Sbinkertn@umich.edu                value = value * 100.0
521049Sbinkertn@umich.edu            value = format % value
531049Sbinkertn@umich.edu
541049Sbinkertn@umich.edu        if self.percent:
551049Sbinkertn@umich.edu            value = value + "%"
561049Sbinkertn@umich.edu
571049Sbinkertn@umich.edu        return value
581049Sbinkertn@umich.edu
591049Sbinkertn@umich.educlass Print:
601049Sbinkertn@umich.edu    def __init__(self, **vals):
611049Sbinkertn@umich.edu        self.__dict__.update(vals)
621049Sbinkertn@umich.edu
631049Sbinkertn@umich.edu    def __str__(self):
641049Sbinkertn@umich.edu        value = Value(self.value, self.precision)
651049Sbinkertn@umich.edu        pdf = ''
661049Sbinkertn@umich.edu        cdf = ''
671049Sbinkertn@umich.edu        if self.__dict__.has_key('pdf'):
681049Sbinkertn@umich.edu            pdf = Value(self.pdf, 2, True)
691049Sbinkertn@umich.edu        if self.__dict__.has_key('cdf'):
701049Sbinkertn@umich.edu            cdf = Value(self.cdf, 2, True)
711049Sbinkertn@umich.edu
721049Sbinkertn@umich.edu        output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
731049Sbinkertn@umich.edu
741049Sbinkertn@umich.edu        if descriptions and self.__dict__.has_key('desc') and self.desc:
751049Sbinkertn@umich.edu            output = "%s # %s" % (output, self.desc)
761049Sbinkertn@umich.edu
771049Sbinkertn@umich.edu        return output
781049Sbinkertn@umich.edu
791049Sbinkertn@umich.edu    def doprint(self):
801049Sbinkertn@umich.edu        if display_all:
811049Sbinkertn@umich.edu            return True
821049Sbinkertn@umich.edu        if self.value == 0.0 and (self.flags & flags_nozero):
831049Sbinkertn@umich.edu            return False
841049Sbinkertn@umich.edu        if isinstance(self.value, str):
851049Sbinkertn@umich.edu            if self.value == 'NaN' and (self.flags & flags_nonan):
861049Sbinkertn@umich.edu                return False
871049Sbinkertn@umich.edu        return True
881049Sbinkertn@umich.edu
891049Sbinkertn@umich.edu    def display(self):
901049Sbinkertn@umich.edu        if self.doprint():
911049Sbinkertn@umich.edu            print self
921049Sbinkertn@umich.edu
931049Sbinkertn@umich.educlass VectorDisplay:
941049Sbinkertn@umich.edu    def display(self):
951929Sbinkertn@umich.edu        if not self.value:
961929Sbinkertn@umich.edu            return
971929Sbinkertn@umich.edu
981049Sbinkertn@umich.edu        p = Print()
991049Sbinkertn@umich.edu        p.flags = self.flags
1001049Sbinkertn@umich.edu        p.precision = self.precision
1011049Sbinkertn@umich.edu
1021929Sbinkertn@umich.edu        if not isinstance(self.value, (list, tuple)):
1031049Sbinkertn@umich.edu            p.name = self.name
1041049Sbinkertn@umich.edu            p.desc = self.desc
1051049Sbinkertn@umich.edu            p.value = self.value
1061049Sbinkertn@umich.edu            p.display()
1071929Sbinkertn@umich.edu            return
1081049Sbinkertn@umich.edu
1091929Sbinkertn@umich.edu        mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
1101929Sbinkertn@umich.edu        mycdf = 0.0
1111929Sbinkertn@umich.edu
1121929Sbinkertn@umich.edu        value = self.value
1131929Sbinkertn@umich.edu
1141929Sbinkertn@umich.edu        if display_all:
1151929Sbinkertn@umich.edu            subnames = [ '[%d]' % i for i in range(len(value)) ]
1161929Sbinkertn@umich.edu        else:
1171929Sbinkertn@umich.edu            subnames = [''] * len(value)
1181929Sbinkertn@umich.edu
1191929Sbinkertn@umich.edu        if self.__dict__.has_key('subnames'):
1201929Sbinkertn@umich.edu            for i,each in enumerate(self.subnames):
1211929Sbinkertn@umich.edu                if len(each) > 0:
1221929Sbinkertn@umich.edu                    subnames[i] = '.%s' % each
1231929Sbinkertn@umich.edu
1241929Sbinkertn@umich.edu        subdescs = [self.desc]*len(value)
1251929Sbinkertn@umich.edu        if self.__dict__.has_key('subdescs'):
1261929Sbinkertn@umich.edu            for i in xrange(min(len(value), len(self.subdescs))):
1271929Sbinkertn@umich.edu                subdescs[i] = self.subdescs[i]
1281929Sbinkertn@umich.edu
1291929Sbinkertn@umich.edu        for val,sname,sdesc in map(None, value, subnames, subdescs):
1301929Sbinkertn@umich.edu            if mytotal > 0.0:
1311929Sbinkertn@umich.edu                mypdf = float(val) / float(mytotal)
1321929Sbinkertn@umich.edu                mycdf += mypdf
1331929Sbinkertn@umich.edu                if (self.flags & flags_pdf):
1341929Sbinkertn@umich.edu                    p.pdf = mypdf
1351929Sbinkertn@umich.edu                    p.cdf = mycdf
1361929Sbinkertn@umich.edu
1371929Sbinkertn@umich.edu            if len(sname) == 0:
1381929Sbinkertn@umich.edu                continue
1391929Sbinkertn@umich.edu
1401929Sbinkertn@umich.edu            p.name = self.name + sname
1411929Sbinkertn@umich.edu            p.desc = sdesc
1421929Sbinkertn@umich.edu            p.value = val
1431929Sbinkertn@umich.edu            p.display()
1441929Sbinkertn@umich.edu
1451929Sbinkertn@umich.edu        if (self.flags & flags_total):
1461929Sbinkertn@umich.edu            if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
1471929Sbinkertn@umich.edu            if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
1481929Sbinkertn@umich.edu            p.name = self.name + '.total'
1491929Sbinkertn@umich.edu            p.desc = self.desc
1501929Sbinkertn@umich.edu            p.value = mytotal
1511929Sbinkertn@umich.edu            p.display()
152