| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram/basic/common.h> | ||
| 41 | #include <geogram/basic/numeric.h> | ||
| 42 | |||
| 43 | // This makes sure the compiler will not optimize y = a*x+b | ||
| 44 | // with fused multiply-add, this would break the exact | ||
| 45 | // predicates. | ||
| 46 | GEO_FP_CONTRACT_OFF | ||
| 47 | |||
| 48 | #include <geogram/numerics/multi_precision.h> | ||
| 49 | #include <geogram/numerics/PCK.h> | ||
| 50 | #include <geogram/basic/process.h> | ||
| 51 | #include <geogram/basic/logger.h> | ||
| 52 | |||
| 53 | namespace { | ||
| 54 | |||
| 55 | using namespace GEO; | ||
| 56 | |||
| 57 | /************************************************************************/ | ||
| 58 | |||
| 59 | /** | ||
| 60 | * \brief Computes the sum of a length 2 expansion and a double | ||
| 61 | * into a length 3 expansion. | ||
| 62 | * \param[in] a1 high-magnitude component of first argument | ||
| 63 | * \param[in] a0 low-magnitude component of first argument | ||
| 64 | * \param[in] b second argument | ||
| 65 | * \param[in] x2 high-magnitude component of the result | ||
| 66 | * \param[in] x1 component of the result | ||
| 67 | * \param[in] x0 low-magnitude component of the result | ||
| 68 | * \details By Jonathan Shewchuk. | ||
| 69 | */ | ||
| 70 | inline void two_one_sum( | ||
| 71 | double a1, double a0, double b, double& x2, double& x1, double& x0 | ||
| 72 | ) { | ||
| 73 | double _i; | ||
| 74 | two_sum(a0, b, _i, x0); | ||
| 75 | two_sum(a1, _i, x2, x1); | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * \brief Computes the sum of a length 2 expansion and a double | ||
| 80 | * into a length 3 expansion. | ||
| 81 | * \param[in] a1 high-magnitude component of first argument | ||
| 82 | * \param[in] a0 low-magnitude component of first argument | ||
| 83 | * \param[in] b1 high-magnitude component of second argument | ||
| 84 | * \param[in] b0 high-magnitude component of second argument | ||
| 85 | * \param[in] x3 high-magnitude component of the result | ||
| 86 | * \param[in] x2 component of the result | ||
| 87 | * \param[in] x1 component of the result | ||
| 88 | * \param[in] x0 low-magnitude component of the result | ||
| 89 | * \details By Jonathan Shewchuk. | ||
| 90 | */ | ||
| 91 | 5193885 | inline void two_two_sum( | |
| 92 | double a1, double a0, double b1, double b0, | ||
| 93 | double& x3, double& x2, double& x1, double& x0 | ||
| 94 | ) { | ||
| 95 | double _j, _0; | ||
| 96 | two_one_sum(a1, a0, b0, _j, _0, x0); | ||
| 97 | two_one_sum(_j, _0, b1, x3, x2, x1); | ||
| 98 | 5193885 | } | |
| 99 | |||
| 100 | #ifndef FP_FAST_FMA | ||
| 101 | |||
| 102 | /** | ||
| 103 | * \brief Computes the product between two doubles where | ||
| 104 | * the second one have already been split. | ||
| 105 | * \param[in] a first argument | ||
| 106 | * \param[in] b second argument | ||
| 107 | * \param[in] bhi high-magnitude part of second argument | ||
| 108 | * \param[in] blo low-magnitude part of second argument | ||
| 109 | * \param[out] x high-magnitude component of the result | ||
| 110 | * \param[out] y low-magnitude component of the result | ||
| 111 | * \details By Jonathan Shewchuk. | ||
| 112 | */ | ||
| 113 | inline void two_product_presplit( | ||
| 114 | double a, double b, double bhi, double blo, double& x, double& y | ||
| 115 | ) { | ||
| 116 | x = a * b; | ||
| 117 | double ahi; | ||
| 118 | double alo; | ||
| 119 | split(a, ahi, alo); | ||
| 120 | double err1 = x - (ahi * bhi); | ||
| 121 | double err2 = err1 - (alo * bhi); | ||
| 122 | double err3 = err2 - (ahi * blo); | ||
| 123 | y = (alo * blo) - err3; | ||
| 124 | } | ||
| 125 | |||
| 126 | /** | ||
| 127 | * \brief Computes the product between two doubles | ||
| 128 | * where both have already been split. | ||
| 129 | * \param[in] a first argument | ||
| 130 | * \param[in] ahi high-magnitude part of first argument | ||
| 131 | * \param[in] alo low-magnitude part of first argument | ||
| 132 | * \param[in] b second argument | ||
| 133 | * \param[in] bhi high-magnitude part of second argument | ||
| 134 | * \param[in] blo low-magnitude part of second argument | ||
| 135 | * \param[out] x high-magnitude component of the result | ||
| 136 | * \param[out] y low-magnitude component of the result | ||
| 137 | * \details By Jonathan Shewchuk. | ||
| 138 | */ | ||
| 139 | inline void two_product_2presplit( | ||
| 140 | double a, double ahi, double alo, | ||
| 141 | double b, double bhi, double blo, | ||
| 142 | double& x, double& y | ||
| 143 | ) { | ||
| 144 | x = a * b; | ||
| 145 | double err1 = x - (ahi * bhi); | ||
| 146 | double err2 = err1 - (alo * bhi); | ||
| 147 | double err3 = err2 - (ahi * blo); | ||
| 148 | y = (alo * blo) - err3; | ||
| 149 | } | ||
| 150 | |||
| 151 | #endif | ||
| 152 | |||
| 153 | /** | ||
| 154 | * \brief Computes the square of an expansion of length 2. | ||
| 155 | * \param[in] a1 high-magnitude component of the argument | ||
| 156 | * \param[in] a0 low-magnitude component of the argument | ||
| 157 | * \param[out] x an array of six doubles to store the result. | ||
| 158 | * \details By Jonathan Shewchuk. | ||
| 159 | * An expansion of length two can be squared more quickly than finding the | ||
| 160 | * product of two different expansions of length two, and the result is | ||
| 161 | * guaranteed to have no more than six (rather than eight) components. | ||
| 162 | */ | ||
| 163 | 5193885 | inline void two_square( | |
| 164 | double a1, double a0, | ||
| 165 | double* x | ||
| 166 | ) { | ||
| 167 | double _0, _1, _2; | ||
| 168 | double _j, _k, _l; | ||
| 169 | square(a0, _j, x[0]); | ||
| 170 | 5193885 | _0 = a0 + a0; | |
| 171 | two_product(a1, _0, _k, _1); | ||
| 172 | two_one_sum(_k, _1, _j, _l, _2, x[1]); | ||
| 173 | square(a1, _j, _1); | ||
| 174 | 5193885 | two_two_sum(_j, _1, _l, _2, x[5], x[4], x[3], x[2]); | |
| 175 | 5193885 | } | |
| 176 | |||
| 177 | /** | ||
| 178 | * \brief Computes the product of two expansions of length 2. | ||
| 179 | * \param[in] a first argument (array of 2 doubles) | ||
| 180 | * \param[in] b second argument (array of 2 doubles) | ||
| 181 | * \param[out] x an array of 8 doubles to store the result | ||
| 182 | * \details By Jonathan Shewchuk. | ||
| 183 | */ | ||
| 184 | 50127261 | void two_two_product( | |
| 185 | const double* a, | ||
| 186 | const double* b, | ||
| 187 | double* x | ||
| 188 | ) { | ||
| 189 | double _0, _1, _2; | ||
| 190 | double _i, _j, _k, _l, _m, _n; | ||
| 191 | |||
| 192 | // If the target processor supports the FMA (Fused Multiply Add) | ||
| 193 | // instruction, then the product of two doubles into a length-2 | ||
| 194 | // expansion can be implemented as follows. Thanks to Marc Glisse | ||
| 195 | // for the information. | ||
| 196 | // Note: under gcc, automatic generations of fma() for a*b+c needs | ||
| 197 | // to be deactivated, using -ffp-contract=off, else it may break | ||
| 198 | // other functions such as fast_expansion_sum_zeroelim(). | ||
| 199 | #ifdef FP_FAST_FMA | ||
| 200 | 50127261 | two_product(a[0],b[0],_i,x[0]); | |
| 201 | 50127261 | two_product(a[1],b[0],_j,_0); | |
| 202 | two_sum(_i, _0, _k, _1); | ||
| 203 | fast_two_sum(_j, _k, _l, _2); | ||
| 204 | 50127261 | two_product(a[0], b[1], _i, _0); | |
| 205 | two_sum(_1, _0, _k, x[1]); | ||
| 206 | two_sum(_2, _k, _j, _1); | ||
| 207 | two_sum(_l, _j, _m, _2); | ||
| 208 | 50127261 | two_product(a[1], b[1], _j, _0); | |
| 209 | two_sum(_i, _0, _n, _0); | ||
| 210 | two_sum(_1, _0, _i, x[2]); | ||
| 211 | two_sum(_2, _i, _k, _1); | ||
| 212 | two_sum(_m, _k, _l, _2); | ||
| 213 | two_sum(_j, _n, _k, _0); | ||
| 214 | two_sum(_1, _0, _j, x[3]); | ||
| 215 | two_sum(_2, _j, _i, _1); | ||
| 216 | two_sum(_l, _i, _m, _2); | ||
| 217 | two_sum(_1, _k, _i, x[4]); | ||
| 218 | two_sum(_2, _i, _k, x[5]); | ||
| 219 | two_sum(_m, _k, x[7], x[6]); | ||
| 220 | #else | ||
| 221 | double a0hi, a0lo; | ||
| 222 | split(a[0], a0hi, a0lo); | ||
| 223 | double bhi, blo; | ||
| 224 | split(b[0], bhi, blo); | ||
| 225 | two_product_2presplit( | ||
| 226 | a[0], a0hi, a0lo, b[0], bhi, blo, _i, x[0] | ||
| 227 | ); | ||
| 228 | double a1hi, a1lo; | ||
| 229 | split(a[1], a1hi, a1lo); | ||
| 230 | two_product_2presplit( | ||
| 231 | a[1], a1hi, a1lo, b[0], bhi, blo, _j, _0 | ||
| 232 | ); | ||
| 233 | two_sum(_i, _0, _k, _1); | ||
| 234 | fast_two_sum(_j, _k, _l, _2); | ||
| 235 | split(b[1], bhi, blo); | ||
| 236 | two_product_2presplit( | ||
| 237 | a[0], a0hi, a0lo, b[1], bhi, blo, _i, _0 | ||
| 238 | ); | ||
| 239 | two_sum(_1, _0, _k, x[1]); | ||
| 240 | two_sum(_2, _k, _j, _1); | ||
| 241 | two_sum(_l, _j, _m, _2); | ||
| 242 | two_product_2presplit( | ||
| 243 | a[1], a1hi, a1lo, b[1], bhi, blo, _j, _0 | ||
| 244 | ); | ||
| 245 | two_sum(_i, _0, _n, _0); | ||
| 246 | two_sum(_1, _0, _i, x[2]); | ||
| 247 | two_sum(_2, _i, _k, _1); | ||
| 248 | two_sum(_m, _k, _l, _2); | ||
| 249 | two_sum(_j, _n, _k, _0); | ||
| 250 | two_sum(_1, _0, _j, x[3]); | ||
| 251 | two_sum(_2, _j, _i, _1); | ||
| 252 | two_sum(_l, _i, _m, _2); | ||
| 253 | two_sum(_1, _k, _i, x[4]); | ||
| 254 | two_sum(_2, _i, _k, x[5]); | ||
| 255 | two_sum(_m, _k, x[7], x[6]); | ||
| 256 | #endif | ||
| 257 | 50127261 | } | |
| 258 | |||
| 259 | // [Shewchuk 97] | ||
| 260 | // (https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) | ||
| 261 | // Section 2.8: other operations | ||
| 262 | // Compression | ||
| 263 | // Note: when converting the algorithms in Shewchuk's article | ||
| 264 | // into code, indices in the article go from 1 to m, and in the | ||
| 265 | // code they go from 0 to m-1 !!! | ||
| 266 | // /!\ there is a bug in the original article, | ||
| 267 | // line 14 of the algorithm should be h_top <= q (small q and not capital Q) | ||
| 268 | |||
| 269 | /** | ||
| 270 | * \brief Compresses an expansion | ||
| 271 | * \details Modifies in-place an expansion in such a way that it | ||
| 272 | * is shorter. The represented value is not modified. | ||
| 273 | * \param[in,out] e a reference to the expansion to be compressed | ||
| 274 | */ | ||
| 275 | 2290279 | void compress_expansion(expansion& e) { | |
| 276 | expansion& h = e; | ||
| 277 | |||
| 278 | index_t m = e.length(); | ||
| 279 | double Qnew,q; | ||
| 280 | |||
| 281 | 2290279 | index_t bottom = m-1; | |
| 282 | 2290279 | double Q = e[bottom]; | |
| 283 | |||
| 284 |
2/2✓ Branch 0 taken 3832660 times.
✓ Branch 1 taken 2290279 times.
|
6122939 | for(int i=int(m)-2; i>=0; --i) { |
| 285 |
2/2✓ Branch 0 taken 2424467 times.
✓ Branch 1 taken 1408193 times.
|
3832660 | fast_two_sum(Q, e[index_t(i)], Qnew, q); |
| 286 | Q = Qnew; | ||
| 287 |
2/2✓ Branch 0 taken 2424467 times.
✓ Branch 1 taken 1408193 times.
|
3832660 | if(q != 0.0) { |
| 288 | 2424467 | h[bottom] = Q; | |
| 289 | 2424467 | --bottom; | |
| 290 | Q = q; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | 2290279 | h[bottom] = Q; | |
| 294 | |||
| 295 | index_t top = 0; | ||
| 296 |
2/2✓ Branch 0 taken 2424467 times.
✓ Branch 1 taken 2290279 times.
|
4714746 | for(index_t i=bottom+1; i<m; ++i) { |
| 297 |
1/2✓ Branch 0 taken 2424467 times.
✗ Branch 1 not taken.
|
2424467 | fast_two_sum(h[i],Q,Qnew,q); |
| 298 | Q = Qnew; | ||
| 299 |
1/2✓ Branch 0 taken 2424467 times.
✗ Branch 1 not taken.
|
2424467 | if(q != 0) { |
| 300 | 2424467 | h[top] = q; | |
| 301 | 2424467 | ++top; | |
| 302 | } | ||
| 303 | } | ||
| 304 | 2290279 | h[top] = Q; | |
| 305 | 2290279 | h.set_length(top+1); | |
| 306 | 2290279 | } | |
| 307 | } | ||
| 308 | |||
| 309 | namespace GEO { | ||
| 310 | |||
| 311 | ✗ | void grow_expansion_zeroelim( | |
| 312 | const expansion& e, double b, expansion& h | ||
| 313 | ) { | ||
| 314 | double Q, hh; | ||
| 315 | double Qnew; | ||
| 316 | index_t eindex, hindex; | ||
| 317 | index_t elen = e.length(); | ||
| 318 | |||
| 319 | hindex = 0; | ||
| 320 | Q = b; | ||
| 321 | ✗ | for(eindex = 0; eindex < elen; eindex++) { | |
| 322 | ✗ | double enow = e[eindex]; | |
| 323 | two_sum(Q, enow, Qnew, hh); | ||
| 324 | Q = Qnew; | ||
| 325 | ✗ | if(hh != 0.0) { | |
| 326 | ✗ | h[hindex++] = hh; | |
| 327 | } | ||
| 328 | } | ||
| 329 | ✗ | if((Q != 0.0) || (hindex == 0)) { | |
| 330 | ✗ | h[hindex++] = Q; | |
| 331 | } | ||
| 332 | h.set_length(hindex); | ||
| 333 | ✗ | } | |
| 334 | |||
| 335 |
2/2✓ Branch 0 taken 17175066 times.
✓ Branch 1 taken 17986087 times.
|
35161153 | void scale_expansion_zeroelim( |
| 336 | const expansion& e, double b, expansion& h | ||
| 337 | ) { | ||
| 338 | double Q, sum; | ||
| 339 | double hh; | ||
| 340 | double product1; | ||
| 341 | double product0; | ||
| 342 | index_t eindex, hindex; | ||
| 343 | |||
| 344 | // If the target processor supports the FMA (Fused Multiply Add) | ||
| 345 | // instruction, then the product of two doubles into a length-2 | ||
| 346 | // expansion can be implemented as follows. Thanks to Marc Glisse | ||
| 347 | // for the information. | ||
| 348 | // Note: under gcc, automatic generations of fma() for a*b+c needs | ||
| 349 | // to be deactivated, using -ffp-contract=off, else it may break | ||
| 350 | // other functions such as fast_expansion_sum_zeroelim(). | ||
| 351 | #ifndef FP_FAST_FMA | ||
| 352 | double bhi, blo; | ||
| 353 | #endif | ||
| 354 | index_t elen = e.length(); | ||
| 355 | |||
| 356 | // Sanity check: e and h cannot be the same. | ||
| 357 | geo_debug_assert(&e != &h); | ||
| 358 | |||
| 359 | #ifdef FP_FAST_FMA | ||
| 360 |
2/2✓ Branch 0 taken 17175066 times.
✓ Branch 1 taken 17986087 times.
|
35161153 | two_product(e[0], b, Q, hh); |
| 361 | #else | ||
| 362 | split(b, bhi, blo); | ||
| 363 | two_product_presplit(e[0], b, bhi, blo, Q, hh); | ||
| 364 | #endif | ||
| 365 | |||
| 366 | hindex = 0; | ||
| 367 |
2/2✓ Branch 0 taken 17175066 times.
✓ Branch 1 taken 17986087 times.
|
35161153 | if(hh != 0) { |
| 368 | 17175066 | h[hindex++] = hh; | |
| 369 | } | ||
| 370 |
2/2✓ Branch 0 taken 96928127 times.
✓ Branch 1 taken 35161153 times.
|
132089280 | for(eindex = 1; eindex < elen; eindex++) { |
| 371 |
2/2✓ Branch 0 taken 12607319 times.
✓ Branch 1 taken 84320808 times.
|
96928127 | double enow = e[eindex]; |
| 372 | #ifdef FP_FAST_FMA | ||
| 373 | two_product(enow, b, product1, product0); | ||
| 374 | #else | ||
| 375 | two_product_presplit(enow, b, bhi, blo, product1, product0); | ||
| 376 | #endif | ||
| 377 | two_sum(Q, product0, sum, hh); | ||
| 378 |
2/2✓ Branch 0 taken 12607319 times.
✓ Branch 1 taken 84320808 times.
|
96928127 | if(hh != 0) { |
| 379 | 12607319 | h[hindex++] = hh; | |
| 380 | } | ||
| 381 | fast_two_sum(product1, sum, Q, hh); | ||
| 382 |
2/2✓ Branch 0 taken 79915289 times.
✓ Branch 1 taken 17012838 times.
|
96928127 | if(hh != 0) { |
| 383 | 79915289 | h[hindex++] = hh; | |
| 384 | } | ||
| 385 | } | ||
| 386 |
3/4✓ Branch 0 taken 7734644 times.
✓ Branch 1 taken 27426509 times.
✓ Branch 2 taken 7734644 times.
✗ Branch 3 not taken.
|
35161153 | if((Q != 0.0) || (hindex == 0)) { |
| 387 | 35161153 | h[hindex++] = Q; | |
| 388 | } | ||
| 389 | h.set_length(hindex); | ||
| 390 | 35161153 | } | |
| 391 | |||
| 392 |
2/2✓ Branch 0 taken 30178592 times.
✓ Branch 1 taken 4023488 times.
|
34202080 | void fast_expansion_sum_zeroelim( |
| 393 | const expansion& e, const expansion& f, expansion& h | ||
| 394 | ) { | ||
| 395 | double Q; | ||
| 396 | double Qnew; | ||
| 397 | double hh; | ||
| 398 | index_t eindex, findex, hindex; | ||
| 399 | double enow, fnow; | ||
| 400 | index_t elen = e.length(); | ||
| 401 | index_t flen = f.length(); | ||
| 402 | |||
| 403 | // sanity check: h cannot be e or f | ||
| 404 | geo_debug_assert(&h != &e); | ||
| 405 | geo_debug_assert(&h != &f); | ||
| 406 | |||
| 407 | 34202080 | enow = e[0]; | |
| 408 | 34202080 | fnow = f[0]; | |
| 409 | eindex = findex = 0; | ||
| 410 |
2/2✓ Branch 0 taken 30178592 times.
✓ Branch 1 taken 4023488 times.
|
34202080 | if((fnow > enow) == (fnow > -enow)) { |
| 411 | Q = enow; | ||
| 412 | 30178592 | enow = e[++eindex]; | |
| 413 | } else { | ||
| 414 | Q = fnow; | ||
| 415 | 4023488 | fnow = f[++findex]; | |
| 416 | } | ||
| 417 | hindex = 0; | ||
| 418 |
2/2✓ Branch 0 taken 27328413 times.
✓ Branch 1 taken 6873667 times.
|
34202080 | if((eindex < elen) && (findex < flen)) { |
| 419 |
2/2✓ Branch 0 taken 21722468 times.
✓ Branch 1 taken 5605945 times.
|
27328413 | if((fnow > enow) == (fnow > -enow)) { |
| 420 | fast_two_sum(enow, Q, Qnew, hh); | ||
| 421 | 21722468 | enow = e[++eindex]; | |
| 422 | } else { | ||
| 423 | fast_two_sum(fnow, Q, Qnew, hh); | ||
| 424 | 5605945 | fnow = f[++findex]; | |
| 425 | } | ||
| 426 | Q = Qnew; | ||
| 427 |
2/2✓ Branch 0 taken 4544645 times.
✓ Branch 1 taken 22783768 times.
|
27328413 | if(hh != 0.0) { |
| 428 | 4544645 | h[hindex++] = hh; | |
| 429 | } | ||
| 430 |
2/2✓ Branch 0 taken 234526530 times.
✓ Branch 1 taken 27328413 times.
|
261854943 | while((eindex < elen) && (findex < flen)) { |
| 431 |
2/2✓ Branch 0 taken 131907709 times.
✓ Branch 1 taken 102618821 times.
|
234526530 | if((fnow > enow) == (fnow > -enow)) { |
| 432 | two_sum(Q, enow, Qnew, hh); | ||
| 433 | 131907709 | enow = e[++eindex]; | |
| 434 | } else { | ||
| 435 | two_sum(Q, fnow, Qnew, hh); | ||
| 436 | 102618821 | fnow = f[++findex]; | |
| 437 | } | ||
| 438 | Q = Qnew; | ||
| 439 |
2/2✓ Branch 0 taken 113716070 times.
✓ Branch 1 taken 120810460 times.
|
234526530 | if(hh != 0.0) { |
| 440 | 113716070 | h[hindex++] = hh; | |
| 441 | } | ||
| 442 | } | ||
| 443 | } | ||
| 444 |
2/2✓ Branch 0 taken 9401854 times.
✓ Branch 1 taken 34202080 times.
|
43603934 | while(eindex < elen) { |
| 445 | two_sum(Q, enow, Qnew, hh); | ||
| 446 | 9401854 | enow = e[++eindex]; | |
| 447 | Q = Qnew; | ||
| 448 |
2/2✓ Branch 0 taken 2923938 times.
✓ Branch 1 taken 6477916 times.
|
9401854 | if(hh != 0.0) { |
| 449 | 2923938 | h[hindex++] = hh; | |
| 450 | } | ||
| 451 | } | ||
| 452 |
2/2✓ Branch 0 taken 49000270 times.
✓ Branch 1 taken 34202080 times.
|
83202350 | while(findex < flen) { |
| 453 | two_sum(Q, fnow, Qnew, hh); | ||
| 454 | 49000270 | fnow = f[++findex]; | |
| 455 | Q = Qnew; | ||
| 456 |
2/2✓ Branch 0 taken 20788335 times.
✓ Branch 1 taken 28211935 times.
|
49000270 | if(hh != 0.0) { |
| 457 | 20788335 | h[hindex++] = hh; | |
| 458 | } | ||
| 459 | } | ||
| 460 |
2/2✓ Branch 0 taken 34174381 times.
✓ Branch 1 taken 27699 times.
|
34202080 | if((Q != 0.0) || (hindex == 0)) { |
| 461 | 34174381 | h[hindex++] = Q; | |
| 462 | } | ||
| 463 | h.set_length(hindex); | ||
| 464 | 34202080 | } | |
| 465 | |||
| 466 |
2/2✓ Branch 0 taken 25441314 times.
✓ Branch 1 taken 4807846 times.
|
30249160 | void fast_expansion_diff_zeroelim( |
| 467 | const expansion& e, const expansion& f, expansion& h | ||
| 468 | ) { | ||
| 469 | double Q; | ||
| 470 | double Qnew; | ||
| 471 | double hh; | ||
| 472 | index_t eindex, findex, hindex; | ||
| 473 | double enow, fnow; | ||
| 474 | index_t elen = e.length(); | ||
| 475 | index_t flen = f.length(); | ||
| 476 | |||
| 477 | // sanity check: h cannot be e or f | ||
| 478 | geo_debug_assert(&h != &e); | ||
| 479 | geo_debug_assert(&h != &f); | ||
| 480 | |||
| 481 | 30249160 | enow = e[0]; | |
| 482 | 30249160 | fnow = -f[0]; | |
| 483 | eindex = findex = 0; | ||
| 484 |
2/2✓ Branch 0 taken 25441314 times.
✓ Branch 1 taken 4807846 times.
|
30249160 | if((fnow > enow) == (fnow > -enow)) { |
| 485 | Q = enow; | ||
| 486 | 25441314 | enow = e[++eindex]; | |
| 487 | } else { | ||
| 488 | Q = fnow; | ||
| 489 | 4807846 | fnow = -f[++findex]; | |
| 490 | } | ||
| 491 | hindex = 0; | ||
| 492 |
2/2✓ Branch 0 taken 26664895 times.
✓ Branch 1 taken 3584265 times.
|
30249160 | if((eindex < elen) && (findex < flen)) { |
| 493 |
2/2✓ Branch 0 taken 23283420 times.
✓ Branch 1 taken 3381475 times.
|
26664895 | if((fnow > enow) == (fnow > -enow)) { |
| 494 | fast_two_sum(enow, Q, Qnew, hh); | ||
| 495 | 23283420 | enow = e[++eindex]; | |
| 496 | } else { | ||
| 497 | fast_two_sum(fnow, Q, Qnew, hh); | ||
| 498 | 3381475 | fnow = -f[++findex]; | |
| 499 | } | ||
| 500 | Q = Qnew; | ||
| 501 |
2/2✓ Branch 0 taken 554374 times.
✓ Branch 1 taken 26110521 times.
|
26664895 | if(hh != 0.0) { |
| 502 | 554374 | h[hindex++] = hh; | |
| 503 | } | ||
| 504 |
2/2✓ Branch 0 taken 241790021 times.
✓ Branch 1 taken 26664895 times.
|
268454916 | while((eindex < elen) && (findex < flen)) { |
| 505 |
2/2✓ Branch 0 taken 138021250 times.
✓ Branch 1 taken 103768771 times.
|
241790021 | if((fnow > enow) == (fnow > -enow)) { |
| 506 | two_sum(Q, enow, Qnew, hh); | ||
| 507 | 138021250 | enow = e[++eindex]; | |
| 508 | } else { | ||
| 509 | two_sum(Q, fnow, Qnew, hh); | ||
| 510 | 103768771 | fnow = -f[++findex]; | |
| 511 | } | ||
| 512 | Q = Qnew; | ||
| 513 |
2/2✓ Branch 0 taken 30761570 times.
✓ Branch 1 taken 211028451 times.
|
241790021 | if(hh != 0.0) { |
| 514 | 30761570 | h[hindex++] = hh; | |
| 515 | } | ||
| 516 | } | ||
| 517 | } | ||
| 518 |
2/2✓ Branch 0 taken 12783543 times.
✓ Branch 1 taken 30249160 times.
|
43032703 | while(eindex < elen) { |
| 519 | two_sum(Q, enow, Qnew, hh); | ||
| 520 | 12783543 | enow = e[++eindex]; | |
| 521 | Q = Qnew; | ||
| 522 |
2/2✓ Branch 0 taken 3909909 times.
✓ Branch 1 taken 8873634 times.
|
12783543 | if(hh != 0.0) { |
| 523 | 3909909 | h[hindex++] = hh; | |
| 524 | } | ||
| 525 | } | ||
| 526 |
2/2✓ Branch 0 taken 87464778 times.
✓ Branch 1 taken 30249160 times.
|
117713938 | while(findex < flen) { |
| 527 | two_sum(Q, fnow, Qnew, hh); | ||
| 528 | 87464778 | fnow = -f[++findex]; | |
| 529 | Q = Qnew; | ||
| 530 |
2/2✓ Branch 0 taken 3752894 times.
✓ Branch 1 taken 83711884 times.
|
87464778 | if(hh != 0.0) { |
| 531 | 3752894 | h[hindex++] = hh; | |
| 532 | } | ||
| 533 | } | ||
| 534 |
2/2✓ Branch 0 taken 30221487 times.
✓ Branch 1 taken 27673 times.
|
30249160 | if((Q != 0.0) || (hindex == 0)) { |
| 535 | 30221487 | h[hindex++] = Q; | |
| 536 | } | ||
| 537 | h.set_length(hindex); | ||
| 538 | 30249160 | } | |
| 539 | |||
| 540 | } | ||
| 541 | |||
| 542 | /****************************************************************************/ | ||
| 543 | |||
| 544 | namespace GEO { | ||
| 545 | |||
| 546 | double expansion_splitter_; | ||
| 547 | double expansion_epsilon_; | ||
| 548 | |||
| 549 | 252 | void expansion::initialize() { | |
| 550 | // Taken from Jonathan Shewchuk's exactinit. | ||
| 551 | double half; | ||
| 552 | double check, lastcheck; | ||
| 553 | int every_other; | ||
| 554 | |||
| 555 | every_other = 1; | ||
| 556 | half = 0.5; | ||
| 557 | 252 | expansion_epsilon_ = 1.0; | |
| 558 | 252 | expansion_splitter_ = 1.0; | |
| 559 | check = 1.0; | ||
| 560 | // Repeatedly divide `epsilon' by two until it is too small to add to | ||
| 561 | // one without causing roundoff. (Also check if the sum is equal to | ||
| 562 | // the previous sum, for machines that round up instead of using exact | ||
| 563 | // rounding. Not that this library will work on such machines anyway. | ||
| 564 | do { | ||
| 565 | lastcheck = check; | ||
| 566 | 13356 | expansion_epsilon_ *= half; | |
| 567 |
2/2✓ Branch 0 taken 6804 times.
✓ Branch 1 taken 6552 times.
|
13356 | if(every_other) { |
| 568 | 6804 | expansion_splitter_ *= 2.0; | |
| 569 | } | ||
| 570 | 13356 | every_other = !every_other; | |
| 571 | 13356 | check = 1.0 + expansion_epsilon_; | |
| 572 |
2/2✓ Branch 0 taken 13104 times.
✓ Branch 1 taken 252 times.
|
13356 | } while((check != 1.0) && (check != lastcheck)); |
| 573 | 252 | expansion_splitter_ += 1.0; | |
| 574 | 252 | } | |
| 575 | |||
| 576 | // ====== Initialization from expansion and double =============== | ||
| 577 | |||
| 578 | ✗ | expansion& expansion::assign_sum(const expansion& a, double b) { | |
| 579 | geo_debug_assert(capacity() >= sum_capacity(a, b)); | ||
| 580 | ✗ | grow_expansion_zeroelim(a, b, *this); | |
| 581 | ✗ | return *this; | |
| 582 | } | ||
| 583 | |||
| 584 | ✗ | expansion& expansion::assign_diff(const expansion& a, double b) { | |
| 585 | geo_debug_assert(capacity() >= diff_capacity(a, b)); | ||
| 586 | ✗ | grow_expansion_zeroelim(a, -b, *this); | |
| 587 | ✗ | return *this; | |
| 588 | } | ||
| 589 | |||
| 590 | 25301071 | expansion& expansion::assign_product(const expansion& a, double b) { | |
| 591 | // TODO: implement special case where the double argument | ||
| 592 | // is a power of two. | ||
| 593 | geo_debug_assert(capacity() >= product_capacity(a, b)); | ||
| 594 | 25301071 | scale_expansion_zeroelim(a, b, *this); | |
| 595 | 25301071 | return *this; | |
| 596 | } | ||
| 597 | |||
| 598 | // ============= expansion sum and difference ========================= | ||
| 599 | |||
| 600 | 34202080 | expansion& expansion::assign_sum( | |
| 601 | const expansion& a, const expansion& b | ||
| 602 | ) { | ||
| 603 | geo_debug_assert(capacity() >= sum_capacity(a, b)); | ||
| 604 | 34202080 | fast_expansion_sum_zeroelim(a, b, *this); | |
| 605 | 34202080 | return *this; | |
| 606 | } | ||
| 607 | |||
| 608 | 4772898 | expansion& expansion::assign_sum( | |
| 609 | const expansion& a, const expansion& b, const expansion& c | ||
| 610 | ) { | ||
| 611 | geo_debug_assert(capacity() >= sum_capacity(a, b, c)); | ||
| 612 | 4772898 | expansion& ab = expansion_sum(a, b); | |
| 613 | 4772898 | this->assign_sum(ab, c); | |
| 614 | 4772898 | return *this; | |
| 615 | } | ||
| 616 | |||
| 617 | 297392 | expansion& expansion::assign_sum( | |
| 618 | const expansion& a, const expansion& b, | ||
| 619 | const expansion& c, const expansion& d | ||
| 620 | ) { | ||
| 621 | geo_debug_assert(capacity() >= sum_capacity(a, b, c)); | ||
| 622 | 297392 | expansion& ab = expansion_sum(a, b); | |
| 623 | 297392 | expansion& cd = expansion_sum(c, d); | |
| 624 | 297392 | this->assign_sum(ab, cd); | |
| 625 | 297392 | return *this; | |
| 626 | } | ||
| 627 | |||
| 628 | 30249160 | expansion& expansion::assign_diff(const expansion& a, const expansion& b) { | |
| 629 | geo_debug_assert(capacity() >= diff_capacity(a, b)); | ||
| 630 | 30249160 | fast_expansion_diff_zeroelim(a, b, *this); | |
| 631 | 30249160 | return *this; | |
| 632 | } | ||
| 633 | |||
| 634 | // ============= expansion product ================================== | ||
| 635 | |||
| 636 | // Recursive helper function for product implementation | ||
| 637 | 67865 | expansion& expansion::assign_sub_product( | |
| 638 | const double* a, index_t a_length, const expansion& b | ||
| 639 | ) { | ||
| 640 | geo_debug_assert( | ||
| 641 | capacity() >= sub_product_capacity(a_length, b.length()) | ||
| 642 | ); | ||
| 643 |
2/2✓ Branch 0 taken 34829 times.
✓ Branch 1 taken 33036 times.
|
67865 | if(a_length == 1) { |
| 644 | 34829 | scale_expansion_zeroelim(b, a[0], *this); | |
| 645 | } else { | ||
| 646 | // "Distillation" (see Shewchuk's paper) is computed recursively, | ||
| 647 | // by splitting the list of expansions to sum into two halves. | ||
| 648 | |||
| 649 | const double* a1 = a; | ||
| 650 | 33036 | index_t a1_length = a_length / 2; | |
| 651 | 33036 | const double* a2 = a1 + a1_length; | |
| 652 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 33018 times.
|
33036 | index_t a2_length = a_length - a1_length; |
| 653 | |||
| 654 | // Allocate both halves on the stack or on the heap if too large | ||
| 655 | // (some platformes, e.g. MacOSX, have a small stack) | ||
| 656 | |||
| 657 | index_t a1b_capa = sub_product_capacity(a1_length, b.length()); | ||
| 658 | index_t a2b_capa = sub_product_capacity(a2_length, b.length()); | ||
| 659 | |||
| 660 | bool a1b_on_heap = (a1b_capa > MAX_CAPACITY_ON_STACK); | ||
| 661 | bool a2b_on_heap = (a2b_capa > MAX_CAPACITY_ON_STACK); | ||
| 662 | |||
| 663 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 33018 times.
|
33036 | expansion* a1b = a1b_on_heap ? |
| 664 | new_expansion_on_heap(a1b_capa) : | ||
| 665 | 33018 | new_expansion_on_stack(a1b_capa); | |
| 666 | |||
| 667 | 33036 | a1b->assign_sub_product(a1, a1_length, b); | |
| 668 | |||
| 669 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 33011 times.
|
33036 | expansion* a2b = a2b_on_heap ? |
| 670 | new_expansion_on_heap(a2b_capa) : | ||
| 671 | 33011 | new_expansion_on_stack(a2b_capa); | |
| 672 | |||
| 673 | 33036 | a2b->assign_sub_product(a2, a2_length, b); | |
| 674 | |||
| 675 | 33036 | this->assign_sum(*a1b, *a2b); | |
| 676 | |||
| 677 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 33018 times.
|
33036 | if(a1b_on_heap) { |
| 678 | delete_expansion_on_heap(a1b); | ||
| 679 | } | ||
| 680 | |||
| 681 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 33011 times.
|
33036 | if(a2b_on_heap) { |
| 682 | delete_expansion_on_heap(a2b); | ||
| 683 | } | ||
| 684 | } | ||
| 685 | 67865 | return *this; | |
| 686 | } | ||
| 687 | |||
| 688 |
1/2✓ Branch 0 taken 67541206 times.
✗ Branch 1 not taken.
|
67541206 | expansion& expansion::assign_product( |
| 689 | const expansion& a, const expansion& b | ||
| 690 | ) { | ||
| 691 | geo_debug_assert(capacity() >= product_capacity(a, b)); | ||
| 692 |
2/4✓ Branch 0 taken 67541206 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67541206 times.
|
67541206 | if(a.length() == 0 || b.length() == 0) { |
| 693 | ✗ | x_[0] = 0.0; | |
| 694 | set_length(0); | ||
| 695 |
4/4✓ Branch 0 taken 6456496 times.
✓ Branch 1 taken 61084710 times.
✓ Branch 2 taken 1948176 times.
✓ Branch 3 taken 4508320 times.
|
67541206 | } else if(a.length() == 1 && b.length() == 1) { |
| 696 | 4508320 | two_product(a[0], b[0], x_[1], x_[0]); | |
| 697 | set_length(2); | ||
| 698 |
2/2✓ Branch 0 taken 1948176 times.
✓ Branch 1 taken 61084710 times.
|
63032886 | } else if(a.length() == 1) { |
| 699 | 1948176 | scale_expansion_zeroelim(b, a[0], *this); | |
| 700 |
2/2✓ Branch 0 taken 7877077 times.
✓ Branch 1 taken 53207633 times.
|
61084710 | } else if(b.length() == 1) { |
| 701 | 7877077 | scale_expansion_zeroelim(a, b[0], *this); | |
| 702 |
4/4✓ Branch 0 taken 46801357 times.
✓ Branch 1 taken 6406276 times.
✓ Branch 2 taken 2826859 times.
✓ Branch 3 taken 43974498 times.
|
53207633 | } else if(a.length() == 2 && b.length() == 2) { |
| 703 | 43974498 | two_two_product(a.data(), b.data(), x_); | |
| 704 | set_length(8); | ||
| 705 | } else { | ||
| 706 | |||
| 707 | |||
| 708 | const expansion* pa = &a; | ||
| 709 | const expansion* pb = &b; | ||
| 710 | |||
| 711 |
2/2✓ Branch 0 taken 2976691 times.
✓ Branch 1 taken 6256444 times.
|
9233135 | if(pa->length() > pb->length()) { |
| 712 | std::swap(pa, pb); | ||
| 713 | } | ||
| 714 | |||
| 715 | // [Shewchuk 97] | ||
| 716 | // (https://people.eecs.berkeley.edu/~jrs/papers/robustr.pdf) | ||
| 717 | // Section 2.8: other operations | ||
| 718 | // Distillation: sum of k values. | ||
| 719 | // Worst case: 1/2*k*(k-1) | ||
| 720 | // But O(k log(k)) if the "summing tree" is well balanced | ||
| 721 | // and using fast_expansion_sum(). | ||
| 722 | // Recommended way of computing a product: | ||
| 723 | // compute a1*b, a2*b ... ak*b using scale_expansion_zeroelim() | ||
| 724 | // sum them using a well-balanced tree | ||
| 725 | // However, there is an extra cost for the recursion (and more | ||
| 726 | // importantly, for allocating the intermediary sums, especially | ||
| 727 | // when they do not fit on the stack). So when there are less than | ||
| 728 | // 16 values to add, we simply accumulate them. | ||
| 729 | |||
| 730 | bool use_balanced_distillation = (pa->length() >= 16); | ||
| 731 | |||
| 732 |
2/2✓ Branch 0 taken 1793 times.
✓ Branch 1 taken 9231342 times.
|
9233135 | if(use_balanced_distillation) { |
| 733 | // assign_sub_product() is a recursive function that | ||
| 734 | // creates a balanced distillation tree on the stack. | ||
| 735 | 1793 | assign_sub_product(pa->data(), pa->length(),*pb); | |
| 736 | } else { | ||
| 737 | // trivial implementation: compute all the products | ||
| 738 | // P = ak*b and accumulate them into S | ||
| 739 | |||
| 740 | index_t P_capa = product_capacity(*pb, 3.0); // 3.0, or any | ||
| 741 | // number that is | ||
| 742 | // not a power of 2 | ||
| 743 | |||
| 744 | index_t S_capa = capacity(); // same capacity as this, | ||
| 745 | // enough to store sum. | ||
| 746 | |||
| 747 | bool P_on_heap = (P_capa > MAX_CAPACITY_ON_STACK); | ||
| 748 | bool S_on_heap = (S_capa > MAX_CAPACITY_ON_STACK); | ||
| 749 | |||
| 750 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9231342 times.
|
9231342 | expansion* P = P_on_heap ? |
| 751 | new_expansion_on_heap(P_capa) : | ||
| 752 | 9231342 | new_expansion_on_stack(P_capa); | |
| 753 | |||
| 754 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9231341 times.
|
9231342 | expansion* S = S_on_heap ? |
| 755 | new_expansion_on_heap(S_capa) : | ||
| 756 | 9231341 | new_expansion_on_stack(S_capa); | |
| 757 | |||
| 758 | expansion* S1 = S; | ||
| 759 | expansion* S2 = this; | ||
| 760 | |||
| 761 |
2/2✓ Branch 0 taken 5382431 times.
✓ Branch 1 taken 3848911 times.
|
9231342 | if((pa->length()%2) == 0) { |
| 762 | std::swap(S1,S2); | ||
| 763 | } | ||
| 764 | |||
| 765 |
2/2✓ Branch 0 taken 23964265 times.
✓ Branch 1 taken 9231342 times.
|
33195607 | for(index_t i=0; i<pa->length(); ++i) { |
| 766 |
2/2✓ Branch 0 taken 9231342 times.
✓ Branch 1 taken 14732923 times.
|
23964265 | if(i == 0) { |
| 767 | 9231342 | S2->assign_product(*pb, (*pa)[i]); | |
| 768 | } else { | ||
| 769 | 14732923 | P->assign_product(*pb, (*pa)[i]); | |
| 770 | 14732923 | S2->assign_sum(*S1,*P); | |
| 771 | } | ||
| 772 | std::swap(S1,S2); | ||
| 773 | } | ||
| 774 | |||
| 775 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 9231342 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
9231342 | geo_assert(S1 == this); |
| 776 | |||
| 777 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9231341 times.
|
9231342 | if(S_on_heap) { |
| 778 | delete_expansion_on_heap(S); | ||
| 779 | } | ||
| 780 | |||
| 781 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9231342 times.
|
9231342 | if(P_on_heap) { |
| 782 | delete_expansion_on_heap(P); | ||
| 783 | } | ||
| 784 | } | ||
| 785 | } | ||
| 786 | 67541206 | return *this; | |
| 787 | } | ||
| 788 | |||
| 789 | ✗ | expansion& expansion::assign_product( | |
| 790 | const expansion& a, const expansion& b, const expansion& c | ||
| 791 | ) { | ||
| 792 | ✗ | const expansion& bc = expansion_product(b, c); | |
| 793 | ✗ | this->assign_product(a, bc); | |
| 794 | ✗ | return *this; | |
| 795 | } | ||
| 796 | |||
| 797 | ✗ | expansion& expansion::assign_square(const expansion& a) { | |
| 798 | geo_debug_assert(capacity() >= square_capacity(a)); | ||
| 799 | ✗ | if(a.length() == 1) { | |
| 800 | ✗ | square(a[0], x_[1], x_[0]); | |
| 801 | set_length(2); | ||
| 802 | ✗ | } else if(a.length() == 2) { | |
| 803 | ✗ | two_square(a[1], a[0], x_); | |
| 804 | set_length(6); | ||
| 805 | } else { | ||
| 806 | ✗ | this->assign_product(a, a); | |
| 807 | } | ||
| 808 | ✗ | return *this; | |
| 809 | } | ||
| 810 | |||
| 811 | // ============= determinants ========================================== | ||
| 812 | |||
| 813 | 23813812 | expansion& expansion::assign_det2x2( | |
| 814 | const expansion& a11, const expansion& a12, | ||
| 815 | const expansion& a21, const expansion& a22 | ||
| 816 | ) { | ||
| 817 | 23813812 | const expansion& a11a22 = expansion_product(a11, a22); | |
| 818 | 23813812 | const expansion& a12a21 = expansion_product(a12, a21); | |
| 819 | 23813812 | return this->assign_diff(a11a22, a12a21); | |
| 820 | } | ||
| 821 | |||
| 822 | 3048334 | expansion& expansion::assign_det3x3( | |
| 823 | const expansion& a11, const expansion& a12, const expansion& a13, | ||
| 824 | const expansion& a21, const expansion& a22, const expansion& a23, | ||
| 825 | const expansion& a31, const expansion& a32, const expansion& a33 | ||
| 826 | ) { | ||
| 827 | // Development w.r.t. first row | ||
| 828 | 3048334 | const expansion& c11 = expansion_det2x2(a22, a23, a32, a33); | |
| 829 | 3048334 | const expansion& c12 = expansion_det2x2(a23, a21, a33, a31); | |
| 830 | 3048334 | const expansion& c13 = expansion_det2x2(a21, a22, a31, a32); | |
| 831 | 3048334 | const expansion& a11c11 = expansion_product(a11, c11); | |
| 832 | 3048334 | const expansion& a12c12 = expansion_product(a12, c12); | |
| 833 | 3048334 | const expansion& a13c13 = expansion_product(a13, c13); | |
| 834 | 3048334 | return this->assign_sum(a11c11, a12c12, a13c13); | |
| 835 | } | ||
| 836 | |||
| 837 | ✗ | expansion& expansion::assign_det_111_2x3( | |
| 838 | const expansion& a21, const expansion& a22, const expansion& a23, | ||
| 839 | const expansion& a31, const expansion& a32, const expansion& a33 | ||
| 840 | ) { | ||
| 841 | ✗ | const expansion& c11 = expansion_det2x2(a22, a23, a32, a33); | |
| 842 | ✗ | const expansion& c12 = expansion_det2x2(a23, a21, a33, a31); | |
| 843 | ✗ | const expansion& c13 = expansion_det2x2(a21, a22, a31, a32); | |
| 844 | ✗ | return this->assign_sum(c11, c12, c13); | |
| 845 | } | ||
| 846 | |||
| 847 | // ============= geometric operations ================================== | ||
| 848 | |||
| 849 | 8656447 | expansion& expansion::assign_sq_dist( | |
| 850 | const double* p1, const double* p2, coord_index_t dim | ||
| 851 | ) { | ||
| 852 | geo_debug_assert(capacity() >= sq_dist_capacity(dim)); | ||
| 853 | geo_debug_assert(dim > 0); | ||
| 854 |
2/2✓ Branch 0 taken 5193885 times.
✓ Branch 1 taken 3462562 times.
|
8656447 | if(dim == 1) { |
| 855 | double d0, d1; | ||
| 856 | 5193885 | two_diff(p1[0], p2[0], d1, d0); | |
| 857 | 5193885 | two_square(d1, d0, x_); | |
| 858 | set_length(6); | ||
| 859 | } else { | ||
| 860 | // "Distillation" (see Shewchuk's paper) is computed recursively, | ||
| 861 | // by splitting the list of expansions to sum into two halves. | ||
| 862 | 3462562 | coord_index_t dim1 = dim / 2; | |
| 863 | 3462562 | coord_index_t dim2 = coord_index_t(dim - dim1); | |
| 864 | 3462562 | const double* p1_2 = p1 + dim1; | |
| 865 | 3462562 | const double* p2_2 = p2 + dim1; | |
| 866 | 3462562 | expansion& d1 = expansion_sq_dist(p1, p2, dim1); | |
| 867 | 3462562 | expansion& d2 = expansion_sq_dist(p1_2, p2_2, dim2); | |
| 868 | 3462562 | this->assign_sum(d1, d2); | |
| 869 | } | ||
| 870 | 8656447 | return *this; | |
| 871 | } | ||
| 872 | |||
| 873 | 10254521 | expansion& expansion::assign_dot_at( | |
| 874 | const double* p1, const double* p2, const double* p0, | ||
| 875 | coord_index_t dim | ||
| 876 | ) { | ||
| 877 | geo_debug_assert(capacity() >= dot_at_capacity(dim)); | ||
| 878 |
2/2✓ Branch 0 taken 6152763 times.
✓ Branch 1 taken 4101758 times.
|
10254521 | if(dim == 1) { |
| 879 | |||
| 880 | double v[2]; | ||
| 881 | 6152763 | two_diff(p1[0], p0[0], v[1], v[0]); | |
| 882 | double w[2]; | ||
| 883 | 6152763 | two_diff(p2[0], p0[0], w[1], w[0]); | |
| 884 | 6152763 | two_two_product(v, w, x_); | |
| 885 | set_length(8); | ||
| 886 | } else { | ||
| 887 | // "Distillation" (see Shewchuk's paper) is computed recursively, | ||
| 888 | // by splitting the list of expansions to sum into two halves. | ||
| 889 | 4101758 | coord_index_t dim1 = dim / 2; | |
| 890 | 4101758 | coord_index_t dim2 = coord_index_t(dim - dim1); | |
| 891 | 4101758 | const double* p1_2 = p1 + dim1; | |
| 892 | 4101758 | const double* p2_2 = p2 + dim1; | |
| 893 | 4101758 | const double* p0_2 = p0 + dim1; | |
| 894 | 4101758 | expansion& d1 = expansion_dot_at(p1, p2, p0, dim1); | |
| 895 | 4101758 | expansion& d2 = expansion_dot_at(p1_2, p2_2, p0_2, dim2); | |
| 896 | 4101758 | this->assign_sum(d1, d2); | |
| 897 | } | ||
| 898 | 10254521 | return *this; | |
| 899 | } | ||
| 900 | |||
| 901 | ✗ | expansion& expansion::assign_length2( | |
| 902 | const expansion& x, const expansion& y, const expansion& z | ||
| 903 | ) { | ||
| 904 | ✗ | const expansion& x2 = expansion_square(x); | |
| 905 | ✗ | const expansion& y2 = expansion_square(y); | |
| 906 | ✗ | const expansion& z2 = expansion_square(z); | |
| 907 | ✗ | this->assign_sum(x2,y2,z2); | |
| 908 | ✗ | return *this; | |
| 909 | } | ||
| 910 | |||
| 911 | /************************************************************************/ | ||
| 912 | |||
| 913 |
2/2✓ Branch 0 taken 407217 times.
✓ Branch 1 taken 1152465 times.
|
1559682 | bool expansion::is_same_as(const expansion& rhs) const { |
| 914 |
2/2✓ Branch 0 taken 407217 times.
✓ Branch 1 taken 1152465 times.
|
1559682 | if(length() != rhs.length()) { |
| 915 | return false; | ||
| 916 | } | ||
| 917 |
2/2✓ Branch 0 taken 1388746 times.
✓ Branch 1 taken 747191 times.
|
2135937 | for(index_t i=0; i<length(); ++i) { |
| 918 |
2/2✓ Branch 0 taken 405274 times.
✓ Branch 1 taken 983472 times.
|
1388746 | if(x_[i] != rhs.x_[i]) { |
| 919 | return false; | ||
| 920 | } | ||
| 921 | } | ||
| 922 | return true; | ||
| 923 | } | ||
| 924 | |||
| 925 | ✗ | bool expansion::is_same_as(double rhs) const { | |
| 926 | ✗ | if(length() != 1) { | |
| 927 | return false; | ||
| 928 | } | ||
| 929 | ✗ | return (x_[0] == rhs); | |
| 930 | } | ||
| 931 | |||
| 932 |
1/2✓ Branch 0 taken 4216572 times.
✗ Branch 1 not taken.
|
4216572 | Sign expansion::compare(const expansion& rhs) const { |
| 933 | // Fast path: different signs or both zero | ||
| 934 | Sign s1 = sign(); | ||
| 935 | Sign s2 = rhs.sign(); | ||
| 936 |
2/2✓ Branch 0 taken 492609 times.
✓ Branch 1 taken 3723963 times.
|
4216572 | if(s1 == ZERO && s2 == ZERO) { |
| 937 | return ZERO; | ||
| 938 | } | ||
| 939 |
2/2✓ Branch 0 taken 2164281 times.
✓ Branch 1 taken 1559682 times.
|
3723963 | if(s1 != s2) { |
| 940 |
2/2✓ Branch 0 taken 1462001 times.
✓ Branch 1 taken 702280 times.
|
3626282 | return (int(s1) > int(s2) ? POSITIVE : NEGATIVE); |
| 941 | } | ||
| 942 | |||
| 943 | // Fast path: same internal representation | ||
| 944 |
2/2✓ Branch 1 taken 747191 times.
✓ Branch 2 taken 812491 times.
|
1559682 | if(is_same_as(rhs)) { |
| 945 | return ZERO; | ||
| 946 | } | ||
| 947 | |||
| 948 | // Compute difference and return sign of difference | ||
| 949 | index_t capa = diff_capacity(*this, rhs); | ||
| 950 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 812491 times.
|
812491 | if(capa > MAX_CAPACITY_ON_STACK) { |
| 951 | expansion* d = new_expansion_on_heap(capa); | ||
| 952 | ✗ | d->assign_diff(*this, rhs); | |
| 953 | Sign result = d->sign(); | ||
| 954 | delete_expansion_on_heap(d); | ||
| 955 | ✗ | return result; | |
| 956 | } | ||
| 957 | 812491 | const expansion& d = expansion_diff(*this, rhs); | |
| 958 | return d.sign(); | ||
| 959 | } | ||
| 960 | |||
| 961 | ✗ | Sign expansion::compare(double rhs) const { | |
| 962 | // Fast path: different signs or both zero | ||
| 963 | Sign s1 = sign(); | ||
| 964 | Sign s2 = geo_sgn(rhs); | ||
| 965 | ✗ | if(s1 == ZERO && s2 == ZERO) { | |
| 966 | return ZERO; | ||
| 967 | } | ||
| 968 | ✗ | if(s1 != s2) { | |
| 969 | ✗ | return (int(s1) > int(s2) ? POSITIVE : NEGATIVE); | |
| 970 | } | ||
| 971 | |||
| 972 | // Fast path: same internal representation | ||
| 973 | ✗ | if(is_same_as(rhs)) { | |
| 974 | return ZERO; | ||
| 975 | } | ||
| 976 | |||
| 977 | // Compute difference and return sign of difference | ||
| 978 | index_t capa = diff_capacity(*this, rhs); | ||
| 979 | ✗ | if(capa > MAX_CAPACITY_ON_STACK) { | |
| 980 | expansion* d = new_expansion_on_heap(capa); | ||
| 981 | ✗ | d->assign_diff(*this, rhs); | |
| 982 | Sign result = d->sign(); | ||
| 983 | delete_expansion_on_heap(d); | ||
| 984 | ✗ | return result; | |
| 985 | } | ||
| 986 | ✗ | const expansion& d = expansion_diff(*this, rhs); | |
| 987 | return d.sign(); | ||
| 988 | } | ||
| 989 | |||
| 990 | |||
| 991 | /************************************************************************/ | ||
| 992 | |||
| 993 | ✗ | void expansion::show_all_stats() { | |
| 994 | #ifdef PCK_STATS | ||
| 995 | // Place holder: if we compute statistics for expansions, | ||
| 996 | // the code here will be called if sys:stats is specified | ||
| 997 | // on command line. | ||
| 998 | #endif | ||
| 999 | ✗ | } | |
| 1000 | |||
| 1001 | /************************************************************************/ | ||
| 1002 | |||
| 1003 | ✗ | Sign sign_of_expansion_determinant( | |
| 1004 | const expansion& a00,const expansion& a01, | ||
| 1005 | const expansion& a10,const expansion& a11 | ||
| 1006 | ) { | ||
| 1007 | ✗ | const expansion& result = expansion_det2x2(a00, a01, a10, a11); | |
| 1008 | ✗ | return result.sign(); | |
| 1009 | } | ||
| 1010 | |||
| 1011 | ✗ | Sign sign_of_expansion_determinant( | |
| 1012 | const expansion& a00,const expansion& a01,const expansion& a02, | ||
| 1013 | const expansion& a10,const expansion& a11,const expansion& a12, | ||
| 1014 | const expansion& a20,const expansion& a21,const expansion& a22 | ||
| 1015 | ) { | ||
| 1016 | // First compute the det2x2 | ||
| 1017 | const expansion& m01 = | ||
| 1018 | ✗ | expansion_det2x2(a00, a10, a01, a11); | |
| 1019 | const expansion& m02 = | ||
| 1020 | ✗ | expansion_det2x2(a00, a20, a01, a21); | |
| 1021 | const expansion& m12 = | ||
| 1022 | ✗ | expansion_det2x2(a10, a20, a11, a21); | |
| 1023 | |||
| 1024 | // Now compute the minors of rank 3 | ||
| 1025 | ✗ | const expansion& z1 = expansion_product(m01,a22); | |
| 1026 | ✗ | const expansion& z2 = expansion_product(m02,a12).negate(); | |
| 1027 | ✗ | const expansion& z3 = expansion_product(m12,a02); | |
| 1028 | |||
| 1029 | ✗ | const expansion& result = expansion_sum3(z1,z2,z3); | |
| 1030 | ✗ | return result.sign(); | |
| 1031 | } | ||
| 1032 | |||
| 1033 | ✗ | Sign sign_of_expansion_determinant( | |
| 1034 | const expansion& a00,const expansion& a01, | ||
| 1035 | const expansion& a02,const expansion& a03, | ||
| 1036 | const expansion& a10,const expansion& a11, | ||
| 1037 | const expansion& a12,const expansion& a13, | ||
| 1038 | const expansion& a20,const expansion& a21, | ||
| 1039 | const expansion& a22,const expansion& a23, | ||
| 1040 | const expansion& a30,const expansion& a31, | ||
| 1041 | const expansion& a32,const expansion& a33 | ||
| 1042 | ) { | ||
| 1043 | |||
| 1044 | // First compute the det2x2 | ||
| 1045 | const expansion& m01 = | ||
| 1046 | ✗ | expansion_det2x2(a10,a00,a11,a01); | |
| 1047 | const expansion& m02 = | ||
| 1048 | ✗ | expansion_det2x2(a20,a00,a21,a01); | |
| 1049 | const expansion& m03 = | ||
| 1050 | ✗ | expansion_det2x2(a30,a00,a31,a01); | |
| 1051 | const expansion& m12 = | ||
| 1052 | ✗ | expansion_det2x2(a20,a10,a21,a11); | |
| 1053 | const expansion& m13 = | ||
| 1054 | ✗ | expansion_det2x2(a30,a10,a31,a11); | |
| 1055 | const expansion& m23 = | ||
| 1056 | ✗ | expansion_det2x2(a30,a20,a31,a21); | |
| 1057 | |||
| 1058 | // Now compute the minors of rank 3 | ||
| 1059 | ✗ | const expansion& m012_1 = expansion_product(m12,a02); | |
| 1060 | ✗ | expansion& m012_2 = expansion_product(m02,a12); m012_2.negate(); | |
| 1061 | ✗ | const expansion& m012_3 = expansion_product(m01,a22); | |
| 1062 | ✗ | const expansion& m012 = expansion_sum3(m012_1, m012_2, m012_3); | |
| 1063 | |||
| 1064 | ✗ | const expansion& m013_1 = expansion_product(m13,a02); | |
| 1065 | ✗ | expansion& m013_2 = expansion_product(m03,a12); m013_2.negate(); | |
| 1066 | |||
| 1067 | ✗ | const expansion& m013_3 = expansion_product(m01,a32); | |
| 1068 | ✗ | const expansion& m013 = expansion_sum3(m013_1, m013_2, m013_3); | |
| 1069 | |||
| 1070 | ✗ | const expansion& m023_1 = expansion_product(m23,a02); | |
| 1071 | ✗ | expansion& m023_2 = expansion_product(m03,a22); m023_2.negate(); | |
| 1072 | ✗ | const expansion& m023_3 = expansion_product(m02,a32); | |
| 1073 | ✗ | const expansion& m023 = expansion_sum3(m023_1, m023_2, m023_3); | |
| 1074 | |||
| 1075 | ✗ | const expansion& m123_1 = expansion_product(m23,a12); | |
| 1076 | ✗ | expansion& m123_2 = expansion_product(m13,a22); m123_2.negate(); | |
| 1077 | ✗ | const expansion& m123_3 = expansion_product(m12,a32); | |
| 1078 | ✗ | const expansion& m123 = expansion_sum3(m123_1, m123_2, m123_3); | |
| 1079 | |||
| 1080 | // Now compute the minors of rank 4 | ||
| 1081 | ✗ | const expansion& m0123_1 = expansion_product(m123,a03); | |
| 1082 | ✗ | const expansion& m0123_2 = expansion_product(m023,a13); | |
| 1083 | ✗ | const expansion& m0123_3 = expansion_product(m013,a23); | |
| 1084 | ✗ | const expansion& m0123_4 = expansion_product(m012,a33); | |
| 1085 | |||
| 1086 | ✗ | const expansion& z1 = expansion_sum(m0123_1, m0123_3); | |
| 1087 | ✗ | const expansion& z2 = expansion_sum(m0123_2, m0123_4); | |
| 1088 | |||
| 1089 | ✗ | const expansion& result = expansion_diff(z1,z2); | |
| 1090 | ✗ | return result.sign(); | |
| 1091 | } | ||
| 1092 | |||
| 1093 | /************************************************************************/ | ||
| 1094 | |||
| 1095 | 2290279 | void expansion::optimize() { | |
| 1096 | 2290279 | compress_expansion(*this); | |
| 1097 | 2290279 | } | |
| 1098 | |||
| 1099 | /************************************************************************/ | ||
| 1100 | |||
| 1101 | } | ||
| 1102 |