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 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
// Copyright © 2018–2024 Trevor Spiteri
// This library is free software: you can redistribute it and/or
// modify it under the terms of either
//
// * the Apache License, Version 2.0 or
// * the MIT License
//
// at your option.
//
// You should have recieved copies of the Apache License and the MIT
// License along with the library. If not, see
// <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>.
use crate::{
from_str::ParseFixedError,
traits::{Fixed, FixedSigned, FixedUnsigned, FromFixed, ToFixed},
types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
FixedU8,
};
use core::{
fmt::{
Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, Result as FmtResult,
UpperExp, UpperHex,
},
iter::{Product, Sum},
mem,
ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
SubAssign,
},
str::FromStr,
};
/// Provides intentionally wrapped arithmetic on fixed-point numbers.
///
/// The underlying value can be retrieved through the `.0` index.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let max = Wrapping(I16F16::MAX);
/// let delta = Wrapping(I16F16::DELTA);
/// assert_eq!(I16F16::MIN, (max + delta).0);
/// ```
#[repr(transparent)]
#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Wrapping<F>(pub F);
impl<F: Fixed> Wrapping<F> {
/// Zero.
///
/// See also <code>FixedI32::[ZERO][FixedI32::ZERO]</code> and
/// <code>FixedU32::[ZERO][FixedU32::ZERO]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::ZERO, Wrapping(I16F16::ZERO));
/// ```
pub const ZERO: Wrapping<F> = Wrapping(F::ZERO);
/// The difference between any two successive representable numbers, <i>Δ</i>.
///
/// See also <code>FixedI32::[DELTA][FixedI32::DELTA]</code> and
/// <code>FixedU32::[DELTA][FixedU32::DELTA]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::DELTA, Wrapping(I16F16::DELTA));
/// ```
pub const DELTA: Wrapping<F> = Wrapping(F::DELTA);
/// The smallest value that can be represented.
///
/// See also <code>FixedI32::[MIN][FixedI32::MIN]</code> and
/// <code>FixedU32::[MIN][FixedU32::MIN]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::MIN, Wrapping(I16F16::MIN));
/// ```
pub const MIN: Wrapping<F> = Wrapping(F::MIN);
/// The largest value that can be represented.
///
/// See also <code>FixedI32::[MAX][FixedI32::MAX]</code> and
/// <code>FixedU32::[MAX][FixedU32::MAX]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::MAX, Wrapping(I16F16::MAX));
/// ```
pub const MAX: Wrapping<F> = Wrapping(F::MAX);
/// [`true`] if the type is signed.
///
/// See also <code>FixedI32::[IS\_SIGNED][FixedI32::IS_SIGNED]</code> and
/// <code>FixedU32::[IS\_SIGNED][FixedU32::IS_SIGNED]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert!(Wrapping::<I16F16>::IS_SIGNED);
/// assert!(!Wrapping::<U16F16>::IS_SIGNED);
/// ```
pub const IS_SIGNED: bool = F::IS_SIGNED;
/// The number of integer bits.
///
/// See also <code>FixedI32::[INT\_NBITS][FixedI32::INT_NBITS]</code> and
/// <code>FixedU32::[INT\_NBITS][FixedU32::INT_NBITS]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::INT_NBITS, I16F16::INT_NBITS);
/// ```
pub const INT_NBITS: u32 = F::INT_NBITS;
/// The number of fractional bits.
///
/// See also <code>FixedI32::[FRAC\_NBITS][FixedI32::FRAC_NBITS]</code> and
/// <code>FixedU32::[FRAC\_NBITS][FixedU32::FRAC_NBITS]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::FRAC_NBITS, I16F16::FRAC_NBITS);
/// ```
pub const FRAC_NBITS: u32 = F::FRAC_NBITS;
/// Creates a fixed-point number that has a bitwise representation
/// identical to the given integer.
///
/// See also <code>FixedI32::[from\_bits][FixedI32::from_bits]</code> and
/// <code>FixedU32::[from\_bits][FixedU32::from_bits]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping::<I16F16>::from_bits(0x1C), Wrapping(I16F16::from_bits(0x1C)));
/// ```
#[inline]
pub fn from_bits(bits: F::Bits) -> Wrapping<F> {
Wrapping(F::from_bits(bits))
}
/// Creates an integer that has a bitwise representation identical
/// to the given fixed-point number.
///
/// See also <code>FixedI32::[to\_bits][FixedI32::to_bits]</code> and
/// <code>FixedU32::[to\_bits][FixedU32::to_bits]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1C));
/// assert_eq!(w.to_bits(), 0x1C);
/// ```
#[inline]
pub fn to_bits(self) -> F::Bits {
self.0.to_bits()
}
/// Converts a fixed-point number from big endian to the target’s
/// endianness.
///
/// See also <code>FixedI32::[from\_be][FixedI32::from_be]</code> and
/// <code>FixedU32::[from\_be][FixedU32::from_be]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1234_5678));
/// if cfg!(target_endian = "big") {
/// assert_eq!(Wrapping::from_be(w), w);
/// } else {
/// assert_eq!(Wrapping::from_be(w), w.swap_bytes());
/// }
/// ```
#[inline]
pub fn from_be(w: Self) -> Self {
Wrapping(F::from_be(w.0))
}
/// Converts a fixed-point number from little endian to the
/// target’s endianness.
///
/// See also <code>FixedI32::[from\_le][FixedI32::from_le]</code> and
/// <code>FixedU32::[from\_le][FixedU32::from_le]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1234_5678));
/// if cfg!(target_endian = "little") {
/// assert_eq!(Wrapping::from_le(w), w);
/// } else {
/// assert_eq!(Wrapping::from_le(w), w.swap_bytes());
/// }
/// ```
#[inline]
pub fn from_le(w: Self) -> Self {
Wrapping(F::from_le(w.0))
}
/// Converts `self` to big endian from the target’s endianness.
///
/// See also <code>FixedI32::[to\_be][FixedI32::to_be]</code> and
/// <code>FixedU32::[to\_be][FixedU32::to_be]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1234_5678));
/// if cfg!(target_endian = "big") {
/// assert_eq!(w.to_be(), w);
/// } else {
/// assert_eq!(w.to_be(), w.swap_bytes());
/// }
/// ```
#[inline]
#[must_use]
pub fn to_be(self) -> Self {
Wrapping(self.0.to_be())
}
/// Converts `self` to little endian from the target’s endianness.
///
/// See also <code>FixedI32::[to\_le][FixedI32::to_le]</code> and
/// <code>FixedU32::[to\_le][FixedU32::to_le]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1234_5678));
/// if cfg!(target_endian = "little") {
/// assert_eq!(w.to_le(), w);
/// } else {
/// assert_eq!(w.to_le(), w.swap_bytes());
/// }
/// ```
#[inline]
#[must_use]
pub fn to_le(self) -> Self {
Wrapping(self.0.to_le())
}
/// Reverses the byte order of the fixed-point number.
///
/// See also <code>FixedI32::[swap\_bytes][FixedI32::swap_bytes]</code> and
/// <code>FixedU32::[swap\_bytes][FixedU32::swap_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x1234_5678));
/// let swapped = Wrapping(I16F16::from_bits(0x7856_3412));
/// assert_eq!(w.swap_bytes(), swapped);
/// ```
#[inline]
#[must_use]
pub fn swap_bytes(self) -> Self {
Wrapping(self.0.swap_bytes())
}
/// Creates a fixed-point number from its representation
/// as a byte array in big endian.
///
/// See also
/// <code>FixedI32::[from\_be\_bytes][FixedI32::from_be_bytes]</code> and
/// <code>FixedU32::[from\_be\_bytes][FixedU32::from_be_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let bytes = [0x12, 0x34, 0x56, 0x78];
/// assert_eq!(
/// Wrapping::<I16F16>::from_be_bytes(bytes),
/// Wrapping::<I16F16>::from_bits(0x1234_5678)
/// );
/// ```
#[inline]
pub fn from_be_bytes(bytes: F::Bytes) -> Self {
Wrapping(F::from_be_bytes(bytes))
}
/// Creates a fixed-point number from its representation
/// as a byte array in little endian.
///
/// See also
/// <code>FixedI32::[from\_le\_bytes][FixedI32::from_le_bytes]</code> and
/// <code>FixedU32::[from\_le\_bytes][FixedU32::from_le_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let bytes = [0x78, 0x56, 0x34, 0x12];
/// assert_eq!(
/// Wrapping::<I16F16>::from_le_bytes(bytes),
/// Wrapping::<I16F16>::from_bits(0x1234_5678)
/// );
/// ```
#[inline]
pub fn from_le_bytes(bytes: F::Bytes) -> Self {
Wrapping(F::from_le_bytes(bytes))
}
/// Creates a fixed-point number from its representation
/// as a byte array in native endian.
///
/// See also
/// <code>FixedI32::[from\_ne\_bytes][FixedI32::from_ne_bytes]</code> and
/// <code>FixedU32::[from\_ne\_bytes][FixedU32::from_ne_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let bytes = if cfg!(target_endian = "big") {
/// [0x12, 0x34, 0x56, 0x78]
/// } else {
/// [0x78, 0x56, 0x34, 0x12]
/// };
/// assert_eq!(
/// Wrapping::<I16F16>::from_ne_bytes(bytes),
/// Wrapping::<I16F16>::from_bits(0x1234_5678)
/// );
/// ```
#[inline]
pub fn from_ne_bytes(bytes: F::Bytes) -> Self {
Wrapping(F::from_ne_bytes(bytes))
}
/// Returns the memory representation of this fixed-point
/// number as a byte array in big-endian byte order.
///
/// See also <code>FixedI32::[to\_be\_bytes][FixedI32::to_be_bytes]</code>
/// and <code>FixedU32::[to\_be\_bytes][FixedU32::to_be_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(
/// Wrapping::<I16F16>::from_bits(0x1234_5678).to_be_bytes(),
/// [0x12, 0x34, 0x56, 0x78]
/// );
/// ```
#[inline]
pub fn to_be_bytes(self) -> F::Bytes {
self.0.to_be_bytes()
}
/// Returns the memory representation of this fixed-point
/// number as a byte array in little-endian byte order.
///
/// See also <code>FixedI32::[to\_le\_bytes][FixedI32::to_le_bytes]</code>
/// and <code>FixedU32::[to\_le\_bytes][FixedU32::to_le_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(
/// Wrapping::<I16F16>::from_bits(0x1234_5678).to_le_bytes(),
/// [0x78, 0x56, 0x34, 0x12]
/// );
/// ```
#[inline]
pub fn to_le_bytes(self) -> F::Bytes {
self.0.to_le_bytes()
}
/// Returns the memory representation of this fixed-point
/// number as a byte array in native-endian byte order.
///
/// See also <code>FixedI32::[to\_ne\_bytes][FixedI32::to_ne_bytes]</code>
/// and <code>FixedU32::[to\_ne\_bytes][FixedU32::to_ne_bytes]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let bytes = if cfg!(target_endian = "big") {
/// [0x12, 0x34, 0x56, 0x78]
/// } else {
/// [0x78, 0x56, 0x34, 0x12]
/// };
/// assert_eq!(
/// Wrapping::<I16F16>::from_bits(0x1234_5678).to_ne_bytes(),
/// bytes
/// );
/// ```
#[inline]
pub fn to_ne_bytes(self) -> F::Bytes {
self.0.to_ne_bytes()
}
/// Wrapping conversion from another number.
///
/// The other number can be:
///
/// * A fixed-point number. Any extra fractional bits are
/// discarded, which rounds towards −∞.
/// * An integer of type [`i8`], [`i16`], [`i32`], [`i64`], [`i128`],
/// [`isize`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], or
/// [`usize`].
/// * A floating-point number of type [`f16`], [`bf16`],
/// [`f32`], [`f64`] or [`F128`]. For this conversion, the
/// method rounds to the nearest, with ties rounding to even.
/// * Any other number `src` for which [`ToFixed`] is
/// implemented, in which case this method returns
/// <code>[Wrapping]\(src.[wrapping\_to\_fixed][ToFixed::wrapping_to_fixed]\())</code>.
///
/// See also
/// <code>FixedI32::[wrapping\_from\_num][FixedI32::wrapping_from_num]</code>
/// and
/// <code>FixedU32::[wrapping\_from\_num][FixedU32::wrapping_from_num]</code>.
///
/// # Panics
///
/// For floating-point numbers, panics if the value is not [finite].
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I4F4, I16F16},
/// Wrapping,
/// };
///
/// // 0x1234.5678 wraps into 0x4.5
/// let src = I16F16::from_bits(0x1234_5678);
/// let dst = Wrapping::<I4F4>::from_num(src);
/// assert_eq!(dst, Wrapping(I4F4::from_bits(0x45)));
///
/// // 0x1234 wraps into 0x4.0
/// let src_int = 0x1234_i32;
/// let dst_int = Wrapping::<I4F4>::from_num(src_int);
/// assert_eq!(dst_int, Wrapping(I4F4::from_bits(0x40)));
///
/// // 129.75 wrapped into 1.75 (binary 1.1100)
/// let src_float = 129.75;
/// let dst_float = Wrapping::<I4F4>::from_num(src_float);
/// assert_eq!(dst_float, Wrapping(I4F4::from_bits(0b11100)));
/// ```
///
/// [`F128`]: crate::F128
/// [`bf16`]: half::bf16
/// [`f16`]: half::f16
/// [finite]: f64::is_finite
#[inline]
#[track_caller]
pub fn from_num<Src: ToFixed>(src: Src) -> Wrapping<F> {
Wrapping(src.wrapping_to_fixed())
}
/// Converts a fixed-point number to another number, wrapping the
/// value on overflow.
///
/// The other number can be:
///
/// * Another fixed-point number. Any extra fractional bits are
/// discarded, which rounds towards −∞.
/// * An integer of type [`i8`], [`i16`], [`i32`], [`i64`], [`i128`],
/// [`isize`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], or
/// [`usize`]. Any fractional bits are discarded, which rounds
/// towards −∞.
/// * A floating-point number of type [`f16`], [`bf16`],
/// [`f32`], [`f64`] or [`F128`]. For this conversion, the
/// method rounds to the nearest, with ties rounding to even.
/// * Any other type `Dst` for which [`FromFixed`] is
/// implemented, in which case this method returns
/// <code>Dst::[wrapping\_from\_fixed][FromFixed::wrapping_from_fixed]\(self.0)</code>.
///
/// See also <code>FixedI32::[wrapping\_to\_num][FixedI32::wrapping_to_num]</code> and
/// <code>FixedU32::[wrapping\_to\_num][FixedU32::wrapping_to_num]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, I2F6, I4F4},
/// Wrapping,
/// };
///
/// // conversion that fits
/// let src = Wrapping(I4F4::from_num(1.75));
/// let expected = I16F16::from_num(1.75);
/// assert_eq!(src.to_num::<I16F16>(), expected);
///
/// // conversion that wraps
/// let src = Wrapping(I4F4::MAX);
/// let wrapped = I2F6::from_bits(I2F6::MAX.to_bits() << 2);
/// assert_eq!(src.to_num::<I2F6>(), wrapped);
/// ```
///
/// [`F128`]: crate::F128
/// [`bf16`]: half::bf16
/// [`f16`]: half::f16
#[inline]
pub fn to_num<Dst: FromFixed>(self) -> Dst {
Dst::wrapping_from_fixed(self.0)
}
/// Parses a string slice containing binary digits to return a fixed-point number.
///
/// Rounding is to the nearest, with ties rounded to even.
///
/// See also
/// <code>FixedI32::[wrapping\_from\_str\_binary][FixedI32::wrapping_from_str_binary]</code>
/// and
/// <code>FixedU32::[wrapping\_from\_str\_binary][FixedU32::wrapping_from_str_binary]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I8F8, Wrapping};
/// let check = Wrapping(I8F8::from_bits(0b1110001 << (8 - 1)));
/// assert_eq!(Wrapping::<I8F8>::from_str_binary("101100111000.1"), Ok(check));
/// ```
#[inline]
pub fn from_str_binary(src: &str) -> Result<Wrapping<F>, ParseFixedError> {
F::wrapping_from_str_binary(src).map(Wrapping)
}
/// Parses a string slice containing octal digits to return a fixed-point number.
///
/// Rounding is to the nearest, with ties rounded to even.
///
/// See also
/// <code>FixedI32::[wrapping\_from\_str\_octal][FixedI32::wrapping_from_str_octal]</code>
/// and
/// <code>FixedU32::[wrapping\_from\_str\_octal][FixedU32::wrapping_from_str_octal]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I8F8, Wrapping};
/// let check = Wrapping(I8F8::from_bits(0o1654 << (8 - 3)));
/// assert_eq!(Wrapping::<I8F8>::from_str_octal("7165.4"), Ok(check));
/// ```
#[inline]
pub fn from_str_octal(src: &str) -> Result<Wrapping<F>, ParseFixedError> {
F::wrapping_from_str_octal(src).map(Wrapping)
}
/// Parses a string slice containing hexadecimal digits to return a fixed-point number.
///
/// Rounding is to the nearest, with ties rounded to even.
///
/// See also
/// <code>FixedI32::[wrapping\_from\_str\_hex][FixedI32::wrapping_from_str_hex]</code>
/// and
/// <code>FixedU32::[wrapping\_from\_str\_hex][FixedU32::wrapping_from_str_hex]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I8F8, Wrapping};
/// let check = Wrapping(I8F8::from_bits(0xFFE));
/// assert_eq!(Wrapping::<I8F8>::from_str_hex("C0F.FE"), Ok(check));
/// ```
#[inline]
pub fn from_str_hex(src: &str) -> Result<Wrapping<F>, ParseFixedError> {
F::wrapping_from_str_hex(src).map(Wrapping)
}
/// Returns the integer part.
///
/// Note that since the numbers are stored in two’s complement,
/// negative numbers with non-zero fractional parts will be
/// rounded towards −∞, except in the case where there are no
/// integer bits, for example for the type
/// <code>[Wrapping]<[I0F16]></code>, where the return value
/// is always zero.
///
/// See also <code>FixedI32::[int][FixedI32::int]</code> and
/// <code>FixedU32::[int][FixedU32::int]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping(I16F16::from_num(12.25)).int(), Wrapping(I16F16::from_num(12)));
/// assert_eq!(Wrapping(I16F16::from_num(-12.25)).int(), Wrapping(I16F16::from_num(-13)));
/// ```
///
/// [I0F16]: crate::types::I0F16
#[inline]
#[must_use]
pub fn int(self) -> Wrapping<F> {
Wrapping(self.0.int())
}
/// Returns the fractional part.
///
/// Note that since the numbers are stored in two’s complement,
/// the returned fraction will be non-negative for negative
/// numbers, except in the case where there are no integer bits,
/// for example for the type
/// <code>[Wrapping]<[I0F16]></code>,
/// where the return value is always equal to `self`.
///
/// See also <code>FixedI32::[frac][FixedI32::frac]</code> and
/// <code>FixedU32::[frac][FixedU32::frac]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping(I16F16::from_num(12.25)).frac(), Wrapping(I16F16::from_num(0.25)));
/// assert_eq!(Wrapping(I16F16::from_num(-12.25)).frac(), Wrapping(I16F16::from_num(0.75)));
/// ```
///
/// [I0F16]: crate::types::I0F16
#[inline]
#[must_use]
pub fn frac(self) -> Wrapping<F> {
Wrapping(self.0.frac())
}
/// Rounds to the next integer towards 0.
///
/// See also
/// <code>FixedI32::[round\_to\_zero][FixedI32::round_to_zero]</code> and
/// <code>FixedU32::[round\_to\_zero][FixedU32::round_to_zero]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let three = Wrapping(I16F16::from_num(3));
/// assert_eq!(Wrapping(I16F16::from_num(3.9)).round_to_zero(), three);
/// assert_eq!(Wrapping(I16F16::from_num(-3.9)).round_to_zero(), -three);
/// ```
#[inline]
#[must_use]
pub fn round_to_zero(self) -> Wrapping<F> {
Wrapping(self.0.round_to_zero())
}
/// Wrapping ceil. Rounds to the next integer towards +∞, wrapping
/// on overflow.
///
/// See also
/// <code>FixedI32::[wrapping\_ceil][FixedI32::wrapping_ceil]</code> and
/// <code>FixedU32::[wrapping\_ceil][FixedU32::wrapping_ceil]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.ceil(), Wrapping(I16F16::from_num(3)));
/// assert_eq!(Wrapping(I16F16::MAX).ceil(), Wrapping(I16F16::MIN));
/// ```
#[inline]
#[must_use]
pub fn ceil(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_ceil())
}
/// Wrapping floor. Rounds to the next integer towards −∞,
/// wrapping on overflow.
///
/// Overflow can only occur for signed numbers with zero integer
/// bits.
///
/// See also
/// <code>FixedI32::[wrapping\_floor][FixedI32::wrapping_floor]</code> and
/// <code>FixedU32::[wrapping\_floor][FixedU32::wrapping_floor]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I0F32, I16F16},
/// Wrapping,
/// };
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.floor(), Wrapping(I16F16::from_num(2)));
/// assert_eq!(Wrapping(I0F32::MIN).floor(), Wrapping(I0F32::ZERO));
/// ```
#[inline]
#[must_use]
pub fn floor(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_floor())
}
/// Wrapping round. Rounds to the next integer to the nearest,
/// with ties rounded away from zero, and wrapping on overflow.
///
/// See also
/// <code>FixedI32::[wrapping\_round][FixedI32::wrapping_round]</code> and
/// <code>FixedU32::[wrapping\_round][FixedU32::wrapping_round]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let two_half = Wrapping(I16F16::from_num(5) / 2);
/// assert_eq!(two_half.round(), Wrapping(I16F16::from_num(3)));
/// assert_eq!((-two_half).round(), Wrapping(I16F16::from_num(-3)));
/// assert_eq!(Wrapping(I16F16::MAX).round(), Wrapping(I16F16::MIN));
/// ```
#[inline]
#[must_use]
pub fn round(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_round())
}
/// Wrapping round. Rounds to the next integer to the nearest,
/// with ties rounded to even, and wrapping on overflow.
///
/// See also
/// <code>FixedI32::[wrapping\_round\_ties\_to\_even][FixedI32::wrapping_round_ties_to_even]</code>
/// and
/// <code>FixedU32::[wrapping\_round\_ties\_to\_even][FixedU32::wrapping_round_ties_to_even]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let two_half = Wrapping(I16F16::from_num(2.5));
/// assert_eq!(two_half.round_ties_to_even(), Wrapping(I16F16::from_num(2)));
/// let three_half = Wrapping(I16F16::from_num(3.5));
/// assert_eq!(three_half.round_ties_to_even(), Wrapping(I16F16::from_num(4)));
/// let max = Wrapping(I16F16::MAX);
/// assert_eq!(max.round_ties_to_even(), Wrapping(I16F16::MIN));
/// ```
#[inline]
#[must_use]
#[doc(alias("round_ties_even"))]
pub fn round_ties_to_even(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_round_ties_to_even())
}
/// Returns the number of ones in the binary representation.
///
/// See also <code>FixedI32::[count\_ones][FixedI32::count_ones]</code> and
/// <code>FixedU32::[count\_ones][FixedU32::count_ones]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x00FF_FF00));
/// assert_eq!(w.count_ones(), w.0.count_ones());
/// ```
#[inline]
#[doc(alias("popcount", "popcnt"))]
pub fn count_ones(self) -> u32 {
self.0.count_ones()
}
/// Returns the number of zeros in the binary representation.
///
/// See also <code>FixedI32::[count\_zeros][FixedI32::count_zeros]</code>
/// and <code>FixedU32::[count\_zeros][FixedU32::count_zeros]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x00FF_FF00));
/// assert_eq!(w.count_zeros(), w.0.count_zeros());
/// ```
#[inline]
pub fn count_zeros(self) -> u32 {
self.0.count_zeros()
}
/// Returns the number of leading ones in the binary representation.
///
/// See also <code>FixedI32::[leading\_ones][FixedI32::leading_ones]</code>
/// and <code>FixedU32::[leading\_ones][FixedU32::leading_ones]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U16F16, Wrapping};
/// let w = Wrapping(U16F16::from_bits(0xFF00_00FF));
/// assert_eq!(w.leading_ones(), w.0.leading_ones());
/// ```
#[inline]
pub fn leading_ones(self) -> u32 {
self.0.leading_ones()
}
/// Returns the number of leading zeros in the binary representation.
///
/// See also
/// <code>FixedI32::[leading\_zeros][FixedI32::leading_zeros]</code> and
/// <code>FixedU32::[leading\_zeros][FixedU32::leading_zeros]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x00FF_FF00));
/// assert_eq!(w.leading_zeros(), w.0.leading_zeros());
/// ```
#[inline]
pub fn leading_zeros(self) -> u32 {
self.0.leading_zeros()
}
/// Returns the number of trailing ones in the binary representation.
///
/// See also
/// <code>FixedI32::[trailing\_ones][FixedI32::trailing_ones]</code> and
/// <code>FixedU32::[trailing\_ones][FixedU32::trailing_ones]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U16F16, Wrapping};
/// let w = Wrapping(U16F16::from_bits(0xFF00_00FF));
/// assert_eq!(w.trailing_ones(), w.0.trailing_ones());
/// ```
#[inline]
pub fn trailing_ones(self) -> u32 {
self.0.trailing_ones()
}
/// Returns the number of trailing zeros in the binary representation.
///
/// See also
/// <code>FixedI32::[trailing\_zeros][FixedI32::trailing_zeros]</code> and
/// <code>FixedU32::[trailing\_zeros][FixedU32::trailing_zeros]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let w = Wrapping(I16F16::from_bits(0x00FF_FF00));
/// assert_eq!(w.trailing_zeros(), w.0.trailing_zeros());
/// ```
#[inline]
pub fn trailing_zeros(self) -> u32 {
self.0.trailing_zeros()
}
/// Returns the square root.
///
/// See also
/// <code>FixedI32::[wrapping\_sqrt][FixedI32::wrapping_sqrt]</code> and
/// <code>FixedU32::[wrapping\_sqrt][FixedU32::wrapping_sqrt]</code>.
///
/// # Panics
///
/// Panics if the number is negative.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I0F32, Wrapping};
/// assert_eq!(Wrapping(I0F32::lit("0b0.0001")).sqrt().0, I0F32::lit("0b0.01"));
///
/// // This method handles the overflow corner case.
/// let w = Wrapping(I0F32::from_num(0.25));
/// assert_eq!(w.sqrt().0, -0.5);
/// ```
#[inline]
#[track_caller]
pub fn sqrt(self) -> Self {
Wrapping(self.0.wrapping_sqrt())
}
/// Integer base-2 logarithm, rounded down.
///
/// See also <code>FixedI32::[int\_log2][FixedI32::int_log2]</code> and
/// <code>FixedU32::[int\_log2][FixedU32::int_log2]</code>.
///
/// # Panics
///
/// Panics if the fixed-point number is ≤ 0.
#[inline]
#[track_caller]
#[doc(alias("ilog2"))]
pub fn int_log2(self) -> i32 {
self.0.int_log2()
}
/// Integer base-10 logarithm, rounded down.
///
/// See also <code>FixedI32::[int\_log10][FixedI32::int_log10]</code> and
/// <code>FixedU32::[int\_log10][FixedU32::int_log10]</code>.
///
/// # Panics
///
/// Panics if the fixed-point number is ≤ 0.
#[inline]
#[track_caller]
#[doc(alias("ilog10"))]
pub fn int_log10(self) -> i32 {
self.0.int_log10()
}
/// Integer logarithm to the specified base, rounded down.
///
/// See also <code>FixedI32::[int\_log][FixedI32::int_log]</code> and
/// <code>FixedU32::[int\_log][FixedU32::int_log]</code>.
///
/// # Panics
///
/// Panics if the fixed-point number is ≤ 0 or if the base is < 2.
#[inline]
#[track_caller]
#[doc(alias("ilog"))]
pub fn int_log(self, base: u32) -> i32 {
self.0.int_log(base)
}
/// Reverses the order of the bits of the fixed-point number.
///
/// See also <code>FixedI32::[reverse\_bits][FixedI32::reverse_bits]</code>
/// and <code>FixedU32::[reverse\_bits][FixedU32::reverse_bits]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let i = I16F16::from_bits(0x1234_5678);
/// assert_eq!(Wrapping(i).reverse_bits(), Wrapping(i.reverse_bits()));
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn reverse_bits(self) -> Wrapping<F> {
Wrapping(self.0.reverse_bits())
}
/// Shifts to the left by `n` bits, wrapping the truncated bits to the right end.
///
/// See also <code>FixedI32::[rotate\_left][FixedI32::rotate_left]</code>
/// and <code>FixedU32::[rotate\_left][FixedU32::rotate_left]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let i = I16F16::from_bits(0x00FF_FF00);
/// assert_eq!(Wrapping(i).rotate_left(12), Wrapping(i.rotate_left(12)));
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn rotate_left(self, n: u32) -> Wrapping<F> {
Wrapping(self.0.rotate_left(n))
}
/// Shifts to the right by `n` bits, wrapping the truncated bits to the left end.
///
/// See also <code>FixedI32::[rotate\_right][FixedI32::rotate_right]</code>
/// and <code>FixedU32::[rotate\_right][FixedU32::rotate_right]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let i = I16F16::from_bits(0x00FF_FF00);
/// assert_eq!(Wrapping(i).rotate_right(12), Wrapping(i.rotate_right(12)));
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn rotate_right(self, n: u32) -> Wrapping<F> {
Wrapping(self.0.rotate_right(n))
}
/// Returns [`true`] if the number is zero.
///
/// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
/// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(Wrapping(I16F16::ZERO).is_zero());
/// assert!(!Wrapping(I16F16::from_num(4.3)).is_zero());
/// ```
#[inline]
pub fn is_zero(self) -> bool {
self.0.is_zero()
}
/// Returns the distance from `self` to `other`.
///
/// See also
/// <code>FixedI32::[wrapping\_dist][FixedI32::wrapping_dist]</code> and
/// <code>FixedU32::[wrapping\_dist][FixedU32::wrapping_dist]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// type Wr = Wrapping<I16F16>;
/// assert_eq!(Wr::from_num(-1).dist(Wr::from_num(4)), Wr::from_num(5));
/// assert_eq!(Wr::MIN.dist(Wr::MAX), -Wr::DELTA);
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn dist(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_dist(other.0))
}
/// Returns the mean of `self` and `other`.
///
/// See also <code>FixedI32::[mean][FixedI32::mean]</code> and
/// <code>FixedU32::[mean][FixedU32::mean]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let three = Wrapping(I16F16::from_num(3));
/// let four = Wrapping(I16F16::from_num(4));
/// assert_eq!(three.mean(four), Wrapping(I16F16::from_num(3.5)));
/// assert_eq!(three.mean(-four), Wrapping(I16F16::from_num(-0.5)));
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn mean(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.mean(other.0))
}
/// Compute the hypotenuse of a right triange.
///
/// See also
/// <code>FixedI32::[wrapping\_hypot][FixedI32::wrapping_hypot]</code> and
/// <code>FixedU32::[wrapping\_hypot][FixedU32::wrapping_hypot]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I8F8, Wrapping};
/// type Wr = Wrapping<I8F8>;
/// // hypot(3, 4) == 5
/// assert_eq!(Wr::from_num(3).hypot(Wr::from_num(4)), Wr::from_num(5));
/// // hypot(88, 105) == 137, which wraps to -119
/// assert_eq!(Wr::from_num(88).hypot(Wr::from_num(105)), Wr::from_num(-119));
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn hypot(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_hypot(other.0))
}
/// Returns the reciprocal (inverse), 1/`self`.
///
/// See also
/// <code>FixedI32::[wrapping\_recip][FixedI32::wrapping_recip]</code> and
/// <code>FixedU32::[wrapping\_recip][FixedU32::wrapping_recip]</code>.
///
/// # Panics
///
/// Panics if `self` is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I8F24, Wrapping};
/// let quarter = Wrapping(I8F24::from_num(0.25));
/// let frac_1_512 = Wrapping(I8F24::ONE / 512);
/// assert_eq!(quarter.recip(), Wrapping(I8F24::from_num(4)));
/// assert_eq!(frac_1_512.recip(), Wrapping(I8F24::ZERO));
/// ```
#[inline]
#[track_caller]
#[must_use]
pub fn recip(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_recip())
}
/// Returns the next multiple of `other`.
///
/// See also
/// <code>FixedI32::[wrapping\_next\_multiple\_of][FixedI32::wrapping_next_multiple_of]</code>
/// and
/// <code>FixedU32::[wrapping\_next\_multiple\_of][FixedU32::wrapping_next_multiple_of]</code>.
///
/// # Panics
///
/// Panics if `other` is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let one_point_5 = Wrapping::<I16F16>::from_num(1.5);
/// let four = Wrapping::<I16F16>::from_num(4);
/// let four_point_5 = Wrapping::<I16F16>::from_num(4.5);
/// assert_eq!(four.next_multiple_of(one_point_5), four_point_5);
///
/// let max = Wrapping::<I16F16>::MAX;
/// let max_minus_delta = max - Wrapping::<I16F16>::DELTA;
/// assert_eq!(max.next_multiple_of(max_minus_delta), max_minus_delta * 2);
/// ```
#[inline]
#[track_caller]
#[must_use]
pub fn next_multiple_of(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_next_multiple_of(other.0))
}
/// Multiply and add. Returns `self` × `mul` + `add`.
///
/// See also
/// <code>FixedI32::[wrapping\_mul\_add][FixedI32::wrapping_mul_add]</code>
/// and
/// <code>FixedU32::[wrapping\_mul\_add][FixedU32::wrapping_mul_add]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let half = Wrapping(I16F16::from_num(0.5));
/// let three = Wrapping(I16F16::from_num(3));
/// let four = Wrapping(I16F16::from_num(4));
/// let max = Wrapping(I16F16::MAX);
/// assert_eq!(three.mul_add(half, four), Wrapping(I16F16::from_num(5.5)));
/// assert_eq!(max.mul_add(three, max), max * 4);
/// ```
#[inline]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn mul_add(self, mul: Wrapping<F>, add: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_mul_add(mul.0, add.0))
}
/// Adds `self` to the product `a` × `b`.
///
/// See also
/// <code>FixedI32::[wrapping\_add\_prod][FixedI32::wrapping_add_prod]</code>
/// and
/// <code>FixedU32::[wrapping\_add\_prod][FixedU32::wrapping_add_prod]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let half = Wrapping(I16F16::from_num(0.5));
/// let three = Wrapping(I16F16::from_num(3));
/// let four = Wrapping(I16F16::from_num(4));
/// let max = Wrapping(I16F16::MAX);
/// assert_eq!(four.add_prod(three, half), Wrapping(I16F16::from_num(5.5)));
/// assert_eq!(max.add_prod(max, three), max * 4);
/// ```
#[inline]
#[must_use]
pub fn add_prod(self, a: Wrapping<F>, b: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_add_prod(a.0, b.0))
}
/// Multiply and accumulate. Adds (`a` × `b`) to `self`.
///
/// See also
/// <code>FixedI32::[wrapping\_mul\_acc][FixedI32::wrapping_mul_acc]</code>
/// and
/// <code>FixedU32::[wrapping\_mul\_acc][FixedU32::wrapping_mul_acc]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let mut acc = Wrapping(I16F16::from_num(3));
/// acc.mul_acc(Wrapping(I16F16::from_num(4)), Wrapping(I16F16::from_num(0.5)));
/// assert_eq!(acc, Wrapping(I16F16::from_num(5)));
///
/// acc = Wrapping(I16F16::MAX);
/// acc.mul_acc(Wrapping(I16F16::MAX), Wrapping(I16F16::from_num(3)));
/// assert_eq!(acc, Wrapping(I16F16::MAX) * 4);
/// ```
#[inline]
pub fn mul_acc(&mut self, a: Wrapping<F>, b: Wrapping<F>) {
self.0.wrapping_mul_acc(a.0, b.0);
}
/// Euclidean division.
///
/// See also
/// <code>FixedI32::[wrapping\_div\_euclid][FixedI32::wrapping_div_euclid]</code>
/// and
/// <code>FixedU32::[wrapping\_div\_euclid][FixedU32::wrapping_div_euclid]</code>.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// let den = Wrapping(I16F16::from_num(2));
/// assert_eq!(num.div_euclid(den), Wrapping(I16F16::from_num(3)));
/// let quarter = Wrapping(I16F16::from_num(0.25));
/// let check = (Wrapping(I16F16::MAX) * 4i32).round_to_zero();
/// assert_eq!(Wrapping(I16F16::MAX).div_euclid(quarter), check);
/// ```
#[inline]
#[track_caller]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn div_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_div_euclid(divisor.0))
}
/// Remainder for Euclidean division.
///
/// See also <code>FixedI32::[rem\_euclid][FixedI32::rem_euclid]</code> and
/// <code>FixedU32::[rem\_euclid][FixedU32::rem_euclid]</code>.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// let den = Wrapping(I16F16::from_num(2));
/// assert_eq!(num.rem_euclid(den), Wrapping(I16F16::from_num(1.5)));
/// assert_eq!((-num).rem_euclid(den), Wrapping(I16F16::from_num(0.5)));
/// ```
#[inline]
#[track_caller]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn rem_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.rem_euclid(divisor.0))
}
/// Euclidean division by an integer.
///
/// See also
/// <code>FixedI32::[wrapping\_div\_euclid\_int][FixedI32::wrapping_div_euclid_int]</code>
/// and
/// <code>FixedU32::[wrapping\_div\_euclid\_int][FixedU32::wrapping_div_euclid_int]</code>.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// assert_eq!(num.div_euclid_int(2), Wrapping(I16F16::from_num(3)));
/// let min = Wrapping(I16F16::MIN);
/// assert_eq!(min.div_euclid_int(-1), min);
/// ```
#[inline]
#[track_caller]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn div_euclid_int(self, divisor: F::Bits) -> Wrapping<F> {
Wrapping(self.0.wrapping_div_euclid_int(divisor))
}
/// Remainder for Euclidean division.
///
/// See also
/// <code>FixedI32::[wrapping\_rem\_euclid\_int][FixedI32::wrapping_rem_euclid_int]</code>
/// and
/// <code>FixedU32::[wrapping\_rem\_euclid\_int][FixedU32::wrapping_rem_euclid_int]</code>.
///
/// # Panics
///
/// Panics if the divisor is zero.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let num = Wrapping(I16F16::from_num(7.5));
/// assert_eq!(num.rem_euclid_int(2), Wrapping(I16F16::from_num(1.5)));
/// assert_eq!((-num).rem_euclid_int(2), Wrapping(I16F16::from_num(0.5)));
/// ```
#[inline]
#[track_caller]
#[must_use = "this returns the result of the operation, without modifying the original"]
pub fn rem_euclid_int(self, divisor: F::Bits) -> Wrapping<F> {
Wrapping(self.0.wrapping_rem_euclid_int(divisor))
}
/// Linear interpolation between `start` and `end`.
///
/// See also
/// <code>FixedI32::[wrapping\_lerp][FixedI32::wrapping_lerp]</code> and
/// <code>FixedU32::[wrapping\_lerp][FixedU32::wrapping_lerp]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// type Wr = Wrapping<I16F16>;
/// assert_eq!(Wr::from_num(0.5).lerp(Wr::ZERO, Wr::MAX), Wr::MAX / 2);
/// assert_eq!(Wr::from_num(1.5).lerp(Wr::ZERO, Wr::MAX), Wr::MAX + Wr::MAX / 2);
/// ```
#[inline]
#[must_use]
pub fn lerp(self, start: Wrapping<F>, end: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_lerp(start.0, end.0))
}
/// Inverse linear interpolation between `start` and `end`.
///
/// See also
/// <code>FixedI32::[wrapping\_inv\_lerp][FixedI32::wrapping_inv_lerp]</code> and
/// <code>FixedU32::[wrapping\_inv\_lerp][FixedU32::wrapping_inv_lerp]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// type Wr = Wrapping<I16F16>;
/// assert_eq!(
/// Wr::from_num(25).inv_lerp(Wr::from_num(20), Wr::from_num(40)),
/// Wr::from_num(0.25)
/// );
/// ```
#[inline]
#[must_use]
pub fn inv_lerp(self, start: Wrapping<F>, end: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_inv_lerp(start.0, end.0))
}
}
impl<F: FixedSigned> Wrapping<F> {
/// Returns the number of bits required to represent the value.
///
/// The number of bits required includes an initial one for
/// negative numbers, and an initial zero for non-negative
/// numbers.
///
/// See also <code>FixedI32::[signed\_bits][FixedI32::signed_bits]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I4F4, Wrapping};
/// assert_eq!(Wrapping(I4F4::from_num(-3)).signed_bits(), 7); // “_101.0000”
/// assert_eq!(Wrapping(I4F4::from_num(-1)).signed_bits(), 5); // “___1.0000”
/// assert_eq!(Wrapping(I4F4::from_num(-0.0625)).signed_bits(), 1); // “____.___1”
/// assert_eq!(Wrapping(I4F4::from_num(0)).signed_bits(), 1); // “____.___0”
/// assert_eq!(Wrapping(I4F4::from_num(0.0625)).signed_bits(), 2); // “____.__01”
/// assert_eq!(Wrapping(I4F4::from_num(1)).signed_bits(), 6); // “__01.0000”
/// assert_eq!(Wrapping(I4F4::from_num(3)).signed_bits(), 7); // “_011.0000”
/// ```
#[inline]
pub fn signed_bits(self) -> u32 {
self.0.signed_bits()
}
/// Returns [`true`] if the number is > 0.
///
/// See also <code>FixedI32::[is\_positive][FixedI32::is_positive]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(Wrapping(I16F16::from_num(4.3)).is_positive());
/// assert!(!Wrapping(I16F16::ZERO).is_positive());
/// assert!(!Wrapping(I16F16::from_num(-4.3)).is_positive());
/// ```
#[inline]
pub fn is_positive(self) -> bool {
self.0.is_positive()
}
/// Returns [`true`] if the number is < 0.
///
/// See also <code>FixedI32::[is\_negative][FixedI32::is_negative]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert!(!Wrapping(I16F16::from_num(4.3)).is_negative());
/// assert!(!Wrapping(I16F16::ZERO).is_negative());
/// assert!(Wrapping(I16F16::from_num(-4.3)).is_negative());
/// ```
#[inline]
pub fn is_negative(self) -> bool {
self.0.is_negative()
}
/// Wrapping absolute value. Returns the absolute value, wrapping
/// on overflow.
///
/// Overflow can only occur when trying to find the absolute value
/// of the minimum value.
///
/// See also <code>FixedI32::[wrapping\_abs][FixedI32::wrapping_abs]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// assert_eq!(Wrapping(I16F16::from_num(-5)).abs(), Wrapping(I16F16::from_num(5)));
/// assert_eq!(Wrapping(I16F16::MIN).abs(), Wrapping(I16F16::MIN));
/// ```
#[inline]
#[must_use]
pub fn abs(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_abs())
}
/// Returns a number representing the sign of `self`.
///
/// # Warning
///
/// Using this method when 1 and −1 cannot be represented is
/// almost certainly a bug, however, this is allowed and gives the
/// following wrapped results.
///
/// * When there are no integer bits, for example for the type
/// <code>[Wrapping]<[I0F16]></code>, the return value
/// is always zero.
/// * When there is one integer bit, for example for the type
/// <code>[Wrapping]<[I1F15]></code>, the return value
/// is zero when `self` is zero, and −1 otherwise. This means
/// that for a positive number, −1 is returned, because +1
/// does not fit and is wrapped to −1.
///
/// See also
/// <code>FixedI32::[wrapping\_signum][FixedI32::wrapping_signum]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I0F32, I1F31, I16F16},
/// Wrapping,
/// };
/// assert_eq!(Wrapping(<I16F16>::from_num(-3.9)).signum(), Wrapping(I16F16::NEG_ONE));
/// assert_eq!(Wrapping(<I16F16>::ZERO).signum(), Wrapping(I16F16::ZERO));
/// assert_eq!(Wrapping(<I16F16>::from_num(3.9)).signum(), Wrapping(I16F16::ONE));
///
/// assert_eq!(Wrapping(<I1F31>::from_num(0.5)).signum(), Wrapping(I1F31::NEG_ONE));
/// assert_eq!(Wrapping(<I0F32>::from_num(0.25)).signum(), Wrapping(I0F32::ZERO));
/// assert_eq!(Wrapping(<I0F32>::from_num(-0.5)).signum(), Wrapping(I0F32::ZERO));
/// ```
///
/// [I0F16]: crate::types::I0F16
/// [I1F15]: crate::types::I1F15
#[inline]
#[must_use]
pub fn signum(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_signum())
}
/// Addition with an unsigned fixed-point number.
///
/// See also
/// <code>FixedI32::[wrapping\_add\_unsigned][FixedI32::wrapping_add_unsigned]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert_eq!(
/// Wrapping::<I16F16>::from_num(-5).add_unsigned(U16F16::from_num(3)),
/// Wrapping::<I16F16>::from_num(-2)
/// );
/// assert_eq!(
/// Wrapping::<I16F16>::ZERO.add_unsigned(U16F16::MAX),
/// -Wrapping::<I16F16>::DELTA
/// );
/// ```
#[inline]
#[must_use]
pub fn add_unsigned(self, rhs: F::Unsigned) -> Wrapping<F> {
Wrapping(self.0.wrapping_add_unsigned(rhs))
}
/// Subtraction with an unsigned fixed-point number.
///
/// See also
/// <code>FixedI32::[wrapping\_sub\_unsigned][FixedI32::wrapping_sub_unsigned]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert_eq!(
/// Wrapping::<I16F16>::from_num(3).sub_unsigned(U16F16::from_num(5)),
/// Wrapping::<I16F16>::from_num(-2)
/// );
/// assert_eq!(
/// Wrapping::<I16F16>::ZERO.sub_unsigned(U16F16::MAX),
/// Wrapping::<I16F16>::DELTA
/// );
/// ```
#[inline]
#[must_use]
pub fn sub_unsigned(self, rhs: F::Unsigned) -> Wrapping<F> {
Wrapping(self.0.wrapping_sub_unsigned(rhs))
}
}
impl<F: FixedUnsigned> Wrapping<F> {
/// Returns the number of bits required to represent the value.
///
/// See also
/// <code>FixedU32::[significant\_bits][FixedU32::significant_bits]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U4F4, Wrapping};
/// assert_eq!(Wrapping(U4F4::from_num(0)).significant_bits(), 0); // “____.____”
/// assert_eq!(Wrapping(U4F4::from_num(0.0625)).significant_bits(), 1); // “____.___1”
/// assert_eq!(Wrapping(U4F4::from_num(1)).significant_bits(), 5); // “___1.0000”
/// assert_eq!(Wrapping(U4F4::from_num(3)).significant_bits(), 6); // “__11.0000”
/// ```
#[inline]
pub fn significant_bits(self) -> u32 {
self.0.significant_bits()
}
/// Returns [`true`] if the fixed-point number is
/// 2<sup><i>k</i></sup> for some integer <i>k</i>.
///
/// See also
/// <code>FixedU32::[is\_power\_of\_two][FixedU32::is_power_of_two]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U16F16, Wrapping};
/// assert!(Wrapping(U16F16::from_num(0.5)).is_power_of_two());
/// assert!(Wrapping(U16F16::from_num(4)).is_power_of_two());
/// assert!(!Wrapping(U16F16::from_num(5)).is_power_of_two());
/// ```
#[inline]
pub fn is_power_of_two(self) -> bool {
self.0.is_power_of_two()
}
/// Returns the highest one in the binary representation, or zero
/// if `self` is zero.
///
/// If `self` > 0, the highest one is equal to the largest power
/// of two that is ≤ `self`.
///
/// See also <code>FixedU32::[highest\_one][FixedU32::highest_one]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U16F16, Wrapping};
/// type T = Wrapping<U16F16>;
/// assert_eq!(T::from_bits(0b11_0010).highest_one(), T::from_bits(0b10_0000));
/// assert_eq!(T::from_num(0.3).highest_one(), T::from_num(0.25));
/// assert_eq!(T::from_num(4).highest_one(), T::from_num(4));
/// assert_eq!(T::from_num(6.5).highest_one(), T::from_num(4));
/// assert_eq!(T::ZERO.highest_one(), T::ZERO);
/// ```
#[inline]
#[must_use]
pub fn highest_one(self) -> Wrapping<F> {
Wrapping(self.0.highest_one())
}
/// Returns the smallest power of two that is ≥ `self`.
///
/// If the next power of two is too large to fit, it is wrapped to zero.
///
/// See also
/// <code>FixedU32::[wrapping\_next\_power\_of\_two][FixedU32::wrapping_next_power_of_two]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::U16F16, Wrapping};
/// type T = Wrapping<U16F16>;
/// assert_eq!(T::from_bits(0b11_0010).next_power_of_two(), T::from_bits(0b100_0000));
/// assert_eq!(T::from_num(0.3).next_power_of_two(), T::from_num(0.5));
/// assert_eq!(T::from_num(4).next_power_of_two(), T::from_num(4));
/// assert_eq!(T::from_num(6.5).next_power_of_two(), T::from_num(8));
/// // if the next power of two is too large, it is wrapped to zero
/// assert_eq!(T::MAX.next_power_of_two(), T::ZERO);
/// ```
#[inline]
#[must_use]
pub fn next_power_of_two(self) -> Wrapping<F> {
Wrapping(self.0.wrapping_next_power_of_two())
}
/// Addition with an signed fixed-point number.
///
/// See also
/// <code>FixedU32::[wrapping\_add\_signed][FixedU32::wrapping_add_signed]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert_eq!(
/// Wrapping::<U16F16>::from_num(5).add_signed(I16F16::from_num(-3)),
/// Wrapping::<U16F16>::from_num(2)
/// );
/// assert_eq!(
/// Wrapping::<U16F16>::ZERO.add_signed(-I16F16::DELTA),
/// Wrapping::<U16F16>::MAX
/// );
/// ```
#[inline]
#[must_use]
pub fn add_signed(self, rhs: F::Signed) -> Wrapping<F> {
Wrapping(self.0.wrapping_add_signed(rhs))
}
/// Subtraction with an signed fixed-point number.
///
/// See also
/// <code>FixedU32::[wrapping\_sub\_signed][FixedU32::wrapping_sub_signed]</code>.
///
/// # Examples
///
/// ```rust
/// use fixed::{
/// types::{I16F16, U16F16},
/// Wrapping,
/// };
/// assert_eq!(
/// Wrapping::<U16F16>::from_num(5).sub_signed(I16F16::from_num(-3)),
/// Wrapping::<U16F16>::from_num(8)
/// );
/// assert_eq!(
/// Wrapping::<U16F16>::ZERO.sub_signed(I16F16::DELTA),
/// Wrapping::<U16F16>::MAX
/// );
/// ```
#[inline]
#[must_use]
pub fn sub_signed(self, rhs: F::Signed) -> Wrapping<F> {
Wrapping(self.0.wrapping_sub_signed(rhs))
}
}
impl<F: Fixed> Display for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
Display::fmt(&self.0, f)
}
}
impl<F: Fixed> Debug for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
Debug::fmt(&self.0, f)
}
}
impl<F: Fixed> Binary for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
Binary::fmt(&self.0, f)
}
}
impl<F: Fixed> Octal for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
Octal::fmt(&self.0, f)
}
}
impl<F: Fixed> LowerHex for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
LowerHex::fmt(&self.0, f)
}
}
impl<F: Fixed> UpperHex for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
UpperHex::fmt(&self.0, f)
}
}
impl<F: Fixed> LowerExp for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
LowerExp::fmt(&self.0, f)
}
}
impl<F: Fixed> UpperExp for Wrapping<F> {
#[inline]
fn fmt(&self, f: &mut Formatter) -> FmtResult {
UpperExp::fmt(&self.0, f)
}
}
impl<F: Fixed> From<F> for Wrapping<F> {
/// Wraps a fixed-point number.
#[inline]
fn from(src: F) -> Wrapping<F> {
Wrapping(src)
}
}
impl<F: Fixed> FromStr for Wrapping<F> {
type Err = ParseFixedError;
/// Parses a string slice containing decimal digits to return a fixed-point number.
///
/// Rounding is to the nearest, with ties rounded to even.
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
F::wrapping_from_str(s).map(Wrapping)
}
}
macro_rules! op {
($wrapping:ident, $Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
impl<F: Fixed> $Op<Wrapping<F>> for Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$wrapping(other.0))
}
}
impl<F: Fixed> $Op<Wrapping<F>> for &Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$wrapping(other.0))
}
}
impl<F: Fixed> $Op<&Wrapping<F>> for Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$wrapping(other.0))
}
}
impl<F: Fixed> $Op<&Wrapping<F>> for &Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$wrapping(other.0))
}
}
impl<F: Fixed> $OpAssign<Wrapping<F>> for Wrapping<F> {
#[inline]
fn $op_assign(&mut self, other: Wrapping<F>) {
self.0 = (self.0).$wrapping(other.0);
}
}
impl<F: Fixed> $OpAssign<&Wrapping<F>> for Wrapping<F> {
#[inline]
fn $op_assign(&mut self, other: &Wrapping<F>) {
self.0 = (self.0).$wrapping(other.0);
}
}
impl<F: Fixed> $OpAssign<F> for Wrapping<F> {
#[inline]
fn $op_assign(&mut self, other: F) {
self.0 = (self.0).$wrapping(other);
}
}
impl<F: Fixed> $OpAssign<&F> for Wrapping<F> {
#[inline]
fn $op_assign(&mut self, other: &F) {
self.0 = (self.0).$wrapping(*other);
}
}
};
}
macro_rules! op_bitwise {
($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
impl<F> $Op<Wrapping<F>> for Wrapping<F>
where
F: $Op<F, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$op(other.0))
}
}
impl<F> $Op<Wrapping<F>> for &Wrapping<F>
where
for<'a> &'a F: $Op<F, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$op(other.0))
}
}
impl<F> $Op<&Wrapping<F>> for Wrapping<F>
where
for<'a> F: $Op<&'a F, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$op(&other.0))
}
}
impl<F> $Op<&Wrapping<F>> for &Wrapping<F>
where
for<'a, 'b> &'a F: $Op<&'b F, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &Wrapping<F>) -> Wrapping<F> {
Wrapping((self.0).$op(&other.0))
}
}
impl<F> $OpAssign<Wrapping<F>> for Wrapping<F>
where
F: $OpAssign<F>,
{
#[inline]
fn $op_assign(&mut self, other: Wrapping<F>) {
(self.0).$op_assign(other.0);
}
}
impl<F> $OpAssign<&Wrapping<F>> for Wrapping<F>
where
for<'a> F: $OpAssign<&'a F>,
{
#[inline]
fn $op_assign(&mut self, other: &Wrapping<F>) {
(self.0).$op_assign(&other.0);
}
}
impl<F> $OpAssign<F> for Wrapping<F>
where
F: $OpAssign<F>,
{
#[inline]
fn $op_assign(&mut self, other: F) {
(self.0).$op_assign(other);
}
}
impl<F> $OpAssign<&F> for Wrapping<F>
where
for<'a> F: $OpAssign<&'a F>,
{
#[inline]
fn $op_assign(&mut self, other: &F) {
(self.0).$op_assign(other);
}
}
};
}
macro_rules! op_shift {
(
$Op:ident $op:ident, $OpAssign:ident $op_assign:ident;
$($Rhs:ident),*
) => { $(
impl<F> $Op<$Rhs> for Wrapping<F>
where
F: $Op<u32, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: $Rhs) -> Wrapping<F> {
let nbits = mem::size_of::<F>() as u32 * 8;
Wrapping((self.0).$op(other as u32 % nbits))
}
}
impl<F> $Op<$Rhs> for &Wrapping<F>
where
for<'a> &'a F: $Op<u32, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: $Rhs) -> Wrapping<F> {
let nbits = mem::size_of::<F>() as u32 * 8;
Wrapping((self.0).$op(other as u32 % nbits))
}
}
impl<F> $Op<&$Rhs> for Wrapping<F>
where
F: $Op<u32, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &$Rhs) -> Wrapping<F> {
let nbits = mem::size_of::<F>() as u32 * 8;
Wrapping((self.0).$op(*other as u32 % nbits))
}
}
impl<F> $Op<&$Rhs> for &Wrapping<F>
where
for<'a> &'a F: $Op<u32, Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn $op(self, other: &$Rhs) -> Wrapping<F> {
let nbits = mem::size_of::<F>() as u32 * 8;
Wrapping((self.0).$op(*other as u32 % nbits))
}
}
impl<F> $OpAssign<$Rhs> for Wrapping<F>
where
F: $OpAssign<u32>,
{
#[inline]
fn $op_assign(&mut self, other: $Rhs) {
let nbits = mem::size_of::<F>() as u32 * 8;
(self.0).$op_assign(other as u32 % nbits);
}
}
impl<F> $OpAssign<&$Rhs> for Wrapping<F>
where
F: $OpAssign<u32>,
{
#[inline]
fn $op_assign(&mut self, other: &$Rhs) {
let nbits = mem::size_of::<F>() as u32 * 8;
(self.0).$op_assign(*other as u32 % nbits);
}
}
)* };
}
impl<F: Fixed> Neg for Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn neg(self) -> Wrapping<F> {
Wrapping((self.0).wrapping_neg())
}
}
impl<F: Fixed> Neg for &Wrapping<F> {
type Output = Wrapping<F>;
#[inline]
fn neg(self) -> Wrapping<F> {
Wrapping((self.0).wrapping_neg())
}
}
op! { wrapping_add, Add add, AddAssign add_assign }
op! { wrapping_sub, Sub sub, SubAssign sub_assign }
op! { wrapping_mul, Mul mul, MulAssign mul_assign }
op! { wrapping_div, Div div, DivAssign div_assign }
op! { rem, Rem rem, RemAssign rem_assign }
impl<F> Not for Wrapping<F>
where
F: Not<Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn not(self) -> Wrapping<F> {
Wrapping((self.0).not())
}
}
impl<F> Not for &Wrapping<F>
where
for<'a> &'a F: Not<Output = F>,
{
type Output = Wrapping<F>;
#[inline]
fn not(self) -> Wrapping<F> {
Wrapping((self.0).not())
}
}
op_bitwise! { BitAnd bitand, BitAndAssign bitand_assign }
op_bitwise! { BitOr bitor, BitOrAssign bitor_assign }
op_bitwise! { BitXor bitxor, BitXorAssign bitxor_assign }
op_shift! {
Shl shl, ShlAssign shl_assign;
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
}
op_shift! {
Shr shr, ShrAssign shr_assign;
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
}
impl<F: Fixed> Sum<Wrapping<F>> for Wrapping<F> {
fn sum<I>(iter: I) -> Wrapping<F>
where
I: Iterator<Item = Wrapping<F>>,
{
iter.fold(Wrapping(F::ZERO), Add::add)
}
}
impl<'a, F: 'a + Fixed> Sum<&'a Wrapping<F>> for Wrapping<F> {
fn sum<I>(iter: I) -> Wrapping<F>
where
I: Iterator<Item = &'a Wrapping<F>>,
{
iter.fold(Wrapping(F::ZERO), Add::add)
}
}
impl<F: Fixed> Product<Wrapping<F>> for Wrapping<F> {
fn product<I>(mut iter: I) -> Wrapping<F>
where
I: Iterator<Item = Wrapping<F>>,
{
match iter.next() {
None => Wrapping(1.wrapping_to_fixed()),
Some(first) => iter.fold(first, Mul::mul),
}
}
}
impl<'a, F: 'a + Fixed> Product<&'a Wrapping<F>> for Wrapping<F> {
fn product<I>(mut iter: I) -> Wrapping<F>
where
I: Iterator<Item = &'a Wrapping<F>>,
{
match iter.next() {
None => Wrapping(1.wrapping_to_fixed()),
Some(first) => iter.fold(*first, Mul::mul),
}
}
}
// The following cannot be implemented for Wrapping<F> where F: Fixed,
// otherwise there will be a conflicting implementation error. For
// example we cannot implement both these without triggering E0119:
//
// impl<F: Fixed> Op<F::Bits> for Wrapping<F> { /* ... */ }
// impl<F: Fixed> Op<&F::Bits> for Wrapping<F> { /* ... */ }
//
// To work around this, we provide implementations like this:
//
// impl<Frac> Op<i8> for Wrapping<FixedI8<Frac>> { /* ... */ }
// impl<Frac> Op<&i8> for Wrapping<FixedI8<Frac>> { /* ... */ }
// impl<Frac> Op<i16> for Wrapping<FixedI16<Frac>> { /* ... */ }
// impl<Frac> Op<&i16> for Wrapping<FixedI16<Frac>> { /* ... */ }
// ...
macro_rules! op_bits {
(
$Fixed:ident($Bits:ident $(, $LeEqU:ident)*)::$wrapping:ident,
$Op:ident $op:ident,
$OpAssign:ident $op_assign:ident
) => {
impl<Frac $(: $LeEqU)*> $Op<$Bits> for Wrapping<$Fixed<Frac>> {
type Output = Wrapping<$Fixed<Frac>>;
#[inline]
fn $op(self, other: $Bits) -> Wrapping<$Fixed<Frac>> {
Wrapping((self.0).$wrapping(other))
}
}
impl<Frac $(: $LeEqU)*> $Op<$Bits> for &Wrapping<$Fixed<Frac>> {
type Output = Wrapping<$Fixed<Frac>>;
#[inline]
fn $op(self, other: $Bits) -> Wrapping<$Fixed<Frac>> {
Wrapping((self.0).$wrapping(other))
}
}
impl<Frac $(: $LeEqU)*> $Op<&$Bits> for Wrapping<$Fixed<Frac>> {
type Output = Wrapping<$Fixed<Frac>>;
#[inline]
fn $op(self, other: &$Bits) -> Wrapping<$Fixed<Frac>> {
Wrapping((self.0).$wrapping(*other))
}
}
impl<Frac $(: $LeEqU)*> $Op<&$Bits> for &Wrapping<$Fixed<Frac>> {
type Output = Wrapping<$Fixed<Frac>>;
#[inline]
fn $op(self, other: &$Bits) -> Wrapping<$Fixed<Frac>> {
Wrapping((self.0).$wrapping(*other))
}
}
impl<Frac $(: $LeEqU)*> $OpAssign<$Bits> for Wrapping<$Fixed<Frac>> {
#[inline]
fn $op_assign(&mut self, other: $Bits) {
self.0 = (self.0).$wrapping(other);
}
}
impl<Frac $(: $LeEqU)*> $OpAssign<&$Bits> for Wrapping<$Fixed<Frac>> {
#[inline]
fn $op_assign(&mut self, other: &$Bits) {
self.0 = (self.0).$wrapping(*other);
}
}
};
}
macro_rules! ops {
($Fixed:ident($Bits:ident, $LeEqU:ident)) => {
op_bits! { $Fixed($Bits)::wrapping_mul_int, Mul mul, MulAssign mul_assign }
op_bits! { $Fixed($Bits)::wrapping_div_int, Div div, DivAssign div_assign }
op_bits! { $Fixed($Bits, $LeEqU)::rem, Rem rem, RemAssign rem_assign }
};
}
ops! { FixedI8(i8, LeEqU8) }
ops! { FixedI16(i16, LeEqU16) }
ops! { FixedI32(i32, LeEqU32) }
ops! { FixedI64(i64, LeEqU64) }
ops! { FixedI128(i128, LeEqU128) }
ops! { FixedU8(u8, LeEqU8) }
ops! { FixedU16(u16, LeEqU16) }
ops! { FixedU32(u32, LeEqU32) }
ops! { FixedU64(u64, LeEqU64) }
ops! { FixedU128(u128, LeEqU128) }