2 * Copyright (c) 1996-1999 Barton P. Miller
4 * We provide the Paradyn Parallel Performance Tools (below
5 * described as Paradyn") on an AS IS basis, and do not warrant its
6 * validity or performance. We reserve the right to update, modify,
7 * or discontinue this software at any time. We shall have no
8 * obligation to supply such updates or modifications or any other
9 * form of support to you.
11 * This license is for research uses. For such uses, there is no
12 * charge. We define "research use" to mean you may freely use it
13 * inside your organization for whatever purposes you see fit. But you
14 * may not re-distribute Paradyn or parts of Paradyn, in any form
15 * source or binary (including derivatives), electronic or otherwise,
16 * to any other organization or entity without our permission.
18 * (for other uses, please contact us at paradyn@cs.wisc.edu)
20 * All warranties, including without limitation, any warranty of
21 * merchantability or fitness for a particular purpose, are hereby
24 * By your use of Paradyn, you understand and agree that we (or any
25 * other person or entity with proprietary rights in Paradyn) are
26 * under no obligation to provide either maintenance services,
27 * update services, notices of latent defects, or correction of
28 * defects for Paradyn.
30 * Even if advised of the possibility of such damages, under no
31 * circumstances shall we (or any other person or entity with
32 * proprietary rights in the software licensed hereunder) be liable
33 * to you or any third party for direct, indirect, or consequential
34 * damages of any character regardless of type of action, including,
35 * without limitation, loss of profits, loss of use, loss of good
36 * will, or computer failure or malfunction. You agree to indemnify
37 * us (and any other person or entity with proprietary rights in the
38 * software licensed hereunder) for any and all liability it may
39 * incur to third parties resulting from your use of Paradyn.
48 #include "common/h/Time.h"
49 #include "common/h/headers.h" // for isnan on NT
50 #include "pdutil/h/pdSample.h"
53 // -----------------------------------------------------------------------
54 // pdRate class definition ----------------------------------------
59 static const pdRate *_zero;
60 static const pdRate *_nan;
61 static const pdRate *nanHelp();
63 static const pdRate &Zero();
64 static const pdRate &NaN(); // returns the not-a-number element of pdRate
67 pdRate() { setNaN(); }
68 explicit pdRate(const double v) : data(v) { }
69 explicit pdRate(const pdSample s, const timeLength t,
70 const timeUnit tu = timeUnit::ns()) {
71 if(s.isNaN()) { setNaN(); }
73 data = static_cast<double>(s.getValue()) / t.getD(tu);
77 // General Assignment from like type
79 pdRate& operator=(const pdRate &o) {
86 bool isNaN() const { return (isnan(getValue()) != 0); }
87 void setNaN() { *this = NaN(); }
89 // user-defined operator casts are very bad, More Effective C++ ch.5
90 // so use non-operator cast function
91 double getValue() const { return data; }
93 void assign(const double v) { data = v; }
95 pdRate& operator+=(const pdRate &a) {
97 assign(data + a.getValue());
100 pdRate& operator-=(const pdRate &a) {
102 assign(data - a.getValue());
107 inline const pdRate &pdRate::Zero() {
108 if(_zero == NULL) _zero = new pdRate(0);
112 inline const pdRate &pdRate::NaN() {
113 if(_nan == NULL) { _nan = nanHelp(); }
117 // pdRate @ pdRate operators ----------------------------------
118 inline const pdRate operator+(const pdRate a, const pdRate b) {
119 assert(!a.isNaN() && !b.isNaN());
120 return pdRate(a.getValue() + b.getValue());
122 inline const pdRate operator-(const pdRate a, const pdRate b) {
123 assert(!a.isNaN() && !b.isNaN());
124 return pdRate(a.getValue() - b.getValue());
126 inline const pdRate operator*(const pdRate a, const pdRate b) {
127 assert(!a.isNaN() && !b.isNaN());
128 return pdRate(a.getValue() * b.getValue());
130 inline const pdRate operator/(const pdRate a, const pdRate b) {
131 assert(!a.isNaN() && !b.isNaN());
132 return pdRate(a.getValue() / b.getValue());
135 // assumes that the pdRate was formed with pdSample / timeLength.getD(ns())
136 // don't use if this isn't true
137 inline const pdSample operator*(const pdRate a, const timeLength b) {
138 assert(!a.isNaN() && b.isInitialized());
139 return pdSample(static_cast<int64_t>(a.getValue() * b.getD(timeUnit::ns())));
143 inline const pdRate operator*(const pdRate a, const double b) {
145 return pdRate(a.getValue() * b);
148 inline const pdRate operator*(const double a, const pdRate b) {
150 return pdRate(a * b.getValue());
153 inline const pdRate operator/(const pdRate a, const double b) {
155 return pdRate(a.getValue() / b);
158 inline const pdRate operator/(const double a, const pdRate b) {
160 return pdRate(a / b.getValue());
164 inline bool operator==(const pdRate a, const pdRate b) {
165 assert(!a.isNaN() && !b.isNaN());
166 return (a.getValue() == b.getValue());
168 inline bool operator!=(const pdRate a, const pdRate b) {
169 assert(!a.isNaN() && !b.isNaN());
170 return (a.getValue() != b.getValue());
172 inline bool operator>(const pdRate a, const pdRate b) {
173 assert(!a.isNaN() && !b.isNaN());
174 return (a.getValue() > b.getValue());
176 inline bool operator>=(const pdRate a, const pdRate b) {
177 assert(!a.isNaN() && !b.isNaN());
178 return (a.getValue() >= b.getValue());
180 inline bool operator<(const pdRate a, const pdRate b) {
181 assert(!a.isNaN() && !b.isNaN());
182 return (a.getValue() < b.getValue());
184 inline bool operator<=(const pdRate a, const pdRate b) {
185 assert(!a.isNaN() && !b.isNaN());
186 return (a.getValue() <= b.getValue());
189 ostream& operator<<(ostream&s, const pdRate &sm);