/usr/include/vigra/fixedpoint.hxx is in libvigraimpex-dev 1.7.1+dfsg1-2ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 | /************************************************************************/
/* */
/* Copyright 2004-2005 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_FIXEDPOINT_HXX
#define VIGRA_FIXEDPOINT_HXX
#include "mathutil.hxx"
#include "static_assert.hxx"
#include "error.hxx"
#include "numerictraits.hxx"
namespace vigra {
template <unsigned IntBits, unsigned FractionalBits>
class FixedPoint;
struct Error_FixedPointTraits_not_specialized_for_this_case;
template <class T1, class T2>
class FixedPointTraits
{
public:
typedef Error_FixedPointTraits_not_specialized_for_this_case PlusType;
typedef Error_FixedPointTraits_not_specialized_for_this_case MinusType;
typedef Error_FixedPointTraits_not_specialized_for_this_case MultipliesType;
// typedef Error_FixedPointTraits_not_specialized_for_this_case DividesType;
};
// return type policy:
// * try to allocate enough bits to represent the biggest possible result
// * in case of add/subtract: if all bits of the internal int are used up,
// keep the representation
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
class FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >
{
enum { MaxIntBits = (IntBits1 < IntBits2) ? IntBits2 : IntBits1,
MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1,
PlusMinusIntBits = (MaxIntBits + 1 + MaxFracBits < 32) ?
MaxIntBits + 1 : MaxIntBits,
MultipliesFracBits = (IntBits1 + IntBits2 < 31)
? (FracBits1 + FracBits2) > (31 - IntBits1 - IntBits2)
? 31 - IntBits1 - IntBits2
: FracBits1 + FracBits2
: 0
};
public:
typedef FixedPoint<PlusMinusIntBits, MaxFracBits> PlusType;
typedef FixedPoint<PlusMinusIntBits, MaxFracBits> MinusType;
typedef FixedPoint<IntBits1 + IntBits2, MultipliesFracBits> MultipliesType;
// typedef FixedPoint<IntBits1 + FracBits2, FracBits1 + IntBits2> DividesType;
};
template <unsigned IntBits, unsigned FracBits>
struct SquareRootTraits<FixedPoint<IntBits, FracBits> >
{
enum { SRTotalBits = (IntBits + FracBits + 1) / 2,
SRIntBits = (IntBits + 1) / 2,
SRFracBits = SRTotalBits - SRIntBits
};
public:
typedef FixedPoint<IntBits, FracBits> Type;
typedef FixedPoint<SRIntBits, SRFracBits> SquareRootResult;
typedef Type SquareRootArgument;
};
#ifndef DOXYGEN
template <int N>
struct FixedPoint_overflow_error__More_than_31_bits_requested
: staticAssert::AssertBool<(N < 32)>
{};
#endif /* DOXYGEN */
template <bool Predicate>
struct FixedPoint_assignment_error__Target_object_has_too_few_integer_bits
: staticAssert::AssertBool<Predicate>
{};
enum FixedPointNoShift { FPNoShift };
namespace detail {
template <bool MustRound>
struct FPAssignWithRound;
template <>
struct FPAssignWithRound<false>
{
template <int N>
static inline int exec(int v) { return v << (-N); }
};
template <>
struct FPAssignWithRound<true>
{
template <int N>
static inline int exec(int const v)
{
return (v + (1 << (N - 1))) >> (N);
}
};
template <bool MustRound>
struct FPMulImplementation;
template <>
struct FPMulImplementation<false>
{
template <int N>
static inline int exec(int l, int r) { return (l * r) << (-N); }
};
template <>
struct FPMulImplementation<true>
{
template <int N>
static inline int exec(int l, int r)
{
// there is not enough space in the result
// => perform calculations that preserve as much accuracy as possible
enum { diffl = N / 2, diffr = N - diffl, maskl = (1 << diffl) - 1, maskr = (1 << diffr) - 1 };
int shiftl = l >> diffl;
int shiftr = r >> diffr;
return shiftl * shiftr + (((l & maskl) * shiftr) >> diffl) +
(((r & maskr) * shiftl) >> diffr);
}
};
} // namespace detail
/********************************************************/
/* */
/* FixedPoint */
/* */
/********************************************************/
/** Template for fixed point arithmetic.
Fixed point arithmetic is used when computations with fractional accuracy
must be made at the highest speed possible (e.g. in the inner loop
of a volume rendering routine). The speed-up relative to floating
point arithmetic can be dramatic, especially when one can avoid
conversions between integer anfloating point numbers (these are
very expensive because integer and floating point arithmetic
resides in different pipelines).
The template wraps an <tt>int</tt> and uses <tt>IntBits</tt> to
represent the integral part of a number, and <tt>FractionalBits</tt>
for the fractional part, where <tt>IntBits + FractionalBits < 32</tt>.
(The 32rd bit is reserved because FixedPoint is a signed type).
These numbers will be automatically allocated in an intelligent way
in the result of an arithmetic operation. For example, when two
fixed point numbers are multiplied, the required number of integer
bits in the result is the sum of the number of integer bits of the
arguments, but only when so many bits are avaiable. This is figured out
by means of FixedPointTraits, and a compile-time error is raised
when no suitable representation can be found. The idea is that the right
thing happens automatically as often as possible.
<tt>FixedPoint</tt> implements the required interface of an
\ref AlgebraicRing and the required numeric and
promotion traits. In addition, it supports functions <tt>add</tt>,
<tt>sub</tt>, and <tt>mul</tt>, where a particular layout of the result can
be enforced.
<tt>unsigned char, signed char, unsigned short, signed short, int</tt> can be
transformed into a FixedPoint with appropriate layout by means of the factory
function <tt>fixedPoint()</tt>.
<b>See also:</b>
<ul>
<li> \ref FixedPointOperations
<li> \ref FixedPointTraits
</ul>
<b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
Namespace: vigra
*/
template <unsigned IntBits, unsigned FractionalBits>
class FixedPoint
{
public:
enum {
INT_BITS = IntBits,
FRACTIONAL_BITS = FractionalBits,
TOTAL_BITS = IntBits + FractionalBits,
MAX = (int)(((unsigned)1 << TOTAL_BITS) - 1),
ONE = 1 << FractionalBits,
ONE_HALF = ONE >> 1,
FRACTIONAL_MASK = ONE - 1,
INT_MASK = MAX ^ FRACTIONAL_MASK
};
Int32 value;
FixedPoint()
{
VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<(IntBits + FractionalBits)>));
}
/** Construct from an int (fractional part will become zero).
*/
explicit FixedPoint(int v)
: value(v << FractionalBits)
{
VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<(IntBits + FractionalBits)>));
}
/** Construct from an int by a bitwise copy. This is normally only used internally.
*/
FixedPoint(int v, FixedPointNoShift)
: value(v)
{
VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<(IntBits + FractionalBits)>));
}
/** Construct from an double and round the fractional part to
<tt>FractionalBits</tt> accuracy. A PreconditionViolation exception is raised when
the integer part is too small to represent the number.
*/
explicit FixedPoint(double rhs)
: value((int)round(rhs * ONE))
{
VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<(IntBits + FractionalBits)>));
vigra_precondition(abs(rhs * ONE) <= (double)MAX,
"FixedPoint(double rhs): Too few integer bits to convert rhs.");
}
/** Copy constructor.
*/
FixedPoint(const FixedPoint &other)
: value(other.value)
{}
/** Construct from a FixedPoint with different layout. It rounds as appropriate and raises
a compile-time error when the target type has too few integer bits.
*/
template <unsigned Int2, unsigned Frac2>
FixedPoint(const FixedPoint<Int2, Frac2> &other)
: value(detail::FPAssignWithRound<(Frac2 > FractionalBits)>::template exec<Frac2 - FractionalBits>(other.value))
{
VIGRA_STATIC_ASSERT((FixedPoint_overflow_error__More_than_31_bits_requested<(IntBits + FractionalBits)>));
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits >= Int2)>));
}
/** Assignment from int. The fractional part will become zero.
A PreconditionViolation exception is raised when
the integer part is too small to represent the number.
*/
FixedPoint &operator=(int rhs)
{
vigra_precondition(abs(rhs) < (1 << IntBits),
"FixedPoint::operator=(int rhs): Too few integer bits to represent rhs.");
value = rhs << FractionalBits;
return *this;
}
/** Assignment form double. The fractional part is rounded, and a
PreconditionViolation exception is raised when
the integer part is too small to represent the number.
*/
FixedPoint &operator=(double rhs)
{
vigra_precondition(abs(rhs) <= ((1 << IntBits) - 1),
"FixedPoint::operator=(double rhs): Too few integer bits to convert rhs.");
value = (int)round(rhs * ONE);
return *this;
}
/** Copy assignment.
*/
FixedPoint & operator=(const FixedPoint &other)
{
value = other.value;
return *this;
}
/** Assignment from a FixedPoint with different layout. It rounds as appropriate and raises
a compile-time error when the target type has too few integer bits.
*/
template <unsigned Int2, unsigned Frac2>
FixedPoint & operator=(const FixedPoint<Int2, Frac2> &other)
{
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits >= Int2)>));
value = detail::FPAssignWithRound<(Frac2 > FractionalBits)>::template exec<Frac2 - FractionalBits>(other.value);
return *this;
}
/** Negation.
*/
FixedPoint operator-() const
{
return FixedPoint(-value, FPNoShift);
}
/** Pre-increment.
*/
FixedPoint & operator++()
{
value += ONE;
return *this;
}
/** Post-increment.
*/
FixedPoint operator++(int)
{
FixedPoint old(*this);
value += ONE;
return old;
}
/** Pre-decrement.
*/
FixedPoint & operator--()
{
value -= ONE;
return *this;
}
/** Post-decrement.
*/
FixedPoint operator--(int)
{
FixedPoint old(*this);
value -= ONE;
return old;
}
/** Add-assignment from a FixedPoint with different layout. It rounds as appropriate and raises
a compile-time error when the target type has too few integer bits.
*/
template <unsigned Int2, unsigned Frac2>
FixedPoint & operator+=(const FixedPoint<Int2, Frac2> &other)
{
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits >= Int2)>));
value += detail::FPAssignWithRound<(Frac2 > FractionalBits)>::template exec<Frac2 - FractionalBits>(other.value);
return *this;
}
/** Subtract-assignment from a FixedPoint with different layout. It rounds as appropriate and raises
a compile-time error when the target type has too few integer bits.
*/
template <unsigned Int2, unsigned Frac2>
FixedPoint & operator-=(const FixedPoint<Int2, Frac2> &other)
{
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits >= Int2)>));
value -= detail::FPAssignWithRound<(Frac2 > FractionalBits)>::template exec<Frac2 - FractionalBits>(other.value);
return *this;
}
/** Multiply-assignment from a FixedPoint with different layout. It rounds as appropriate and raises
a compile-time error when the target type has too few integer bits.
*/
template <unsigned Int2, unsigned Frac2>
FixedPoint & operator*=(const FixedPoint<Int2, Frac2> &other)
{
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits >= Int2)>));
value = detail::FPMulImplementation<(Frac2 > 0)>::template exec<Frac2>(value, other.value);
return *this;
}
};
#define VIGRA_FIXED_POINT_FACTORY(T, INTBITS) \
inline FixedPoint<INTBITS, 0> fixedPoint(T t) \
{ \
return FixedPoint<INTBITS, 0>(t, FPNoShift); \
}
VIGRA_FIXED_POINT_FACTORY(unsigned char, 8)
VIGRA_FIXED_POINT_FACTORY(signed char, 7)
VIGRA_FIXED_POINT_FACTORY(unsigned short, 16)
VIGRA_FIXED_POINT_FACTORY(signed short, 15)
VIGRA_FIXED_POINT_FACTORY(int, 31)
#undef VIGRA_FIXED_POINT_FACTORY
template <class T>
struct FixedPointCast;
#define VIGRA_FIXED_POINT_CAST(type) \
template <> \
struct FixedPointCast<type> \
{ \
template <unsigned IntBits, unsigned FracBits> \
static type cast(FixedPoint<IntBits, FracBits> v) \
{ \
return round(v); \
} \
};
VIGRA_FIXED_POINT_CAST(Int8)
VIGRA_FIXED_POINT_CAST(UInt8)
VIGRA_FIXED_POINT_CAST(Int16)
VIGRA_FIXED_POINT_CAST(UInt16)
VIGRA_FIXED_POINT_CAST(Int32)
VIGRA_FIXED_POINT_CAST(UInt32)
#undef VIGRA_FIXED_POINT_CAST
template <>
struct FixedPointCast<float>
{
template <unsigned IntBits, unsigned FracBits>
static float cast(FixedPoint<IntBits, FracBits> v)
{
return (float)v.value / FixedPoint<IntBits, FracBits>::ONE;
}
};
template <>
struct FixedPointCast<double>
{
template <unsigned IntBits, unsigned FracBits>
static double cast(FixedPoint<IntBits, FracBits> v)
{
return (double)v.value / FixedPoint<IntBits, FracBits>::ONE;
}
};
/********************************************************/
/* */
/* FixedPointOperations */
/* */
/********************************************************/
/** \addtogroup FixedPointOperations Functions for FixedPoint
\brief <b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
These functions fulfill the requirements of an \ref AlgebraicRing.
Namespace: vigra
<p>
*/
//@{
/** Convert a FixedPoint to a built-in type.
If the target is integral, the value is rounded.<br>
Usage:
\code
FixedPoint<16,15> fp(...);
double d = fixed_point_cast<double>(fp);
\endcode
*/
template <class TARGET, unsigned IntBits, unsigned FracBits>
TARGET fixed_point_cast(FixedPoint<IntBits, FracBits> v)
{
return FixedPointCast<TARGET>::cast(v);
}
/// equal
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator==(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) == (r.value << (MaxFracBits - FracBits2));
}
/// not equal
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator!=(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) != (r.value << (MaxFracBits - FracBits2));
}
/// less than
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator<(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) < (r.value << (MaxFracBits - FracBits2));
}
/// less or equal
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator<=(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) <= (r.value << (MaxFracBits - FracBits2));
}
/// greater
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator>(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) > (r.value << (MaxFracBits - FracBits2));
}
/// greater or equal
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
bool operator>=(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return (l.value << (MaxFracBits - FracBits1)) >= (r.value << (MaxFracBits - FracBits2));
}
/// addition with automatic determination of the appropriate result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
typename FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::PlusType
operator+(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return typename
FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::
PlusType((l.value << (MaxFracBits - FracBits1)) + (r.value << (MaxFracBits - FracBits2)), FPNoShift);
}
/// addition with enforced result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2,
unsigned IntBits3, unsigned FracBits3>
inline void
add(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r,
FixedPoint<IntBits3, FracBits3> & result)
{
result = l + r;
}
/// subtraction with automatic determination of the appropriate result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
typename FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::MinusType
operator-(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
enum { MaxFracBits = (FracBits1 < FracBits2) ? FracBits2 : FracBits1 };
return typename
FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::
MinusType((l.value << (MaxFracBits - FracBits1)) - (r.value << (MaxFracBits - FracBits2)), FPNoShift);
}
/// subtraction with enforced result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2,
unsigned IntBits3, unsigned FracBits3>
inline void
sub(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r,
FixedPoint<IntBits3, FracBits3> & result)
{
result = l - r;
}
/// multiplication with automatic determination of the appropriate result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
inline
typename FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::MultipliesType
operator*(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r)
{
typename FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::
MultipliesType res;
mul(l, r, res);
return res;
}
/// multiplication with enforced result type.
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2,
unsigned IntBits3, unsigned FracBits3>
inline void
mul(FixedPoint<IntBits1, FracBits1> l, FixedPoint<IntBits2, FracBits2> r,
FixedPoint<IntBits3, FracBits3> & result)
{
VIGRA_STATIC_ASSERT((FixedPoint_assignment_error__Target_object_has_too_few_integer_bits<(IntBits1 + IntBits2 <= IntBits3)>));
enum { diff = FracBits1 + FracBits2 - FracBits3 };
result.value = detail::FPMulImplementation<(diff > 0)>::template exec<diff>(l.value, r.value);
}
/// square root.
template <unsigned IntBits, unsigned FracBits>
inline typename SquareRootTraits<FixedPoint<IntBits, FracBits> >::SquareRootResult
sqrt(FixedPoint<IntBits, FracBits> v)
{
return typename SquareRootTraits<FixedPoint<IntBits, FracBits> >::SquareRootResult(sqrti(v.value), FPNoShift);
}
/// absolute value.
template <unsigned IntBits, unsigned FracBits>
inline FixedPoint<IntBits, FracBits>
abs(FixedPoint<IntBits, FracBits> v)
{
return FixedPoint<IntBits, FracBits>(abs(v.value), FPNoShift);
}
/// squared norm (same as v*v).
template <unsigned IntBits, unsigned FracBits>
inline
typename FixedPointTraits<FixedPoint<IntBits, FracBits>, FixedPoint<IntBits, FracBits> >::MultipliesType
squaredNorm(FixedPoint<IntBits, FracBits> v)
{
return v*v;
}
/// norm (same as abs).
template <unsigned IntBits, unsigned FracBits>
inline
FixedPoint<IntBits, FracBits>
norm(FixedPoint<IntBits, FracBits> const & v)
{
return abs(v);
}
/// fractional part.
template <unsigned IntBits, unsigned FracBits>
inline FixedPoint<0, FracBits>
frac(FixedPoint<IntBits, FracBits> v)
{
return FixedPoint<0, FracBits>(v.value & FixedPoint<IntBits, FracBits>::FRACTIONAL_MASK, FPNoShift);
}
/// dual fractional part: <tt>1 - frac(v)</tt>.
template <unsigned IntBits, unsigned FracBits>
inline FixedPoint<0, FracBits>
dual_frac(FixedPoint<IntBits, FracBits> v)
{
return FixedPoint<0, FracBits>(FixedPoint<0, FracBits>::ONE -
(v.value & FixedPoint<IntBits, FracBits>::FRACTIONAL_MASK), FPNoShift);
}
/// rounding down.
template <unsigned IntBits, unsigned FracBits>
inline int
floor(FixedPoint<IntBits, FracBits> v)
{
return(v.value >> FracBits);
}
/// rounding up.
template <unsigned IntBits, unsigned FracBits>
inline int
ceil(FixedPoint<IntBits, FracBits> v)
{
return((v.value + FixedPoint<IntBits, FracBits>::FRACTIONAL_MASK) >> FracBits);
}
/// rounding to the nearest integer.
template <unsigned IntBits, unsigned FracBits>
inline int
round(FixedPoint<IntBits, FracBits> v)
{
return((v.value + FixedPoint<IntBits, FracBits>::ONE_HALF) >> FracBits);
}
//@}
/********************************************************/
/* */
/* FixedPoint-Traits */
/* */
/********************************************************/
/** \page FixedPointTraits Numeric and Promote Traits of FixedPoint
The numeric and promote traits for FixedPoint follow
the general specifications for \ref NumericPromotionTraits and
\ref AlgebraicRing. They are implemented in terms of the traits of the basic types by
partial template specialization:
\code
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
class FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >
{
typedef FixedPoint<PlusMinusIntBits, MaxFracBits> PlusType;
typedef FixedPoint<PlusMinusIntBits, MaxFracBits> MinusType;
typedef FixedPoint<IntBits1 + IntBits2, FracBits1 + FracBits2> MultipliesType;
};
template <unsigned IntBits, unsigned FracBits>
struct NumericTraits<FixedPoint<IntBits, FracBits> >
{
typedef FixedPoint<IntBits, FracBits> Type;
// Promote undefined because it depends on the layout, use FixedPointTraits
// RealPromote in AlgebraicRing -- multiplication with double is not supported.
// ComplexPromote in AlgebraicRing -- multiplication with double is not supported.
typedef Type ValueType;
typedef VigraFalseType isIntegral;
typedef VigraTrueType isScalar;
typedef VigraTrueType isSigned;
typedef VigraTrueType isOrdered;
typedef VigraFalseType isComplex;
... // etc.
};
template <unsigned IntBits, unsigned FracBits>
struct SquareRootTraits<FixedPoint<IntBits, FracBits> >
{
typedef FixedPoint<IntBits, FracBits> Type;
typedef FixedPoint<SRIntBits, SRFracBits> SquareRootResult;
typedef Type SquareRootArgument;
};
template <unsigned IntBits, unsigned FracBits>
struct NormTraits<FixedPoint<IntBits, FracBits> >
{
typedef FixedPoint<IntBits, FracBits> Type;
typedef typename
FixedPointTraits<FixedPoint<IntBits, FracBits>, FixedPoint<IntBits, FracBits> >::MultipliesType
SquaredNormType;
typedef Type NormType;
};
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
struct PromoteTraits<FixedPoint<IntBits1, FracBits1>,
FixedPoint<IntBits2, FracBits2> >
{
typedef typename
FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::PlusType
Promote;
};
\endcode
<b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
Namespace: vigra
*/
template <unsigned IntBits, unsigned FracBits>
struct NumericTraits<FixedPoint<IntBits, FracBits> >
{
typedef FixedPoint<IntBits, FracBits> Type;
//typedef FixedPoint<IntBits, FracBits> Promote;
//typedef FixedPoint<IntBits, FracBits> RealPromote;
//typedef std::complex<RealPromote> ComplexPromote;
typedef Type ValueType;
typedef VigraFalseType isIntegral;
typedef VigraTrueType isScalar;
typedef VigraTrueType isSigned;
typedef VigraTrueType isOrdered;
typedef VigraFalseType isComplex;
static Type zero() { return Type(0, FPNoShift); }
static Type one() { return Type(Type::ONE, FPNoShift); }
static Type nonZero() { return one(); }
static Type epsilon() { return Type(1, FPNoShift); }
static Type smallestPositive() { return Type(1, FPNoShift); }
static Type max() { return Type( Type::MAX, FPNoShift); }
static Type min() { return -max(); }
};
template <unsigned IntBits, unsigned FracBits>
struct NormTraits<FixedPoint<IntBits, FracBits> >
{
typedef FixedPoint<IntBits, FracBits> Type;
typedef typename
FixedPointTraits<FixedPoint<IntBits, FracBits>, FixedPoint<IntBits, FracBits> >::MultipliesType
SquaredNormType;
typedef Type NormType;
};
template <unsigned IntBits1, unsigned FracBits1, unsigned IntBits2, unsigned FracBits2>
struct PromoteTraits<FixedPoint<IntBits1, FracBits1>,
FixedPoint<IntBits2, FracBits2> >
{
typedef typename
FixedPointTraits<FixedPoint<IntBits1, FracBits1>, FixedPoint<IntBits2, FracBits2> >::PlusType
Promote;
};
/***********************************************************************************/
enum FPOverflowHandling { FPOverflowIgnore, FPOverflowSaturate, FPOverflowError };
template <int IntBits, FPOverflowHandling OverflowHandling = FPOverflowIgnore>
class FixedPoint16;
/********************************************************/
/* */
/* FixedPoint16-Traits */
/* */
/********************************************************/
/** \page FixedPoint16Traits Numeric and Promote Traits of FixedPoint16
The numeric and promote traits for FixedPoint16 follow
the general specifications for \ref NumericPromotionTraits and
\ref AlgebraicRing. They are implemented in terms of the traits of the basic types by
partial template specialization:
\code
template <int IntBits, FPOverflowHandling OverflowHandling>
struct NumericTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef Type Promote;
// RealPromote undefined -- multiplication with double is not supported.
// ComplexPromote undefined -- multiplication with double is not supported.
typedef Type ValueType;
typedef VigraFalseType isIntegral;
typedef VigraTrueType isScalar;
typedef VigraTrueType isSigned;
typedef VigraTrueType isOrdered;
typedef VigraFalseType isComplex;
... // etc.
};
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
struct PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>,
FixedPoint16<IntBits2, OverflowHandling> >
{
typedef FixedPoint16<MetaMax<IntBits1, IntBits2>::value, OverflowHandling> Promote;
... // etc.
};
template <int IntBits, FPOverflowHandling OverflowHandling>
struct NormTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef typename PromoteTraits<Type, Type>::Promote SquaredNormType;
typedef Type NormType;
};
template <int IntBits, FPOverflowHandling OverflowHandling>
struct SquareRootTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef FixedPoint16<(IntBits + 1) / 2, OverflowHandling> SquareRootResult;
typedef Type SquareRootArgument;
};
\endcode
<b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
Namespace: vigra
*/
template <int IntBits, FPOverflowHandling OverflowHandling>
struct NumericTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef Type Promote;
// RealPromote undefined -- multiplication with double is not supported.
// ComplexPromote undefined -- multiplication with double is not supported.
typedef Type ValueType;
typedef VigraFalseType isIntegral;
typedef VigraTrueType isScalar;
typedef VigraTrueType isSigned;
typedef VigraTrueType isOrdered;
typedef VigraFalseType isComplex;
static Type zero() { return Type(0, FPNoShift); }
static Type one() { return Type(Type::ONE, FPNoShift); }
static Type nonZero() { return one(); }
static Type epsilon() { return Type(1, FPNoShift); }
static Type smallestPositive() { return Type(1, FPNoShift); }
static Type max() { return Type( Type::MAX, FPNoShift); }
static Type min() { return Type( Type::MIN, FPNoShift); }
static Promote toPromote(Type v) { return v; }
static Type fromPromote(Promote v) { return v; };
};
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
struct PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>,
FixedPoint16<IntBits2, OverflowHandling> >
{
typedef FixedPoint16<MetaMax<IntBits1, IntBits2>::value, OverflowHandling> Promote;
static Promote toPromote(FixedPoint16<IntBits1, OverflowHandling> v) { return Promote(v); }
static Promote toPromote(FixedPoint16<IntBits2, OverflowHandling> v) { return Promote(v); }
};
template <int IntBits, FPOverflowHandling OverflowHandling>
struct PromoteTraits<FixedPoint16<IntBits, OverflowHandling>,
FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Promote;
static Promote toPromote(FixedPoint16<IntBits, OverflowHandling> v) { return v; }
};
template <int IntBits, FPOverflowHandling OverflowHandling>
struct NormTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef typename PromoteTraits<Type, Type>::Promote SquaredNormType;
typedef Type NormType;
};
template <int IntBits, FPOverflowHandling OverflowHandling>
struct SquareRootTraits<FixedPoint16<IntBits, OverflowHandling> >
{
typedef FixedPoint16<IntBits, OverflowHandling> Type;
typedef FixedPoint16<(IntBits + 1) / 2, OverflowHandling> SquareRootResult;
typedef Type SquareRootArgument;
};
#ifndef DOXYGEN
template <bool Compatible>
struct FixedPoint_error__Right_shift_operator_has_unsupported_semantics
: staticAssert::AssertBool<Compatible>
{};
#endif /* DOXYGEN */
template <bool Predicate>
struct FixedPoint16_assignment_error__Target_object_has_too_few_integer_bits
: staticAssert::AssertBool<Predicate>
{};
namespace detail {
template<int BeforeIntBits, int AfterIntBits,
bool Round = false,
bool RightShift = (AfterIntBits >= BeforeIntBits)>
struct FP16Align;
template<int BeforeIntBits>
struct FP16Align<BeforeIntBits, BeforeIntBits, true, true>
{
static inline Int32 exec(Int32 v)
{
return v;
}
};
template<int BeforeIntBits>
struct FP16Align<BeforeIntBits, BeforeIntBits, false, true>
{
static inline Int32 exec(Int32 v)
{
return v;
}
};
template<int BeforeIntBits, int AfterIntBits>
struct FP16Align<BeforeIntBits, AfterIntBits, false, true>
{
static inline Int32 exec(Int32 v)
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
return v >> (AfterIntBits - BeforeIntBits);
}
};
template<int BeforeIntBits, int AfterIntBits>
struct FP16Align<BeforeIntBits, AfterIntBits, true, true>
{
enum { ONE_HALF = 1 << (AfterIntBits - BeforeIntBits - 1) };
static inline Int32 exec(Int32 v)
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
return (v + ONE_HALF) >> (AfterIntBits - BeforeIntBits);
}
};
template<int BeforeIntBits, int AfterIntBits, bool Round>
struct FP16Align<BeforeIntBits, AfterIntBits, Round, false>
{
static inline Int32 exec(Int32 v)
{
return v << (BeforeIntBits - AfterIntBits);
}
};
template <FPOverflowHandling OverflowHandling = FPOverflowIgnore>
struct FP16OverflowHandling
{
static inline Int32 exec(Int32 v)
{
return v;
}
static inline Int32 exec(UInt32 v)
{
return v;
}
};
template <>
struct FP16OverflowHandling<FPOverflowSaturate>
{
static inline Int32 exec(Int32 v)
{
if(v >= 1 << 15)
return (1 << 15) - 1;
if(v < -(1 << 15))
return -(1 << 15);
return v;
}
static inline Int32 exec(UInt32 v)
{
if(v >= 1 << 15)
return (1 << 15) - 1;
return v;
}
};
template <>
struct FP16OverflowHandling<FPOverflowError>
{
static inline Int32 exec(Int32 v)
{
vigra_precondition(v < (1 << 15) && v >= -(1 << 15),
"FixedPoint16: Operation overflows.");
return v;
}
static inline Int32 exec(UInt32 v)
{
vigra_precondition(v < (1 << 15),
"FixedPoint16: Operation overflows.");
return v;
}
};
template <int IntBits1, int IntBits2, int IntBitsOut,
FPOverflowHandling OverflowHandling >
struct FP16AddImpl
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
static inline Int32 exec(Int32 t1, Int32 t2)
{
return FP16OverflowHandling<OverflowHandling>::exec(
FP16Align<MinIntBits, IntBitsOut, /*Round*/ true>::exec(
FP16Align<IntBits1, MinIntBits, /*Round*/ false>::exec(t1) +
FP16Align<IntBits2, MinIntBits, /*Round*/ false>::exec(t2)));
}
};
template <int IntBits1, int IntBits2, int IntBitsOut,
FPOverflowHandling OverflowHandling >
struct FP16SubImpl
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
static inline Int32 exec(Int32 t1, Int32 t2)
{
return FP16OverflowHandling<OverflowHandling>::exec(
FP16Align<MinIntBits, IntBitsOut, /*Round*/ true>::exec(
FP16Align<IntBits1, MinIntBits, /*Round*/ false>::exec(t1) -
FP16Align<IntBits2, MinIntBits, /*Round*/ false>::exec(t2)));
}
};
template <int IntBits1, int IntBits2, int IntBitsOut,
FPOverflowHandling OverflowHandling >
struct FP16MulImpl
{
static inline Int32 exec(Int32 t1, Int32 t2)
{
return FP16OverflowHandling<OverflowHandling>::exec(
FP16Align<IntBits1+IntBits2, IntBitsOut+15, /*Round*/ true>::exec(t1*t2));
}
};
template <int IntBits1, int IntBits2, int IntBitsOut,
FPOverflowHandling OverflowHandling >
struct FP16DivImpl
{
static inline Int32 exec(Int32 t1, Int32 t2)
{
if(t2 == 0)
return (t1 >= 0)
? (1 << 15) - 1
: -(1 << 15);
return FP16OverflowHandling<OverflowHandling>::exec(
FP16Align<IntBits1-IntBits2, IntBitsOut+1, /*Round*/ true>::exec((t1<<16)/t2));
}
};
} // namespace detail
/********************************************************/
/* */
/* FixedPoint16 */
/* */
/********************************************************/
template <class TARGET, int IntBits, FPOverflowHandling OverflowHandling>
TARGET fixed_point_cast(FixedPoint16<IntBits, OverflowHandling> v);
/** Template for 16-bit signed fixed point arithmetic.
Fixed point arithmetic is used when computations with fractional accuracy
must be made at the highest speed possible (e.g. in the inner loop
of a volume rendering routine). The speed-up relative to floating
point arithmetic can be dramatic, especially when one can avoid
conversions between integer and floating point numbers (these are
very expensive because integer and floating point arithmetic
resides in different pipelines).
The template wraps an <tt>Int16</tt> and uses <tt>IntBits</tt> to
represent the integral part of a number, and <tt>15 - IntBits</tt>
for the fractional part. The 16th bit is reserved because FixedPoint16
is a signed type. Results of expressions with mixed types will preserve
larger number of <tt>IntBits</tt> of the results, in order to minimize
the possibility for overflow. Nonetheless, overflow can occur, and the
template parameter <tt>OverflowHandling</tt> determines how this will be
handled:
<DL>
<DT>FPOverflowIgnore<DD> (default) Ignore overflow, i.e. use the usual modulo behavior of the
built-in integer types.
<DT>FPOverflowSaturate<DD> Use the largest or smallest representable number (depending on sign)
in case of overflow.
<DT>FPOverflowError<DD> Throw <tt>PreconditionViolation</tt> upon overflow. This is useful for
debugging.
</DL>
The implementation relies on Int32-arithmetic and requires that the right-shift operator
preserves signedness. Although not enforced by the C++ standard, this is implemented
by most of today's processors. This property is checked by a
VIGRA_STATIC_ASSERT(FixedPoint_error__Right_shift_operator_has_unsupported_semantics).
<tt>FixedPoint16</tt> implements the required interface of an
\ref AlgebraicRing and the required numeric and
promotion traits. In addition, it supports functions <tt>add</tt>,
<tt>sub</tt>, <tt>mul</tt>, and <tt>div</tt>, where a particular layout
of the result can be enforced.
Built-in numeric types can be converted into <tt>FixedPoint16</tt> by the
appropriate constructors, and from <tt>FixedPoint16</tt> by means of
<tt>fixed_point_cast<TargetType>(fixedPoint)</tt>.
<b>See also:</b>
<ul>
<li> \ref FixedPoint16Operations
<li> \ref FixedPoint16Traits
</ul>
<b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
Namespace: vigra
*/
template <int IntBits, FPOverflowHandling OverflowHandling>
class FixedPoint16
{
public:
static const Int32 TOTAL_BITS = 15; // bit 16 is sign
static const Int32 INT_BITS = IntBits;
static const Int32 FRACTIONAL_BITS = TOTAL_BITS - INT_BITS;
static const Int32 MAX = (Int32)((1u << TOTAL_BITS) - 1);
static const Int32 MIN = -(Int32)(1u << TOTAL_BITS);
static const Int32 ONE = 1 << FRACTIONAL_BITS;
static const Int32 ONE_HALF = ONE >> 1;
static const Int32 FRACTIONAL_MASK = (1u << FRACTIONAL_BITS) - 1;
static const Int32 INT_MASK = 0xffffffffu ^ FRACTIONAL_MASK;
Int16 value;
FixedPoint16()
: value(0)
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Construct from an int (fractional part will become zero).
Possible overflow is handled according to the taget type's <tt>OverflowHandling</tt>.
*/
explicit FixedPoint16(Int32 v)
: value(detail::FP16OverflowHandling<OverflowHandling>::exec(v << FRACTIONAL_BITS))
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Construct from an int by a bitwise copy. This is normally only used internally.
*/
FixedPoint16(Int32 v, FixedPointNoShift)
: value(detail::FP16OverflowHandling<OverflowHandling>::exec(v))
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Construct from a double and round the fractional part to
<tt>FRACTIONAL_BITS</tt> accuracy. Possible overflow is handled according
to the taget type's <tt>OverflowHandling</tt>.
*/
explicit FixedPoint16(double rhs)
: value(detail::FP16OverflowHandling<OverflowHandling>::exec(roundi(rhs * ONE)))
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Copy constructor.
*/
FixedPoint16(const FixedPoint16 &other)
: value(other.value)
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Construct from a FixedPoint16 with different layout. It rounds as appropriate and
handles possible overflow according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2, FPOverflowHandling OverflowHandling2>
FixedPoint16(const FixedPoint16<IntBits2, OverflowHandling2> &other)
: value(detail::FP16OverflowHandling<OverflowHandling>::exec(
detail::FP16Align<IntBits2, IntBits, /*Round*/true>::exec(other.value)))
{
VIGRA_STATIC_ASSERT((FixedPoint_error__Right_shift_operator_has_unsupported_semantics<((-1 >> 8) == -1)>));
}
/** Assignment from int. The fractional part will become zero.
Possible overflow is handled according to the taget type's <tt>OverflowHandling</tt>.
*/
FixedPoint16 &operator=(Int32 rhs)
{
value = detail::FP16OverflowHandling<OverflowHandling>::exec(rhs << FRACTIONAL_BITS);
return *this;
}
/** Assignment form double. The fractional part is rounded, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
FixedPoint16 &operator=(double rhs)
{
value = detail::FP16OverflowHandling<OverflowHandling>::exec(roundi(rhs * ONE));
return *this;
}
/** Copy assignment.
*/
FixedPoint16 & operator=(const FixedPoint16 &other)
{
value = other.value;
return *this;
}
/** Assignment from a FixedPoint16 with different layout. It rounds as appropriate, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2>
FixedPoint16 & operator=(const FixedPoint16<IntBits2, OverflowHandling> &other)
{
value = detail::FP16OverflowHandling<OverflowHandling>::exec(
detail::FP16Align<IntBits2, IntBits, /*Round*/true>::exec(other.value));
return *this;
}
/** Conversion to float
*/
operator float() const
{
return fixed_point_cast<float>(*this);
}
/** Conversion to double
*/
operator double() const
{
return fixed_point_cast<double>(*this);
}
/** Unary plus.
*/
FixedPoint16 operator+() const
{
return *this;
}
/** Negation.
*/
FixedPoint16 operator-() const
{
return FixedPoint16(-value, FPNoShift);
}
/** Pre-increment.
*/
FixedPoint16 & operator++()
{
value = detail::FP16OverflowHandling<OverflowHandling>::exec(value+ONE);
return *this;
}
/** Post-increment.
*/
FixedPoint16 operator++(int)
{
FixedPoint16 old(*this);
++(*this);
return old;
}
/** Pre-decrement.
*/
FixedPoint16 & operator--()
{
value = detail::FP16OverflowHandling<OverflowHandling>::exec(value-ONE);
return *this;
}
/** Post-decrement.
*/
FixedPoint16 operator--(int)
{
FixedPoint16 old(*this);
--(*this);
return old;
}
/** Add-assignment from a FixedPoint16 with different layout. It rounds as appropriate, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2>
FixedPoint16 & operator+=(const FixedPoint16<IntBits2, OverflowHandling> &other)
{
value = detail::FP16AddImpl<IntBits, IntBits2, IntBits, OverflowHandling>::exec(value, other.value);
return *this;
}
/** Subtract-assignment from a FixedPoint16 with different layout. It rounds as appropriate, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2>
FixedPoint16 & operator-=(const FixedPoint16<IntBits2, OverflowHandling> &other)
{
value = detail::FP16SubImpl<IntBits, IntBits2, IntBits, OverflowHandling>::exec(value, other.value);
return *this;
}
/** Multiply-assignment from a FixedPoint16 with different layout. It rounds as appropriate, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2>
FixedPoint16 & operator*=(const FixedPoint16<IntBits2, OverflowHandling> &other)
{
value = detail::FP16MulImpl<IntBits, IntBits2, IntBits, OverflowHandling>::exec(value, other.value);
return *this;
}
/** Divide-assignment from a FixedPoint16 with different layout. It rounds as appropriate, and possible overflow is
handled according to the taget type's <tt>OverflowHandling</tt>.
*/
template <int IntBits2>
FixedPoint16 & operator/=(const FixedPoint16<IntBits2, OverflowHandling> &other)
{
value = detail::FP16DivImpl<IntBits, IntBits2, IntBits, OverflowHandling>::exec(value, other.value);
return *this;
}
};
namespace detail {
template <class T>
struct FixedPoint16Cast;
#define VIGRA_FIXED_POINT_CAST(type) \
template <> \
struct FixedPoint16Cast<type> \
{ \
template <int IntBits, FPOverflowHandling OverflowHandling> \
static type cast(FixedPoint16<IntBits, OverflowHandling> v) \
{ \
return round(v); \
} \
};
VIGRA_FIXED_POINT_CAST(Int8)
VIGRA_FIXED_POINT_CAST(UInt8)
VIGRA_FIXED_POINT_CAST(Int16)
VIGRA_FIXED_POINT_CAST(UInt16)
VIGRA_FIXED_POINT_CAST(Int32)
VIGRA_FIXED_POINT_CAST(UInt32)
VIGRA_FIXED_POINT_CAST(Int64)
VIGRA_FIXED_POINT_CAST(UInt64)
#undef VIGRA_FIXED_POINT_CAST
template <>
struct FixedPoint16Cast<float>
{
template <int IntBits, FPOverflowHandling OverflowHandling>
static float cast(FixedPoint16<IntBits, OverflowHandling> v)
{
return (float)v.value / FixedPoint16<IntBits, OverflowHandling>::ONE;
}
};
template <>
struct FixedPoint16Cast<double>
{
template <int IntBits, FPOverflowHandling OverflowHandling>
static double cast(FixedPoint16<IntBits, OverflowHandling> v)
{
return (double)v.value / FixedPoint16<IntBits, OverflowHandling>::ONE;
}
};
} // namespace detail
/********************************************************/
/* */
/* FixedPoint16Operations */
/* */
/********************************************************/
/** \addtogroup FixedPoint16Operations Functions for FixedPoint16
\brief <b>\#include</b> \<<a href="fixedpoint_8hxx-source.html">vigra/fixedpoint.hxx</a>\><br>
These functions fulfill the requirements of an \ref AlgebraicRing.
Namespace: vigra
<p>
*/
//@{
/** Convert a FixedPoint16 to a built-in type.
If the target is integral, the value is rounded.<br>
Usage:
\code
FixedPoint16<16,15> fp(...);
double d = fixed_point_cast<double>(fp);
\endcode
*/
template <class TARGET, int IntBits, FPOverflowHandling OverflowHandling>
TARGET fixed_point_cast(FixedPoint16<IntBits, OverflowHandling> v)
{
return detail::FixedPoint16Cast<TARGET>::cast(v);
}
/// equal
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator==(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) == (r.value << (IntBits2 - MinIntBits));
}
/// not equal
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator!=(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) != (r.value << (IntBits2 - MinIntBits));
}
/// less than
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator<(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) < (r.value << (IntBits2 - MinIntBits));
}
/// less or equal
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator<=(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) <= (r.value << (IntBits2 - MinIntBits));
}
/// greater
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator>(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) > (r.value << (IntBits2 - MinIntBits));
}
/// greater or equal
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
bool operator>=(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
enum { MinIntBits = MetaMin<IntBits1, IntBits2>::value };
return (l.value << (IntBits1 - MinIntBits)) >= (r.value << (IntBits2 - MinIntBits));
}
/// addition with automatic determination of the appropriate result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
typename PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
operator+(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
typedef typename
PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
Result;
return Result(detail::FP16AddImpl<IntBits1, IntBits2, Result::INT_BITS, OverflowHandling>::exec(l.value, r.value), FPNoShift);
}
/// addition with enforced result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2, int IntBits3>
inline
FixedPoint16<IntBits3, OverflowHandling> &
add(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r,
FixedPoint16<IntBits3, OverflowHandling> & result)
{
result.value = detail::FP16AddImpl<IntBits1, IntBits2, IntBits3, OverflowHandling>::exec(l.value, r.value);
return result;
}
/// subtraction with automatic determination of the appropriate result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
typename PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
operator-(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
typedef typename
PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
Result;
return Result(detail::FP16SubImpl<IntBits1, IntBits2, Result::INT_BITS, OverflowHandling>::exec(l.value, r.value), FPNoShift);
}
/// subtraction with enforced result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2, int IntBits3>
inline FixedPoint16<IntBits3, OverflowHandling> &
sub(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r,
FixedPoint16<IntBits3, OverflowHandling> & result)
{
result.value = detail::FP16SubImpl<IntBits1, IntBits2, IntBits3, OverflowHandling>::exec(l.value, r.value);
return result;
}
/// multiplication with automatic determination of the appropriate result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
typename PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
operator*(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
typedef typename
PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
Result;
return Result(detail::FP16MulImpl<IntBits1, IntBits2, Result::INT_BITS, OverflowHandling>::exec(l.value, r.value), FPNoShift);
}
/// multiplication with enforced result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2, int IntBits3>
inline
FixedPoint16<IntBits3, OverflowHandling> &
mul(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r,
FixedPoint16<IntBits3, OverflowHandling> & result)
{
result.value = detail::FP16MulImpl<IntBits1, IntBits2, IntBits3, OverflowHandling>::exec(l.value, r.value);
return result;
}
/// division with automatic determination of the appropriate result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2>
inline
typename PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
operator/(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r)
{
typedef typename
PromoteTraits<FixedPoint16<IntBits1, OverflowHandling>, FixedPoint16<IntBits2, OverflowHandling> >::Promote
Result;
return Result(detail::FP16DivImpl<IntBits1, IntBits2, Result::INT_BITS, OverflowHandling>::exec(l.value, r.value), FPNoShift);
}
/// division with enforced result type.
template <int IntBits1, FPOverflowHandling OverflowHandling, int IntBits2, int IntBits3>
inline
FixedPoint16<IntBits3, OverflowHandling> &
div(FixedPoint16<IntBits1, OverflowHandling> l, FixedPoint16<IntBits2, OverflowHandling> r,
FixedPoint16<IntBits3, OverflowHandling> & result)
{
result.value = detail::FP16DivImpl<IntBits1, IntBits2, IntBits3, OverflowHandling>::exec(l.value, r.value);
return result;
}
/// square root.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline typename SquareRootTraits<FixedPoint16<IntBits, OverflowHandling> >::SquareRootResult
sqrt(FixedPoint16<IntBits, OverflowHandling> v)
{
typedef typename SquareRootTraits<FixedPoint16<IntBits, OverflowHandling> >::SquareRootResult Result;
enum { Shift = 15 + IntBits - 2*Result::INT_BITS };
return Result(sqrti(v.value << Shift), FPNoShift);
}
#ifndef VIGRA_NO_HYPOT
using ::hypot;
#endif
/// Length of hypothenuse.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline FixedPoint16<IntBits, OverflowHandling>
hypot(FixedPoint16<IntBits, OverflowHandling> v1, FixedPoint16<IntBits, OverflowHandling> v2)
{
UInt32 l = abs(v1.value), r = abs(v2.value);
// sq(l) + sq(r) <= 2**31, so overflow handling after sqrti is sufficient
return FixedPoint16<IntBits, OverflowHandling>(
detail::FP16OverflowHandling<OverflowHandling>::exec(sqrti(sq(l) + sq(r))),
FPNoShift);
}
using std::atan2;
/// Arctangent. Accuracy better than 1/3 degree (9 significant bits).
template <int IntBits, FPOverflowHandling OverflowHandling>
FixedPoint16<2, OverflowHandling>
atan2(FixedPoint16<IntBits, OverflowHandling> y, FixedPoint16<IntBits, OverflowHandling> x)
{
enum { ResIntBits = 2 };
typedef FixedPoint16<ResIntBits, OverflowHandling> FP;
static const FP zero(0), pi(M_PI), pi_2(0.5 * M_PI), mpi_2(-0.5 * M_PI);
static const Int32 Pi_4 = roundi(0.25 * M_PI * (1 << 15)), // 15 frac bits
Pi3_4 = roundi(0.75 * M_PI * (1 << 15)),
c1 = roundi(0.19826763260224867 * (1 << 15)),
c2 = roundi(-0.9757748231899761 * (1 << 30));
// coefficients c1 and c2 minimize
//
// NIntegrate[(c1 r^3 + c2 r + Pi/4 - a)^4 /. r -> (Cos[a] - Sin[a])/(Cos[a] + Sin[a]), {a, 0, Pi/2}]
//
// Thanks to Jim Shima, http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm
if(x.value == 0)
return (y.value > 0)
? pi_2
: (y.value < 0)
? mpi_2
: zero;
Int32 abs_y = abs(y.value);
Int32 r, angle;
if(x.value > 0)
{
if(y.value == 0)
return zero;
r = ((x.value - abs_y) << 15) / (x.value + abs_y); // 15 frac bits
angle = Pi_4;
}
else
{
if(y.value == 0)
return pi;
r = ((x.value + abs_y) << 15) / (abs_y - x.value); // 15 frac bits
angle = Pi3_4;
}
angle += r*((c2 + c1 * (sq(r) >> 15)) >> 15) >> 15;
return (y.value > 0)
? FP(detail::FP16Align<0, ResIntBits, true>::exec( angle), FPNoShift)
: FP(detail::FP16Align<0, ResIntBits, true>::exec(-angle), FPNoShift);
}
/// absolute value.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline FixedPoint16<IntBits, OverflowHandling>
abs(FixedPoint16<IntBits, OverflowHandling> v)
{
return FixedPoint16<IntBits, OverflowHandling>(abs(v.value), FPNoShift);
}
/// squared norm (same as v*v).
template <int IntBits, FPOverflowHandling OverflowHandling>
inline
typename NormTraits<FixedPoint16<IntBits, OverflowHandling> >::SquaredNormType
squaredNorm(FixedPoint16<IntBits, OverflowHandling> v)
{
return v*v;
}
/// norm (same as abs).
template <int IntBits, FPOverflowHandling OverflowHandling>
inline
typename NormTraits<FixedPoint16<IntBits, OverflowHandling> >::NormType
norm(FixedPoint16<IntBits, OverflowHandling> const & v)
{
return abs(v);
}
/// fractional part. (difference between v and its floor)
template <int IntBits, FPOverflowHandling OverflowHandling>
inline FixedPoint16<IntBits, OverflowHandling>
frac(FixedPoint16<IntBits, OverflowHandling> v)
{
return FixedPoint16<IntBits, OverflowHandling>(
v.value - (v.value & FixedPoint16<IntBits, OverflowHandling>::INT_MASK),
FPNoShift);
}
/// dual fractional part. (1 - frac(v))
template <int IntBits, FPOverflowHandling OverflowHandling>
inline FixedPoint16<IntBits, OverflowHandling>
dual_frac(FixedPoint16<IntBits, OverflowHandling> v)
{
return FixedPoint16<IntBits, OverflowHandling>(
FixedPoint16<IntBits, OverflowHandling>::ONE - v.value + (v.value & FixedPoint16<IntBits, OverflowHandling>::INT_MASK),
FPNoShift);
}
/// rounding down.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline Int32
floor(FixedPoint16<IntBits, OverflowHandling> v)
{
return(v.value >> FixedPoint16<IntBits, OverflowHandling>::FRACTIONAL_BITS);
}
/// rounding up.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline Int32
ceil(FixedPoint16<IntBits, OverflowHandling> v)
{
return((v.value + FixedPoint16<IntBits, OverflowHandling>::FRACTIONAL_MASK) >>
FixedPoint16<IntBits, OverflowHandling>::FRACTIONAL_BITS);
}
/// rounding to the nearest integer.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline Int32
round(FixedPoint16<IntBits, OverflowHandling> v)
{
return((v.value + FixedPoint16<IntBits, OverflowHandling>::ONE_HALF) >>
FixedPoint16<IntBits, OverflowHandling>::FRACTIONAL_BITS);
}
/// rounding to the nearest integer.
template <int IntBits, FPOverflowHandling OverflowHandling>
inline Int32
roundi(FixedPoint16<IntBits, OverflowHandling> v)
{
return round(v);
}
//@}
} // namespace vigra
namespace std {
template <int IntBits, vigra::FPOverflowHandling OverflowHandling>
ostream & operator<<(ostream & s, vigra::FixedPoint16<IntBits, OverflowHandling> v)
{
s << vigra::fixed_point_cast<float>(v);
return s;
}
} // namespace std
#endif // VIGRA_FIXEDPOINT_HXX
|