1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
|
// Copyright (c) 1998-2019
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Number_types/include/CGAL/Interval_nt.h $
// $Id: include/CGAL/Interval_nt.h 08b27d3db14 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Sylvain Pion, Michael Hemmer, Marc Glisse
#ifndef CGAL_INTERVAL_NT_H
#define CGAL_INTERVAL_NT_H
// This file contains the description of the following classes:
// - Interval_nt<false> It's a number type that needs the FPU rounding mode
// to be set to +inf. It is also typedef'd to
// Interval_nt_advanced for backward compatibility.
// - Interval_nt<true> Same but it does the rounding mode itself so you
// don't have to worry about it. But it's slower.
//
// Note: When rounding is towards +infinity, to make an operation rounded
// towards -infinity, it's enough to take the opposite of some of the operand,
// and the opposite of the result (see operator+, operator*,...).
// TODO :
// - test whether stopping constant propagation only in functions taking
// double as arguments, improves performance.
#include <utility> // for std::pair
#include <CGAL/number_type_config.h>
#include <CGAL/number_utils.h>
#include <CGAL/utils_classes.h>
#include <CGAL/number_utils.h>
#include <CGAL/Uncertain.h>
#include <CGAL/Interval_traits.h>
#include <CGAL/double.h>
#include <CGAL/FPU.h>
#include <CGAL/IO/io.h>
#include <iostream>
#include <boost/operators.hpp>
#ifdef __GNUC__
// gcc's __builtin_constant_p does not like arguments with side effects. Be
// careful not to use this macro for something that the compiler will have
// trouble eliminating as dead code.
# define CGAL_CST_TRUE(X) __extension__ ({ bool _ugly_ = (X); __builtin_constant_p(_ugly_) && _ugly_; })
#else
# define CGAL_CST_TRUE(X) false
#endif
namespace CGAL {
template <bool Protected = true>
class Interval_nt
{
typedef Interval_nt<Protected> IA;
typedef std::pair<double, double> Pair;
public:
typedef double value_type;
typedef Uncertain_conversion_exception unsafe_comparison;
typedef Checked_protect_FPU_rounding<Protected> Internal_protector;
typedef Protect_FPU_rounding<!Protected> Protector;
Interval_nt()
#ifndef CGAL_NO_ASSERTIONS
# ifdef CGAL_USE_SSE2
: val(_mm_setr_pd(-1, 0))
# else
: _inf(-1), _sup(0)
# endif
// to early and deterministically detect use of uninitialized
#endif
{}
Interval_nt(int i)
{ *this = static_cast<double>(i); }
Interval_nt(unsigned i)
{ *this = static_cast<double>(i); }
Interval_nt(long long i)
{
// gcc ignores -frounding-math when converting integers to floats.
// Is this safe against excess precision? -- Marc Glisse, Dec 2012
double d = static_cast<double>(i);
*this = d;
#ifdef __GNUC__
long long safe = 1LL << 52; // Use numeric_limits?
bool exact = ((long long)d == i) || (i <= safe && i >= -safe);
if (!CGAL_CST_TRUE(exact))
#endif
*this += smallest();
}
Interval_nt(unsigned long long i)
{
double d = static_cast<double>(i);
*this = d;
#ifdef __GNUC__
unsigned long long safe = 1ULL << 52; // Use numeric_limits?
bool exact = ((unsigned long long)d == i) || (i <= safe);
if (!CGAL_CST_TRUE(exact))
#endif
// This requires a suitable rounding mode, which we always set for
// arithmetic, but not always for a conversion...
*this += smallest();
}
Interval_nt(long i)
{
*this = (sizeof(int)==sizeof(long)) ?
Interval_nt((int)i) :
Interval_nt((long long)i);
}
Interval_nt(unsigned long i)
{
*this = (sizeof(int)==sizeof(long)) ?
Interval_nt((unsigned)i) :
Interval_nt((unsigned long long)i);
}
Interval_nt(double d)
{
CGAL_assertion(is_finite(d));
*this = Interval_nt(d, d);
}
// The Intel compiler on Linux is aggressive with constant propagation and
// it seems there is no flag to stop it, so disable this check for it.
#if !defined(CGAL_DISABLE_ROUNDING_MATH_CHECK) && \
defined(__INTEL_COMPILER) && defined(__linux)
# define CGAL_DISABLE_ROUNDING_MATH_CHECK
#endif
#ifdef CGAL_USE_SSE2
// This constructor should really be private, like the simd() function, but
// that would mean a lot of new friends, so they are only undocumented.
explicit Interval_nt(__m128d v) : val(v) {}
#endif
// Unchecked version for Lazy_rep in Lazy.h.
struct no_check_t {};
Interval_nt(double i, double s, no_check_t)
#ifdef CGAL_USE_SSE2
: val(_mm_setr_pd(-i, s))
#else
: _inf(-i), _sup(s)
#endif
{
#ifndef CGAL_DISABLE_ROUNDING_MATH_CHECK
CGAL_assertion_code((void) tester;) // Necessary to trigger a runtime test of rounding modes.
#endif
}
Interval_nt(double i, double s) : Interval_nt(i, s, no_check_t())
{
// Previously it was:
// CGAL_assertion_msg(!(i>s);
// But MSVC++ 2012 optimizes the test "!(i>s)" to "i<=s", even with
// /fp:strict. If 'i' or 's' is a NaN, that makes a difference.
CGAL_assertion_msg( (!is_valid(i)) || (!is_valid(s)) || (!(i>s)),
" Variable used before being initialized (or CGAL bug)");
}
Interval_nt(const Pair & p)
{ *this = Interval_nt(p.first, p.second); }
IA operator-() const
{
#ifdef CGAL_USE_SSE2
return IA (swap_m128d(val));
#else
return IA (-sup(), -inf());
#endif
}
IA & operator+= (const IA &d) { return *this = *this + d; }
IA & operator-= (const IA &d) { return *this = *this - d; }
IA & operator*= (const IA &d) { return *this = *this * d; }
IA & operator/= (const IA &d) { return *this = *this / d; }
bool is_point() const
{
return sup() == inf();
}
bool is_same (const IA & d) const
{
#ifdef CGAL_USE_SSE2
// Faster to answer yes, but slower to answer no.
return _mm_movemask_pd (_mm_cmpneq_pd (val, d.val)) == 0;
#else
return inf() == d.inf() && sup() == d.sup();
#endif
}
bool do_overlap (const IA & d) const
{
#ifdef CGAL_USE_SSE2
__m128d m = _mm_set1_pd (-0.);
__m128d y = _mm_xor_pd ((-d).val, m); // {-ds,di}
__m128d c = _mm_cmplt_pd (val, y); // {i>ds,s<di}
return _mm_movemask_pd (c) == 0;
#else
return !(d.inf() > sup() || d.sup() < inf());
#endif
}
double inf() const
{
#ifdef CGAL_USE_SSE2
return -_mm_cvtsd_f64(val);
#else
return -_inf;
#endif
}
double sup() const
{
#ifdef CGAL_USE_SSE2
return _mm_cvtsd_f64(swap_m128d(val));
// The following is a bit more natural, but
// - it is too opaque
// - it is a less likely CSE candidate
// return _mm_cvtsd_f64(_mm_unpackhi_pd(val, val));
#else
return _sup;
#endif
}
#ifdef CGAL_USE_SSE2
__m128d simd() const { return val; }
#endif
std::pair<double, double> pair() const
{
return std::pair<double, double>(inf(), sup());
}
static IA largest()
{
return IA(-std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
}
static IA smallest()
{
return IA(-CGAL_IA_MIN_DOUBLE, CGAL_IA_MIN_DOUBLE);
}
#if 0 // def CGAL_HISTOGRAM_PROFILER // not yet ready
~Interval_nt()
{
CGAL_HISTOGRAM_PROFILER("[Interval_nt relative precision in log2 scale]",
(unsigned) ( ::log(relative_precision(*this))) / ::log(2.0) ) );
}
#endif
private:
// Pair inf_sup;
// The value stored in _inf is the negated lower bound.
// TODO: experiment with different orders of the values in the SSE2 register,
// for instance {sup, -inf}, or {inf, -sup}, and adapt users to query the low
// value in priority. {-inf, sup} has the drawback that neither inf nor sup
// is free to access.
#ifdef CGAL_USE_SSE2
__m128d val;
#else
double _inf, _sup;
#endif
struct Test_runtime_rounding_modes {
Test_runtime_rounding_modes()
{
// We test whether GCC's -frounding-math option has been forgotten.
// The macros CGAL_IA_MUL and CGAL_IA_DIV stop constant propagation only
// on the second argument, so if -fno-rounding-math, the compiler optimizes
// the 2 negations and we get wrong rounding.
typename Interval_nt<>::Internal_protector P;
CGAL_assertion_msg(-CGAL_IA_MUL(-1.1, 10.1) != CGAL_IA_MUL(1.1, 10.1),
"Wrong rounding: did you forget the -frounding-math option if you use GCC (or -fp-model=strict for Intel)?");
CGAL_assertion_msg(-CGAL_IA_DIV(-1., 10) != CGAL_IA_DIV(1., 10),
"Wrong rounding: did you forget the -frounding-math option if you use GCC (or -fp-model=strict for Intel)?");
}
};
#ifndef CGAL_DISABLE_ROUNDING_MATH_CHECK
static const Test_runtime_rounding_modes tester;
#endif
friend
Uncertain<bool>
operator<(const Interval_nt &a, const Interval_nt &b)
{
if (a.sup() < b.inf()) return true;
if (a.inf() >= b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>(const Interval_nt &a, const Interval_nt &b)
{ return b < a; }
friend
Uncertain<bool>
operator<=(const Interval_nt &a, const Interval_nt &b)
{
if (a.sup() <= b.inf()) return true;
if (a.inf() > b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>=(const Interval_nt &a, const Interval_nt &b)
{ return b <= a; }
friend
Uncertain<bool>
operator==(const Interval_nt &a, const Interval_nt &b)
{
if (b.inf() > a.sup() || b.sup() < a.inf()) return false;
if (b.inf() == a.sup() && b.sup() == a.inf()) return true;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator!=(const Interval_nt &a, const Interval_nt &b)
{ return ! (a == b); }
// Mixed operators with double.
friend
Uncertain<bool>
operator<(double a, const Interval_nt &b)
{
if (a < b.inf()) return true;
if (a >= b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>(double a, const Interval_nt &b)
{ return b < a; }
friend
Uncertain<bool>
operator<=(double a, const Interval_nt &b)
{
if (a <= b.inf()) return true;
if (a > b.sup()) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>=(double a, const Interval_nt &b)
{ return b <= a; }
friend
Uncertain<bool>
operator==(double a, const Interval_nt &b)
{
if (b.inf() > a || b.sup() < a) return false;
if (b.is_point()) return true;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator!=(double a, const Interval_nt &b)
{ return ! (a == b); }
friend
Uncertain<bool>
operator<(const Interval_nt &a, double b)
{
if (a.sup() < b) return true;
if (a.inf() >= b) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>(const Interval_nt &a, double b)
{ return b < a; }
friend
Uncertain<bool>
operator<=(const Interval_nt &a, double b)
{
if (a.sup() <= b) return true;
if (a.inf() > b) return false;
return Uncertain<bool>::indeterminate();
}
friend
Uncertain<bool>
operator>=(const Interval_nt &a, double b)
{ return b <= a; }
friend
Uncertain<bool>
operator==(const Interval_nt &a, double b)
{
return b == a;
}
friend
Uncertain<bool>
operator!=(const Interval_nt &a, double b)
{ return b != a; }
friend
std::ostream & operator<< (std::ostream &os, const Interval_nt & I )
{
return os << "[" << I.inf() << ";" << I.sup() << "]";
}
#define CGAL_SWALLOW(IS,CHAR) \
{ \
char c; \
do is.get(c); while (isspace(c)); \
if (c != CHAR) { \
is.setstate(std::ios_base::failbit); \
} \
} \
friend
std::istream & operator>> (std::istream &is, Interval_nt & I)
{
char c;
do is.get(c); while (isspace(c));
is.putback(c);
if(c == '['){ // read original output from operator <<
double inf,sup;
CGAL_SWALLOW(is, '[');// read the "["
is >> IO::iformat(inf);
CGAL_SWALLOW(is, ';');// read the ";"
is >> IO::iformat(sup);
CGAL_SWALLOW(is, ']');// read the "]"
I = Interval_nt(inf,sup);
}else{ //read double (backward compatibility)
double d;
is >> d;
I = d;
}
return is;
}
#undef CGAL_SWALLOW
friend
Interval_nt
operator+ (const Interval_nt &a, const Interval_nt & b)
{
Internal_protector P;
#ifdef CGAL_USE_SSE2
__m128d aa = IA_opacify128(a.simd());
__m128d bb = IA_opacify128_weak(b.simd());
__m128d r = _mm_add_pd(aa, bb);
return Interval_nt(IA_opacify128(r));
#else
return Interval_nt (-CGAL_IA_ADD(-a.inf(), -b.inf()),
CGAL_IA_ADD(a.sup(), b.sup()));
#endif
}
// MSVC does not define __SSE3__
#if defined CGAL_USE_SSE2 && (defined __SSE3__ || defined __AVX__)
friend
Interval_nt
operator+ (double a, const Interval_nt & b)
{
Internal_protector P;
__m128d aa = _mm_set1_pd(IA_opacify(a));
__m128d bb = IA_opacify128_weak(b.simd());
__m128d r = _mm_addsub_pd(bb, aa);
return Interval_nt(IA_opacify128(r));
}
friend
Interval_nt
operator+ (const Interval_nt & a, double b)
{
return b + a;
}
#endif
friend
Interval_nt
operator+( const Interval_nt& a ) {
return a;
}
friend
Interval_nt
operator- (const Interval_nt &a, const Interval_nt & b)
{
#ifdef CGAL_USE_SSE2
return a+-b;
#else
Internal_protector P;
return Interval_nt(-CGAL_IA_ADD(b.sup(), -a.inf()),
CGAL_IA_ADD(a.sup(), -b.inf()));
#endif
}
#ifdef CGAL_USE_SSE2
friend
Interval_nt
operator- (double a, const Interval_nt & b)
{
return a+-b;
}
#endif
friend
Interval_nt
operator* (const Interval_nt &a, const Interval_nt & b)
{
#if 0
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88626
if(CGAL_CST_TRUE(a.is_point()))
return a.inf() * b;
else if(CGAL_CST_TRUE(b.is_point()))
return a * b.inf();
#endif
Internal_protector P;
#ifdef CGAL_USE_SSE2
# if !defined __SSE4_1__ && !defined __AVX__
// Brutal, compute all products in all directions.
// The actual winner (by a hair) on recent hardware before removing NaNs.
__m128d aa = IA_opacify128_weak(a.simd()); // {-ai,as}
__m128d bb = b.simd(); // {-bi,bs}
__m128d m = _mm_set_sd(-0.); // {-0,+0}
__m128d m1 = _mm_set1_pd(-0.); // {-0,-0}
__m128d ax = swap_m128d (aa); // {as,-ai}
__m128d ap = _mm_xor_pd (ax, m1); // {-as,ai}
__m128d bz = _mm_xor_pd(bb, m); // {bi,bs}
bz = IA_opacify128(bz);
__m128d c = swap_m128d (bz); // {bs,bi}
// The multiplications could produce some NaN, with 0 * inf. Replacing it with inf is safe.
// min(x,y) (the order is essential) returns its second argument when the first is NaN.
// An IEEE 754-2019 maximum could help.
__m128d big = IA::largest().simd();
__m128d x1 = _mm_mul_pd(aa,bz); // {-ai*bi,as*bs}
//x1 = _mm_min_pd(x1,big); // no NaN
__m128d x2 = _mm_mul_pd(aa,c); // {-ai*bs,as*bi}
x2 = _mm_min_pd(x2,big); // no NaN
__m128d x3 = _mm_mul_pd(ap,bz); // {-as*bi,ai*bs}
//x3 = _mm_min_pd(x3,big); // no NaN
__m128d x4 = _mm_mul_pd(ap,c); // {-as*bs,ai*bi}
x4 = _mm_min_pd(x4,big); // no NaN
__m128d y1 = _mm_max_pd(x1,x2);
__m128d y2 = _mm_max_pd(x3,x4);
__m128d r = _mm_max_pd (y1, y2);
// Alternative with fewer instructions but more dependency
// __m128d r = _mm_max_pd(x1,_mm_max_pd(x2,_mm_max_pd(x3,_mm_min_pd(x4,big))));
return IA (IA_opacify128(r));
# elif 1
// we want to multiply ai,as with {ai<0?-bs:-bi,as<0?bi:bs}
// we want to multiply as,ai with {as<0?-bs:-bi,ai<0?bi:bs}
// requires SSE4 (otherwise use _mm_cmplt_pd, _mm_and_pd, _mm_andnot_pd and _mm_or_pd to avoid blendv)
// probably faster on older hardware
__m128d m = _mm_set_sd(-0.); // {-0,+0}
__m128d m1 = _mm_set1_pd(-0.); // {-0,-0}
__m128d big = IA::largest().simd();
__m128d aa = a.simd(); // {-ai,as}
__m128d az = _mm_xor_pd(aa, m); // {ai,as}
az = IA_opacify128_weak(az);
__m128d azp = swap_m128d (az); // {as,ai}
__m128d bb = IA_opacify128(b.simd()); // {-bi,bs}
__m128d bx = swap_m128d (bb); // {bs,-bi}
__m128d bp = _mm_xor_pd(bx, m1); // {-bs,bi}
__m128d x = _mm_blendv_pd (bb, bp, az); // {ai<0?-bs:-bi,as<0?bi:bs}
__m128d y = _mm_blendv_pd (bb, bp, azp); // {as<0?-bs:-bi,ai<0?bi:bs}
__m128d p1 = _mm_mul_pd (az, x);
//p1 = _mm_min_pd(p1,big); // no NaN
__m128d p2 = _mm_mul_pd (azp, y);
p2 = _mm_min_pd(p2,big); // no NaN
__m128d r = _mm_max_pd (p1, p2);
return IA (IA_opacify128(r));
# elif 0
// we want to multiply -ai,as with {ai>0?bi:bs,as<0?bi:bs}
// we want to multiply -as,ai with {as<0?bs:bi,ai>0?bs:bi}
// slightly worse than the previous one
__m128d m1 = _mm_set1_pd(-0.); // {-0,-0}
__m128d big = IA::largest().simd();
__m128d aa = IA_opacify128_weak(a.simd()); // {-ai,as}
__m128d ax = swap_m128d (aa); // {as,-ai}
__m128d ap = _mm_xor_pd (ax, m1); // {-as,ai}
__m128d bb = IA_opacify128(b.simd()); // {-bi,bs}
double bi = -_mm_cvtsd_f64(bb);
double bs = _mm_cvtsd_f64(_mm_unpackhi_pd(bb,bb));
__m128d bbi = _mm_set1_pd(bi); // {bi,bi}
__m128d bbs = _mm_set1_pd(bs); // {bs,bs}
__m128d x = _mm_blendv_pd (bbs, bbi, aa); // {ai>0?bi:bs,as<0?bi:bs}
__m128d y = _mm_blendv_pd (bbi, bbs, ax); // {as<0?bs:bi,ai>0?bs:bi}
__m128d p1 = _mm_mul_pd (aa, x);
//p1 = _mm_min_pd(p1,big); // no NaN
__m128d p2 = _mm_mul_pd (ap, y);
p2 = _mm_min_pd(p2,big); // no NaN
__m128d r = _mm_max_pd (p1, p2);
return IA (IA_opacify128(r));
# else
// AVX version of the brutal method, same running time or slower
__m128d aa = IA_opacify128_weak(a.simd()); // {-ai,as}
__m128d bb = b.simd(); // {-bi,bs}
__m256d big = _mm256_set1_pd(std::numeric_limits<double>::infinity());
__m128d m = _mm_set_sd(-0.); // {-0,+0}
__m128d m1 = _mm_set1_pd(-0.); // {-0,-0}
__m128d ax = swap_m128d (aa); // {as,-ai}
__m128d ap = _mm_xor_pd (ax, m1); // {-as,ai}
__m128d bz = _mm_xor_pd(bb, m); // {bi,bs}
bz = IA_opacify128(bz);
__m256d X = _mm256_set_m128d(ap,aa); // {-ai,as,-as,ai}
__m256d Y1 = _mm256_set_m128d(bz,bz); // {bi,bs,bi,bs}
__m256d Y2 = _mm256_permute_pd(Y1,5); // {bs,bi,bs,bi}
__m256d Z1 = _mm256_mul_pd(X,Y1);
//Z1 = _mm256_min_pd(Z1,big); // no NaN
__m256d Z2 = _mm256_mul_pd(X,Y2);
Z2 = _mm256_min_pd(Z2,big); // no NaN
__m256d Z = _mm256_max_pd(Z1,Z2);
__m128d z1 = _mm256_castpd256_pd128(Z);
__m128d z2 = _mm256_extractf128_pd(Z,1);
__m128d r = _mm_max_pd (z1, z2);
return IA (IA_opacify128(r));
# endif
#else
// TODO: try to move some NaN tests out of the hot path (test a.inf()>0 instead of >=0?).
if (a.inf() >= 0.0) // a>=0
{
// b>=0 [a.inf()*b.inf(); a.sup()*b.sup()]
// b<=0 [a.sup()*b.inf(); a.inf()*b.sup()]
// b~=0 [a.sup()*b.inf(); a.sup()*b.sup()]
double aa = a.inf(), bb = a.sup();
if (bb <= 0.) return 0.; // In case b has an infinite bound, avoid NaN.
if (b.inf() < 0.0)
{
aa = bb;
if (b.sup() < 0.0)
bb = a.inf();
}
double r = (b.sup() == 0) ? 0. : CGAL_IA_MUL(bb, b.sup()); // In case bb is infinite, avoid NaN.
return IA(-CGAL_IA_MUL(aa, -b.inf()), r);
}
else if (a.sup()<=0.0) // a<=0
{
// b>=0 [a.inf()*b.sup(); a.sup()*b.inf()]
// b<=0 [a.sup()*b.sup(); a.inf()*b.inf()]
// b~=0 [a.inf()*b.sup(); a.inf()*b.inf()]
double aa = a.sup(), bb = a.inf();
if (b.inf() < 0.0)
{
aa=bb;
if (b.sup() <= 0.0)
bb=a.sup();
}
else if (b.sup() <= 0) return 0.; // In case a has an infinite bound, avoid NaN.
return IA(-CGAL_IA_MUL(-bb, b.sup()), CGAL_IA_MUL(-aa, -b.inf()));
}
else // 0 \in a
{
if (b.inf()>=0.0) { // b>=0
if (b.sup()<=0.0)
return 0.; // In case a has an infinite bound, avoid NaN.
else
return IA(-CGAL_IA_MUL(-a.inf(), b.sup()),
CGAL_IA_MUL( a.sup(), b.sup()));
}
if (b.sup()<=0.0) { // b<=0
return IA(-CGAL_IA_MUL( a.sup(), -b.inf()),
CGAL_IA_MUL(-a.inf(), -b.inf()));
}
// 0 \in b
double tmp1 = CGAL_IA_MUL(-a.inf(), b.sup());
double tmp2 = CGAL_IA_MUL( a.sup(), -b.inf());
double tmp3 = CGAL_IA_MUL(-a.inf(), -b.inf());
double tmp4 = CGAL_IA_MUL( a.sup(), b.sup());
return IA(-(std::max)(tmp1,tmp2), (std::max)(tmp3,tmp4));
}
#endif
}
friend
Interval_nt
operator* (double a, Interval_nt b)
{
CGAL_assertion(is_finite(a));
// return Interval_nt(a)*b;
Internal_protector P;
if (a < 0) { a = -a; b = -b; }
// Now a >= 0
#ifdef CGAL_USE_SSE2
// TODO: try/benchmark a branchless version
__m128d bb = IA_opacify128_weak(b.simd());
__m128d aa = _mm_set1_pd(IA_opacify(a));
__m128d r = _mm_mul_pd(aa, bb);
// In case a is 0 and b has an infinite bound. This returns an interval
// larger than necessary, but is likely faster to produce.
r = _mm_min_pd(r,largest().simd());
return IA(IA_opacify128(r));
#else
else if (!(a > 0)) return 0.; // We could test this before the SSE block and remove the minpd line.
return IA(-CGAL_IA_MUL(a, -b.inf()), CGAL_IA_MUL(a, b.sup()));
#endif
}
friend
Interval_nt
operator* (const Interval_nt & a, double b)
{
return b * a;
}
friend
Interval_nt
operator/ (const Interval_nt &a, const Interval_nt & b)
{
#if 0
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88626
if(CGAL_CST_TRUE(a.is_point()))
return a.inf() / b;
else if(CGAL_CST_TRUE(b.is_point()))
return a / b.inf();
#endif
Internal_protector P;
#if defined CGAL_USE_SSE2 && (defined __SSE4_1__ || defined __AVX__)
//// not a tight bound, but easy:
// return CGAL::inverse(b)*a;
# if 1
// Current fastest
// if b>0 we want [ai/(ai>0?bs:bi),as/(as>0?bi:bs)]
// if b<0 we want [as/(as>0?bs:bi),ai/(ai>0?bi:bs)]
__m128d m = _mm_set_sd(-0.);
__m128d aa = a.simd();
__m128d bb = b.simd();
int i = _mm_movemask_pd(_mm_cmpge_pd(bb, _mm_set1_pd(0.)));
if(i==3) return largest(); // bi<=0 && bs>=0
__m128d ap = _mm_xor_pd(aa, m); // {ai, as}
__m128d ax = swap_m128d(ap); // {as, ai}
__m128d bp = _mm_xor_pd(bb, m); // {bi, bs}
__m128d bx = swap_m128d(bp); // {bs, bi}
__m128d num = _mm_blendv_pd(ap, ax, bp); // {(b>0)?ai:as, (b>0)?as:ai}
__m128d d = _mm_blendv_pd(bx, bp, num);
// Can we rearrange things so we need fewer xor?
__m128d den = _mm_xor_pd(d, m);
num = IA_opacify128_weak(num);
den = IA_opacify128(den);
__m128d r = _mm_div_pd(num, den);
return IA (IA_opacify128(r));
# else
// Similar to the multiplication, but slow, because divisions are slow
// if b>0 we want [-max(-ai/bi,-ai/bs),max(as/bi,as/bs)] {-ai,as}/{bi,bs} {-ai,as}/{bs,bi}
// if b<0 we want [-max(-as/bi,-as/bs),max(ai/bi,ai/bs)] {-as,ai}/{bi,bs} {-as,ai}/{bs,bi}
__m128d m = _mm_set_sd(-0.);
__m128d m1 = _mm_set1_pd(-0.);
__m128d aa = a.simd(); // {-ai, as}
__m128d bb = b.simd(); // {-bi, bs}
int i = _mm_movemask_pd(_mm_cmpge_pd(bb, _mm_set1_pd(0.)));
if(i==3) return largest(); // bi<=0 && bs>=0
__m128d ap = _mm_xor_pd(aa, m1); // {ai, -as}
__m128d ax = swap_m128d(ap); // {-as, ai}
__m128d bp = _mm_xor_pd(bb, m); // {bi, bs}
__m128d num = _mm_blendv_pd(aa, ax, bp);
num = IA_opacify128_weak(num);
bp = IA_opacify128(bp);
__m128d bx = swap_m128d(bp); // {bs, bi}
__m128d d1 = _mm_div_pd(num, bp);
__m128d d2 = _mm_div_pd(num, bx);
__m128d r = _mm_max_pd(d1, d2);
return IA (IA_opacify128(r));
# endif
#else
if (b.inf() > 0.0) // b>0
{
// e>=0 [a.inf()/b.sup(); a.sup()/b.inf()]
// e<=0 [a.inf()/b.inf(); a.sup()/b.sup()]
// e~=0 [a.inf()/b.inf(); a.sup()/b.inf()]
double aa = b.sup(), bb = b.inf();
if (a.inf() < 0.0)
{
aa = bb;
if (a.sup() < 0.0)
bb = b.sup();
}
return IA(-CGAL_IA_DIV(-a.inf(), aa), CGAL_IA_DIV(a.sup(), bb));
}
else if (b.sup()<0.0) // b<0
{
// e>=0 [a.sup()/b.sup(); a.inf()/b.inf()]
// e<=0 [a.sup()/b.inf(); a.inf()/b.sup()]
// e~=0 [a.sup()/b.sup(); a.inf()/b.sup()]
double aa = b.sup(), bb = b.inf();
if (a.inf() < 0.0)
{
bb = aa;
if (a.sup() < 0.0)
aa = b.inf();
}
return IA(-CGAL_IA_DIV(a.sup(), -aa), CGAL_IA_DIV(a.inf(), bb));
}
else // b~0
return largest();
// We could do slightly better -> [0;infinity] when b.sup()==0,
// but is this worth ?
#endif
}
// Without SSE2, let it use the function above.
#ifdef CGAL_USE_SSE2
friend
Interval_nt
operator/ (double a, const Interval_nt & b)
{
int i = _mm_movemask_pd(_mm_cmpge_pd(b.simd(), _mm_set1_pd(0.)));
if(i==3) return largest(); // bi<=0 && bs>=0
__m128d aa, xx;
if(a>0){
aa = _mm_set1_pd(-a);
xx = (-b).simd();
} else if(a<0){
aa = _mm_set1_pd(a);
xx = b.simd();
} else return 0.;
Internal_protector P;
__m128d r = _mm_div_pd(IA_opacify128_weak(aa), IA_opacify128(xx));
return Interval_nt(IA_opacify128(r));
}
friend
Interval_nt
operator/ (Interval_nt a, double b)
{
if(b<0){ a = -a; b = -b; }
else if(b==0) return largest();
// Now b > 0
Internal_protector P;
# ifdef __GNUC__
// Paradoxically, constants should be safe, and this lets the compiler optimize x/2 to x*.5
if (!__builtin_constant_p(b))
# endif
b = IA_opacify(b);
__m128d bb = _mm_set1_pd(b);
__m128d aa = IA_opacify128(a.simd());
__m128d r = _mm_div_pd(aa, bb);
return Interval_nt(IA_opacify128(r));
}
#endif
};
#ifndef CGAL_DISABLE_ROUNDING_MATH_CHECK
template <bool Protected>
const typename Interval_nt<Protected>::Test_runtime_rounding_modes
Interval_nt<Protected>::tester;
#endif
// Non-documented
// Returns true if the interval is a unique representable double.
template <bool Protected>
inline
bool
fit_in_double (const Interval_nt<Protected> & d, double &r)
{
bool b = d.is_point();
if (b)
r = d.inf();
return b;
}
// Non-documented
template <bool Protected>
inline
bool
is_singleton (const Interval_nt<Protected> & d)
{
return d.is_point();
}
// Non-documented
template <bool Protected>
inline
double
magnitude (const Interval_nt<Protected> & d)
{
#ifdef CGAL_USE_SSE2
const __m128d m = _mm_castsi128_pd (_mm_set1_epi64x (0x7fffffffffffffff));
__m128d x = _mm_and_pd (d.simd(), m); // { abs(inf), abs(sup) }
__m128d y = _mm_unpackhi_pd (x, x);
return _mm_cvtsd_f64 (_mm_max_sd (x, y));
#else
return (std::max)(CGAL::abs(d.inf()), CGAL::abs(d.sup()));
#endif
}
// Non-documented
template <bool Protected>
inline
double
width (const Interval_nt<Protected> & d)
{
return d.sup() - d.inf();
}
// Non-documented
template <bool Protected>
inline
double
radius (const Interval_nt<Protected> & d)
{
return width(d)/2; // This could be improved to avoid overflow.
}
// Non-documented
// This is the relative precision of to_double() (the center of the interval),
// hence we use radius() instead of width().
template <bool Protected>
inline
bool
has_smaller_relative_precision(const Interval_nt<Protected> & d, double prec)
{
return magnitude(d) == 0 || radius(d) < prec * magnitude(d);
}
// Non-documented
template <bool Protected>
double
relative_precision(const Interval_nt<Protected> & d)
{
if (magnitude(d) == 0.0)
return 0.0;
return radius(d) / magnitude(d);
}
template< bool Protected >
class Is_valid< Interval_nt<Protected> >
: public CGAL::cpp98::unary_function< Interval_nt<Protected>, bool > {
public :
bool operator()( const Interval_nt<Protected>& x ) const {
return is_valid(-x.inf()) &&
is_valid(x.sup()) &&
x.inf() <= x.sup();
}
};
typedef Interval_nt<false> Interval_nt_advanced; // for backward-compatibility
// TODO: What about these two guys? Where do they belong to?
template <bool Protected>
struct Min <Interval_nt<Protected> >
: public CGAL::cpp98::binary_function<Interval_nt<Protected>,
Interval_nt<Protected>,
Interval_nt<Protected> >
{
Interval_nt<Protected> operator()( const Interval_nt<Protected>& d,
const Interval_nt<Protected>& e) const
{
#ifdef CGAL_USE_SSE2
__m128d x = _mm_min_pd (d.simd(), e.simd());
// Use _mm_max_sd instead?
__m128d y = _mm_max_pd (d.simd(), e.simd());
return Interval_nt<Protected> (_mm_move_sd (x, y));
#else
return Interval_nt<Protected>(
-(std::max)(-d.inf(), -e.inf()),
(std::min)( d.sup(), e.sup()));
#endif
}
};
template <bool Protected>
struct Max <Interval_nt<Protected> >
: public CGAL::cpp98::binary_function<Interval_nt<Protected>,
Interval_nt<Protected>,
Interval_nt<Protected> >
{
Interval_nt<Protected> operator()( const Interval_nt<Protected>& d,
const Interval_nt<Protected>& e) const
{
#ifdef CGAL_USE_SSE2
// Use _mm_min_sd instead?
__m128d x = _mm_min_pd (d.simd(), e.simd());
__m128d y = _mm_max_pd (d.simd(), e.simd());
return Interval_nt<Protected> (_mm_move_sd (y, x));
#else
return Interval_nt<Protected>(
-(std::min)(-d.inf(), -e.inf()),
(std::max)( d.sup(), e.sup()));
#endif
}
};
template<bool Protected> inline
Interval_nt<Protected> min BOOST_PREVENT_MACRO_SUBSTITUTION(
const Interval_nt<Protected> & x,
const Interval_nt<Protected> & y){
return CGAL::Min<Interval_nt<Protected> > ()(x,y);
}
template<bool Protected> inline
Interval_nt<Protected> max BOOST_PREVENT_MACRO_SUBSTITUTION(
const Interval_nt<Protected> & x,
const Interval_nt<Protected> & y){
return CGAL::Max<Interval_nt<Protected> > ()(x,y);
}
// TODO : document, when we are OK with the interface.
// - should it allow other number types for the exponent ?
template < bool b >
Interval_nt<b>
ldexp(const Interval_nt<b> &i, int e)
{
double scale = std::ldexp(1.0, e);
Interval_nt<b> scale_interval (
CGAL_NTS is_finite(scale) ? scale : CGAL_IA_MAX_DOUBLE,
scale == 0 ? CGAL_IA_MIN_DOUBLE : scale);
return i * scale_interval;
}
// We also specialize some corresponding functors returning Uncertain<>.
// TODO: To which concept do these functors belong? Can we remove them??
template < bool b >
struct Equal_to < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x == y; }
};
template < bool b >
struct Not_equal_to < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x != y; }
};
template < bool b >
struct Greater < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x > y; }
};
template < bool b >
struct Less < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x < y; }
};
template < bool b >
struct Greater_equal < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x >= y; }
};
template < bool b >
struct Less_equal < Interval_nt<b>, Interval_nt<b> >
: public CGAL::cpp98::binary_function< Interval_nt<b>, Interval_nt<b>, Uncertain<bool> >
{
Uncertain<bool> operator()( const Interval_nt<b>& x,
const Interval_nt<b>& y) const
{ return x <= y; }
};
// As in MP_float.h, the namespace INTERN_INTERVAL_NT contains (now) global
// functions like square or sqrt which would have collided with the new
// global functions from AST/RET
//
// TODO: IMHO, a better solution would be to put the INTERN_MP_FLOAT-functions
// into the MP_Float-class... But there is surely a reason why this is not
// the case..?
namespace INTERN_INTERVAL_NT {
template <bool Protected>
inline
double
to_double (const Interval_nt<Protected> & d)
{
return (d.sup() + d.inf()) * 0.5;
// This may overflow...
}
template <bool Protected>
inline
std::pair<double, double>
to_interval (const Interval_nt<Protected> & d)
{
return d.pair();
}
template <bool Protected>
inline
Interval_nt<Protected>
sqrt (const Interval_nt<Protected> & d)
{
typename Interval_nt<Protected>::Internal_protector P; // not optimal here.
// sqrt([+a,+b]) => [sqrt(+a);sqrt(+b)]
// sqrt([-a,+b]) => [0;sqrt(+b)] => assumes roundoff error.
// sqrt([-a,-b]) => [0;sqrt(-b)] => assumes user bug (unspecified result).
#ifdef __AVX512F__
double i = 0;
if(d.inf() > 0){
__m128d x = d.simd();
__m128d m = _mm_set_sd(-0.);
__m128d y = _mm_xor_pd(x, m);
// We don't opacify because hopefully a rounded operation is explicit
// enough that compilers won't mess with it, and it does not care about
// fesetround.
__m128d vr = _mm_sqrt_round_sd(y, y, _MM_FROUND_TO_NEG_INF|_MM_FROUND_NO_EXC);
i = _mm_cvtsd_f64(vr);
// We could compute the sqrt of d.sup() using _mm_sqrt_pd (same speed as
// _sd except on broadwell) so it is already in the high part and we can
// call _mm_sqrt_round_sd(y, x, ...) to merge them directly, but I doubt
// it helps significantly, it might even hurt by introducing a
// dependency.
}
#else // no __AVX512F__
// TODO: Alternative for computing CGAL_IA_SQRT_DOWN(d.inf()) exactly
// without changing the rounding mode:
// - compute x = CGAL_IA_SQRT(d.inf())
// - compute y = CGAL_IA_SQUARE(x)
// - if y==d.inf() use x, else use -CGAL_IA_SUB(CGAL_IA_MIN_DOUBLE,x)
double i = IA_sqrt_toward_zero(d.inf());
#endif // no __AVX512F__
return Interval_nt<Protected>(i, IA_sqrt_up(d.sup()));
}
template <bool Protected>
inline
Interval_nt<Protected>
square (const Interval_nt<Protected> & d)
{
typename Interval_nt<Protected>::Internal_protector P;
#ifdef CGAL_USE_SSE2
__m128d a = IA_opacify128(CGAL::abs(d).simd()); // {-i,s} 0<=i<=s
__m128d b = _mm_xor_pd(a, _mm_setr_pd(-0., 0.)); // {i,s}
__m128d r = _mm_mul_pd(a, b); // {-i*i,s*s}
return Interval_nt<Protected>(IA_opacify128(r));
#else
if (d.inf()>=0.0)
return Interval_nt<Protected>(-CGAL_IA_MUL(-d.inf(), d.inf()),
CGAL_IA_SQUARE(d.sup()));
if (d.sup()<=0.0)
return Interval_nt<Protected>(-CGAL_IA_MUL(d.sup(), -d.sup()),
CGAL_IA_SQUARE(-d.inf()));
return Interval_nt<Protected>(0.0, CGAL_IA_SQUARE((std::max)(-d.inf(),
d.sup())));
#endif
}
template <bool Protected>
inline
Interval_nt<Protected>
abs (const Interval_nt<Protected> & d)
{
#ifdef CGAL_USE_SSE2
__m128d a = d.simd();
__m128d b = (-d).simd();
__m128d x = _mm_min_pd (a, b);
__m128d y = _mm_max_pd (a, b);
__m128d t = _mm_move_sd (y, x);
__m128d z = _mm_set1_pd(-0.); // +0. would be valid, but I'd rather end up with interval [+0, sup]
__m128d r = _mm_min_sd(t, z);
return Interval_nt<Protected> (r);
#else
if (d.inf() >= 0.0) return d;
if (d.sup() <= 0.0) return -d;
return Interval_nt<Protected>(0.0, (std::max)(-d.inf(), d.sup()));
#endif
}
template <bool Protected>
inline
Uncertain<Sign>
sign (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return POSITIVE;
if (d.sup() < 0.0) return NEGATIVE;
if (d.inf() == d.sup()) return ZERO;
return Uncertain<Sign>::indeterminate();
}
template <bool Protected>
inline
Uncertain<Comparison_result>
compare (const Interval_nt<Protected> & d, const Interval_nt<Protected> & e)
{
if (d.inf() > e.sup()) return LARGER;
if (e.inf() > d.sup()) return SMALLER;
if (e.inf() == d.sup() && d.inf() == e.sup()) return EQUAL;
return Uncertain<Comparison_result>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_zero (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return false;
if (d.sup() < 0.0) return false;
if (d.inf() == d.sup()) return true;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_positive (const Interval_nt<Protected> & d)
{
if (d.inf() > 0.0) return true;
if (d.sup() <= 0.0) return false;
return Uncertain<bool>::indeterminate();
}
template <bool Protected>
inline
Uncertain<bool>
is_negative (const Interval_nt<Protected> & d)
{
if (d.inf() >= 0.0) return false;
if (d.sup() < 0.0) return true;
return Uncertain<bool>::indeterminate();
}
} // namespace INTERN_INTERVAL_NT
template< bool B > class Real_embeddable_traits< Interval_nt<B> >
: public INTERN_RET::Real_embeddable_traits_base< Interval_nt<B> , CGAL::Tag_true> {
public:
typedef Interval_nt<B> Type;
typedef Uncertain<CGAL::Sign> Sign;
typedef Uncertain<bool> Boolean;
typedef Uncertain<CGAL::Comparison_result> Comparison_result;
class Abs
: public CGAL::cpp98::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::abs( x );
}
};
class Sgn
: public CGAL::cpp98::unary_function< Type, Uncertain< ::CGAL::Sign > > {
public:
Uncertain< ::CGAL::Sign > operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::sign( x );
}
};
class Is_positive
: public CGAL::cpp98::unary_function< Type, Uncertain<bool> > {
public:
Uncertain<bool> operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::is_positive( x );
}
};
class Is_negative
: public CGAL::cpp98::unary_function< Type, Uncertain<bool> > {
public:
Uncertain<bool> operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::is_negative( x );
}
};
class Compare
: public CGAL::cpp98::binary_function< Type, Type, Comparison_result > {
public:
Comparison_result operator()( const Type& x, const Type& y ) const {
return INTERN_INTERVAL_NT::compare( x, y );
}
CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( Type,
Comparison_result )
};
class To_double
: public CGAL::cpp98::unary_function< Type, double > {
public:
double operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::to_double( x );
}
};
class To_interval
: public CGAL::cpp98::unary_function< Type, std::pair< double, double > > {
public:
std::pair<double, double> operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::to_interval( x );
}
};
class Is_finite
: public CGAL::cpp98::unary_function< Type, Boolean > {
public :
Boolean operator()( const Type& x ) const {
return CGAL_NTS is_finite( x.inf() ) && CGAL_NTS is_finite( x.sup() );
}
};
};
// Algebraic structure traits
template< bool B >
class Algebraic_structure_traits< Interval_nt<B> >
: public Algebraic_structure_traits_base< Interval_nt<B>,
Field_with_sqrt_tag > {
public:
typedef Interval_nt<B> Type;
typedef Tag_false Is_exact;
typedef Tag_true Is_numerical_sensitive;
typedef Uncertain<bool> Boolean;
class Is_zero
: public CGAL::cpp98::unary_function< Type, Boolean > {
public:
Boolean operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::is_zero( x );
}
};
// Specialized just to specify the result type
class Is_one
: public CGAL::cpp98::unary_function< Type, Boolean > {
public:
Boolean operator()( const Type& x ) const {
return x == 1;
}
};
class Square
: public CGAL::cpp98::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::square( x );
}
};
class Sqrt
: public CGAL::cpp98::unary_function< Type, Type > {
public:
Type operator()( const Type& x ) const {
return INTERN_INTERVAL_NT::sqrt( x );
}
};
struct Is_square
:public CGAL::cpp98::binary_function<Interval_nt<B>,Interval_nt<B>&,Boolean >
{
Boolean operator()(const Interval_nt<B>& x) const {
return INTERN_INTERVAL_NT::is_positive( x );
}
Boolean operator()(
const Interval_nt<B>& x,
Interval_nt<B> & result) const {
Boolean is_positive = INTERN_INTERVAL_NT::is_positive( x );
if ( is_positive.inf() == true ){
typename Algebraic_structure_traits<Interval_nt<B> >::Sqrt sqrt;
result = sqrt(x);
}else{
typename Real_embeddable_traits<Interval_nt<B> >::Abs abs;
typename Algebraic_structure_traits<Interval_nt<B> >::Sqrt sqrt;
result = sqrt(abs(x));
}
return is_positive;
}
};
class Divides
: public CGAL::cpp98::binary_function< Type, Type, Boolean > {
public:
Boolean operator()( const Type& x, const Type&) const {
return ! Is_zero()(x);
}
// second operator computing q
Boolean operator()( const Type& x, const Type& y, Type& q) const {
if (! Is_zero()(x) )
q = y/x ;
return Boolean(true);
}
};
};
// COERCION_TRAITS BEGIN
template < class A, class B , int > struct Coercion_traits_for_level;
template < class A, class B, class C> struct Coercion_traits_interval_nt;
template<class A ,bool P >
struct Coercion_traits_for_level<A,Interval_nt<P>,CTL_INTERVAL>
:public Coercion_traits_interval_nt<A,Interval_nt<P>,
typename Real_embeddable_traits<A>::Is_real_embeddable>{};
template<class A , bool P>
struct Coercion_traits_for_level<Interval_nt<P>,A,CTL_INTERVAL>
:public Coercion_traits_for_level<A,Interval_nt<P>, CTL_INTERVAL>{};
template<class A , bool P >
struct Coercion_traits_interval_nt<A, Interval_nt<P>,Tag_false>
:public Coercion_traits_for_level<A,Interval_nt<P>,0>{};
template<class A , bool P>
struct Coercion_traits_interval_nt<A, Interval_nt<P>, Tag_true>{
typedef Tag_true Are_explicit_interoperable;
typedef Tag_false Are_implicit_interoperable;
typedef Interval_nt<P> Type;
struct Cast {
typedef Interval_nt<P> result_type;
Interval_nt<P> inline operator()(const Interval_nt<P>& x ) const {
return x;
}
Interval_nt<P> inline operator()(const A& x ) const {
return typename Real_embeddable_traits<A>::To_interval()(x);
}
};
};
// COERCION_TRAITS END
template< bool B >
class Interval_traits< Interval_nt<B> >
: public internal::Interval_traits_base< Interval_nt<B> > {
public:
typedef Interval_traits<Interval_nt<B> > Self;
typedef Interval_nt<B> Interval;
typedef double Bound;
typedef CGAL::Tag_false With_empty_interval;
typedef CGAL::Tag_true Is_interval;
static constexpr bool is_interval_v = true;
struct Construct :public CGAL::cpp98::binary_function<Bound,Bound,Interval>{
Interval operator()( const Bound& l,const Bound& r) const {
CGAL_precondition( l < r );
return Interval(l,r);
}
};
struct Lower :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return a.inf();
}
};
struct Upper :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return a.sup();
}
};
struct Width :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return width(a);
}
};
struct Median :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return (Lower()(a)+Upper()(a))/2.0;
}
};
struct Norm :public CGAL::cpp98::unary_function<Interval,Bound>{
Bound operator()( const Interval& a ) const {
return magnitude(a);
}
};
struct Singleton :public CGAL::cpp98::unary_function<Interval,bool>{
bool operator()( const Interval& a ) const {
return a.is_point();
}
};
struct Zero_in :public CGAL::cpp98::unary_function<Interval,bool>{
bool operator()( const Interval& a ) const {
return Lower()(a) <= 0 && 0 <= Upper()(a);
}
};
struct In :public CGAL::cpp98::binary_function<Bound,Interval,bool>{
bool operator()( Bound x, const Interval& a ) const {
return Lower()(a) <= x && x <= Upper()(a);
}
};
struct Equal :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return a.is_same(b);
}
};
struct Overlap :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return a.do_overlap(b);
}
};
struct Subset :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return Lower()(b) <= Lower()(a) && Upper()(a) <= Upper()(b) ;
}
};
struct Proper_subset :public CGAL::cpp98::binary_function<Interval,Interval,bool>{
bool operator()( const Interval& a, const Interval& b ) const {
return Subset()(a,b) && ! Equal()(a,b);
}
};
struct Hull :public CGAL::cpp98::binary_function<Interval,Interval,Interval>{
Interval operator()( const Interval& a, const Interval& b ) const {
#ifdef CGAL_USE_SSE2
return Interval(_mm_max_pd(a.simd(), b.simd()));
#else
BOOST_USING_STD_MAX();
BOOST_USING_STD_MIN();
return Interval(
-max BOOST_PREVENT_MACRO_SUBSTITUTION (-a.inf(),-b.inf()),
max BOOST_PREVENT_MACRO_SUBSTITUTION ( a.sup(), b.sup()));
#endif
}
};
// struct Empty is Null_functor
struct Intersection :public CGAL::cpp98::binary_function<Interval,Interval,Interval>{
Interval operator()( const Interval& a, const Interval& b ) const {
BOOST_USING_STD_MAX();
BOOST_USING_STD_MIN();
Bound l(max BOOST_PREVENT_MACRO_SUBSTITUTION (Lower()(a),Lower()(b)));
Bound u(min BOOST_PREVENT_MACRO_SUBSTITUTION (Upper()(a),Upper()(b)));
if(u < l ) throw Exception_intersection_is_empty();
return Construct()(l,u);
}
};
};
} //namespace CGAL
namespace Eigen {
template<class> struct NumTraits;
template<bool b> struct NumTraits<CGAL::Interval_nt<b> >
{
typedef CGAL::Interval_nt<b> Real;
typedef CGAL::Interval_nt<b> NonInteger;
typedef CGAL::Interval_nt<b> Nested;
typedef double Literal;
static inline Real epsilon() { return 0; }
static inline Real dummy_precision() { return 0; }
static inline Real highest() { return Real((std::numeric_limits<double>::max)(), std::numeric_limits<double>::infinity()); }
static inline Real lowest() { return Real(-std::numeric_limits<double>::infinity(), std::numeric_limits<double>::lowest()); }
// Costs could depend on b.
enum {
IsInteger = 0,
IsSigned = 1,
IsComplex = 0,
RequireInitialization = 0,
ReadCost = 2,
AddCost = 2,
MulCost = 10
};
};
template<class A, class B, class C>struct ScalarBinaryOpTraits;
template<bool b, typename BinaryOp>
struct ScalarBinaryOpTraits<CGAL::Interval_nt<b>, double, BinaryOp> {
typedef CGAL::Interval_nt<b> ReturnType;
};
template<bool b, typename BinaryOp>
struct ScalarBinaryOpTraits<double, CGAL::Interval_nt<b>, BinaryOp> {
typedef CGAL::Interval_nt<b> ReturnType;
};
namespace internal {
template<class> struct significant_decimals_impl;
template<bool b>
struct significant_decimals_impl<CGAL::Interval_nt<b> >
: significant_decimals_impl<typename CGAL::Interval_nt<b>::value_type> { };
// Without this, when computing some decompositions for a matrix of
// intervals, Eigen looks for the largest element in a column (for
// instance). There may easily be 2 equal, slightly imprecise numbers that
// could equally well be used as pivots, but Eigen ends up spuriously
// throwing in the comparison between them. So we provide a different
// strategy for picking the pivot.
template<typename> struct scalar_score_coeff_op;
template<bool b> struct scalar_score_coeff_op<CGAL::Interval_nt<b> > {
// If all coeffs can be 0, it is essential to designate as the best one
// that can be non-zero and has a non-zero score, if there is one.
struct result_type : boost::totally_ordered1<result_type> {
CGAL::Interval_nt<b> i;
result_type():i(){}
result_type(CGAL::Interval_nt<b> j):i(j){}
friend bool operator<(result_type x, result_type y){
if(x.i.inf()==0){
if(y.i.inf()==0)return x.i.sup()<y.i.sup(); // [0,0]<[0,1]
else return true; // [0,*]<[1,*]
}
#if 0
// The following is already handled by the general formula below
if(y.i.inf()==0)return false; // [0,*]<[1,*]
#endif
// Both numbers are guaranteed non-zero. With double people usually
// pick the biggest number. Here we choose the tightest interval.
// This is purely heuristic, it doesn't matter much if overflow makes
// us do random choices.
// Best is largest inf/sup (ideally 1)
// Risk of {over,under}flow
return x.i.inf()*y.i.sup() < y.i.inf()*x.i.sup();
}
// Only used as: if(max==Score(0))
friend bool operator==(result_type x, result_type y){
// Throw if we don't know if the max coeff is 0
return x.i == y.i;
}
};
result_type operator()(CGAL::Interval_nt<b> const&x)const{return abs(x);}
};
template<typename> struct functor_traits;
template<bool b> struct functor_traits<scalar_score_coeff_op<CGAL::Interval_nt<b> > >
{
enum {
Cost = 10,
PacketAccess = false
};
};
}
}
#undef CGAL_CST_TRUE
#endif // CGAL_INTERVAL_NT_H
|