Tue, 05 Apr 2011 14:12:31 -0700
7010070: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - second pass
Summary: Update the copyright to be 2010 on all changed files in OpenJDK
Reviewed-by: ohair
ysr@777 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
ysr@777 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ysr@777 | 4 | * |
ysr@777 | 5 | * This code is free software; you can redistribute it and/or modify it |
ysr@777 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ysr@777 | 7 | * published by the Free Software Foundation. |
ysr@777 | 8 | * |
ysr@777 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ysr@777 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ysr@777 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ysr@777 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ysr@777 | 13 | * accompanied this code). |
ysr@777 | 14 | * |
ysr@777 | 15 | * You should have received a copy of the GNU General Public License version |
ysr@777 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ysr@777 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ysr@777 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
ysr@777 | 22 | * |
ysr@777 | 23 | */ |
ysr@777 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 27 | #include "utilities/debug.hpp" |
stefank@2314 | 28 | #include "utilities/globalDefinitions.hpp" |
stefank@2314 | 29 | #include "utilities/numberSeq.hpp" |
ysr@777 | 30 | |
ysr@777 | 31 | AbsSeq::AbsSeq(double alpha) : |
ysr@777 | 32 | _num(0), _sum(0.0), _sum_of_squares(0.0), |
ysr@777 | 33 | _davg(0.0), _dvariance(0.0), _alpha(alpha) { |
ysr@777 | 34 | } |
ysr@777 | 35 | |
ysr@777 | 36 | void AbsSeq::add(double val) { |
ysr@777 | 37 | if (_num == 0) { |
ysr@777 | 38 | // if the sequence is empty, the davg is the same as the value |
ysr@777 | 39 | _davg = val; |
ysr@777 | 40 | // and the variance is 0 |
ysr@777 | 41 | _dvariance = 0.0; |
ysr@777 | 42 | } else { |
ysr@777 | 43 | // otherwise, calculate both |
ysr@777 | 44 | _davg = (1.0 - _alpha) * val + _alpha * _davg; |
ysr@777 | 45 | double diff = val - _davg; |
ysr@777 | 46 | _dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance; |
ysr@777 | 47 | } |
ysr@777 | 48 | } |
ysr@777 | 49 | |
ysr@777 | 50 | double AbsSeq::avg() const { |
ysr@777 | 51 | if (_num == 0) |
ysr@777 | 52 | return 0.0; |
ysr@777 | 53 | else |
ysr@777 | 54 | return _sum / total(); |
ysr@777 | 55 | } |
ysr@777 | 56 | |
ysr@777 | 57 | double AbsSeq::variance() const { |
ysr@777 | 58 | if (_num <= 1) |
ysr@777 | 59 | return 0.0; |
ysr@777 | 60 | |
ysr@777 | 61 | double x_bar = avg(); |
ysr@777 | 62 | double result = _sum_of_squares / total() - x_bar * x_bar; |
ysr@777 | 63 | if (result < 0.0) { |
ysr@777 | 64 | // due to loss-of-precision errors, the variance might be negative |
ysr@777 | 65 | // by a small bit |
ysr@777 | 66 | |
ysr@777 | 67 | // guarantee(-0.1 < result && result < 0.0, |
ysr@777 | 68 | // "if variance is negative, it should be very small"); |
ysr@777 | 69 | result = 0.0; |
ysr@777 | 70 | } |
ysr@777 | 71 | return result; |
ysr@777 | 72 | } |
ysr@777 | 73 | |
ysr@777 | 74 | double AbsSeq::sd() const { |
ysr@777 | 75 | double var = variance(); |
ysr@777 | 76 | guarantee( var >= 0.0, "variance should not be negative" ); |
ysr@777 | 77 | return sqrt(var); |
ysr@777 | 78 | } |
ysr@777 | 79 | |
ysr@777 | 80 | double AbsSeq::davg() const { |
ysr@777 | 81 | return _davg; |
ysr@777 | 82 | } |
ysr@777 | 83 | |
ysr@777 | 84 | double AbsSeq::dvariance() const { |
ysr@777 | 85 | if (_num <= 1) |
ysr@777 | 86 | return 0.0; |
ysr@777 | 87 | |
ysr@777 | 88 | double result = _dvariance; |
ysr@777 | 89 | if (result < 0.0) { |
ysr@777 | 90 | // due to loss-of-precision errors, the variance might be negative |
ysr@777 | 91 | // by a small bit |
ysr@777 | 92 | |
ysr@777 | 93 | guarantee(-0.1 < result && result < 0.0, |
ysr@777 | 94 | "if variance is negative, it should be very small"); |
ysr@777 | 95 | result = 0.0; |
ysr@777 | 96 | } |
ysr@777 | 97 | return result; |
ysr@777 | 98 | } |
ysr@777 | 99 | |
ysr@777 | 100 | double AbsSeq::dsd() const { |
ysr@777 | 101 | double var = dvariance(); |
ysr@777 | 102 | guarantee( var >= 0.0, "variance should not be negative" ); |
ysr@777 | 103 | return sqrt(var); |
ysr@777 | 104 | } |
ysr@777 | 105 | |
ysr@777 | 106 | NumberSeq::NumberSeq(double alpha) : |
ysr@777 | 107 | AbsSeq(alpha), _maximum(0.0), _last(0.0) { |
ysr@777 | 108 | } |
ysr@777 | 109 | |
ysr@777 | 110 | bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) { |
ysr@777 | 111 | for (int i = 0; i < n; ++i) { |
ysr@777 | 112 | if (parts[i] != NULL && total->num() != parts[i]->num()) |
ysr@777 | 113 | return false; |
ysr@777 | 114 | } |
ysr@777 | 115 | return true; |
ysr@777 | 116 | } |
ysr@777 | 117 | |
ysr@777 | 118 | NumberSeq::NumberSeq(NumberSeq *total, int n, NumberSeq **parts) { |
ysr@777 | 119 | guarantee(check_nums(total, n, parts), "all seq lengths should match"); |
ysr@777 | 120 | double sum = total->sum(); |
ysr@777 | 121 | for (int i = 0; i < n; ++i) { |
ysr@777 | 122 | if (parts[i] != NULL) |
ysr@777 | 123 | sum -= parts[i]->sum(); |
ysr@777 | 124 | } |
ysr@777 | 125 | |
ysr@777 | 126 | _num = total->num(); |
ysr@777 | 127 | _sum = sum; |
ysr@777 | 128 | |
ysr@777 | 129 | // we do not calculate these... |
ysr@777 | 130 | _sum_of_squares = -1.0; |
ysr@777 | 131 | _maximum = -1.0; |
ysr@777 | 132 | _davg = -1.0; |
ysr@777 | 133 | _dvariance = -1.0; |
ysr@777 | 134 | } |
ysr@777 | 135 | |
ysr@777 | 136 | void NumberSeq::add(double val) { |
ysr@777 | 137 | AbsSeq::add(val); |
ysr@777 | 138 | |
ysr@777 | 139 | _last = val; |
ysr@777 | 140 | if (_num == 0) { |
ysr@777 | 141 | _maximum = val; |
ysr@777 | 142 | } else { |
ysr@777 | 143 | if (val > _maximum) |
ysr@777 | 144 | _maximum = val; |
ysr@777 | 145 | } |
ysr@777 | 146 | _sum += val; |
ysr@777 | 147 | _sum_of_squares += val * val; |
ysr@777 | 148 | ++_num; |
ysr@777 | 149 | } |
ysr@777 | 150 | |
ysr@777 | 151 | |
ysr@777 | 152 | TruncatedSeq::TruncatedSeq(int length, double alpha): |
ysr@777 | 153 | AbsSeq(alpha), _length(length), _next(0) { |
ysr@777 | 154 | _sequence = NEW_C_HEAP_ARRAY(double, _length); |
ysr@777 | 155 | for (int i = 0; i < _length; ++i) |
ysr@777 | 156 | _sequence[i] = 0.0; |
ysr@777 | 157 | } |
ysr@777 | 158 | |
ysr@777 | 159 | void TruncatedSeq::add(double val) { |
ysr@777 | 160 | AbsSeq::add(val); |
ysr@777 | 161 | |
ysr@777 | 162 | // get the oldest value in the sequence... |
ysr@777 | 163 | double old_val = _sequence[_next]; |
ysr@777 | 164 | // ...remove it from the sum and sum of squares |
ysr@777 | 165 | _sum -= old_val; |
ysr@777 | 166 | _sum_of_squares -= old_val * old_val; |
ysr@777 | 167 | |
ysr@777 | 168 | // ...and update them with the new value |
ysr@777 | 169 | _sum += val; |
ysr@777 | 170 | _sum_of_squares += val * val; |
ysr@777 | 171 | |
ysr@777 | 172 | // now replace the old value with the new one |
ysr@777 | 173 | _sequence[_next] = val; |
ysr@777 | 174 | _next = (_next + 1) % _length; |
ysr@777 | 175 | |
ysr@777 | 176 | // only increase it if the buffer is not full |
ysr@777 | 177 | if (_num < _length) |
ysr@777 | 178 | ++_num; |
ysr@777 | 179 | |
ysr@777 | 180 | guarantee( variance() > -1.0, "variance should be >= 0" ); |
ysr@777 | 181 | } |
ysr@777 | 182 | |
ysr@777 | 183 | // can't easily keep track of this incrementally... |
ysr@777 | 184 | double TruncatedSeq::maximum() const { |
ysr@777 | 185 | if (_num == 0) |
ysr@777 | 186 | return 0.0; |
ysr@777 | 187 | double ret = _sequence[0]; |
ysr@777 | 188 | for (int i = 1; i < _num; ++i) { |
ysr@777 | 189 | double val = _sequence[i]; |
ysr@777 | 190 | if (val > ret) |
ysr@777 | 191 | ret = val; |
ysr@777 | 192 | } |
ysr@777 | 193 | return ret; |
ysr@777 | 194 | } |
ysr@777 | 195 | |
ysr@777 | 196 | double TruncatedSeq::last() const { |
ysr@777 | 197 | if (_num == 0) |
ysr@777 | 198 | return 0.0; |
ysr@777 | 199 | unsigned last_index = (_next + _length - 1) % _length; |
ysr@777 | 200 | return _sequence[last_index]; |
ysr@777 | 201 | } |
ysr@777 | 202 | |
ysr@777 | 203 | double TruncatedSeq::oldest() const { |
ysr@777 | 204 | if (_num == 0) |
ysr@777 | 205 | return 0.0; |
ysr@777 | 206 | else if (_num < _length) |
ysr@777 | 207 | // index 0 always oldest value until the array is full |
ysr@777 | 208 | return _sequence[0]; |
ysr@777 | 209 | else { |
ysr@777 | 210 | // since the array is full, _next is over the oldest value |
ysr@777 | 211 | return _sequence[_next]; |
ysr@777 | 212 | } |
ysr@777 | 213 | } |
ysr@777 | 214 | |
ysr@777 | 215 | double TruncatedSeq::predict_next() const { |
ysr@777 | 216 | if (_num == 0) |
ysr@777 | 217 | return 0.0; |
ysr@777 | 218 | |
ysr@777 | 219 | double num = (double) _num; |
ysr@777 | 220 | double x_squared_sum = 0.0; |
ysr@777 | 221 | double x_sum = 0.0; |
ysr@777 | 222 | double y_sum = 0.0; |
ysr@777 | 223 | double xy_sum = 0.0; |
ysr@777 | 224 | double x_avg = 0.0; |
ysr@777 | 225 | double y_avg = 0.0; |
ysr@777 | 226 | |
ysr@777 | 227 | int first = (_next + _length - _num) % _length; |
ysr@777 | 228 | for (int i = 0; i < _num; ++i) { |
ysr@777 | 229 | double x = (double) i; |
ysr@777 | 230 | double y = _sequence[(first + i) % _length]; |
ysr@777 | 231 | |
ysr@777 | 232 | x_squared_sum += x * x; |
ysr@777 | 233 | x_sum += x; |
ysr@777 | 234 | y_sum += y; |
ysr@777 | 235 | xy_sum += x * y; |
ysr@777 | 236 | } |
ysr@777 | 237 | x_avg = x_sum / num; |
ysr@777 | 238 | y_avg = y_sum / num; |
ysr@777 | 239 | |
ysr@777 | 240 | double Sxx = x_squared_sum - x_sum * x_sum / num; |
ysr@777 | 241 | double Sxy = xy_sum - x_sum * y_sum / num; |
ysr@777 | 242 | double b1 = Sxy / Sxx; |
ysr@777 | 243 | double b0 = y_avg - b1 * x_avg; |
ysr@777 | 244 | |
ysr@777 | 245 | return b0 + b1 * num; |
ysr@777 | 246 | } |
ysr@1521 | 247 | |
ysr@1521 | 248 | |
ysr@1521 | 249 | // Printing/Debugging Support |
ysr@1521 | 250 | |
ysr@1521 | 251 | void AbsSeq::dump() { dump_on(gclog_or_tty); } |
ysr@1521 | 252 | |
ysr@1521 | 253 | void AbsSeq::dump_on(outputStream* s) { |
ysr@1521 | 254 | s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f", |
ysr@1521 | 255 | _num, _sum, _sum_of_squares); |
ysr@1521 | 256 | s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f", |
ysr@1521 | 257 | _davg, _dvariance, _alpha); |
ysr@1521 | 258 | } |
ysr@1521 | 259 | |
ysr@1521 | 260 | void NumberSeq::dump_on(outputStream* s) { |
ysr@1521 | 261 | AbsSeq::dump_on(s); |
ysr@1521 | 262 | s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f"); |
ysr@1521 | 263 | } |
ysr@1521 | 264 | |
ysr@1521 | 265 | void TruncatedSeq::dump_on(outputStream* s) { |
ysr@1521 | 266 | AbsSeq::dump_on(s); |
ysr@1521 | 267 | s->print_cr("\t\t _length = %d, _next = %d", _length, _next); |
ysr@1521 | 268 | for (int i = 0; i < _length; i++) { |
ysr@1521 | 269 | if (i%5 == 0) { |
ysr@1521 | 270 | s->cr(); |
ysr@1521 | 271 | s->print("\t"); |
ysr@1521 | 272 | } |
ysr@1521 | 273 | s->print("\t[%d]=%7.3f", i, _sequence[i]); |
ysr@1521 | 274 | } |
ysr@1521 | 275 | s->print_cr(""); |
ysr@1521 | 276 | } |