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
// Copyright © 2018–2019 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>. /*! This module contains traits. */ use crate::{ frac::{IsLessOrEqual, True, Unsigned, U128, U16, U32, U64, U8}, sealed::{self, Float, Int, SealedFixed, SealedFloat, SealedInt}, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedU8, }; use core::{ fmt::{Binary, Debug, Display, LowerHex, Octal, UpperHex}, hash::Hash, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, }, }; #[cfg(feature = "f16")] use half::f16; #[cfg(feature = "serde")] use serde::{de::Deserialize, ser::Serialize}; macro_rules! comment_features { ($comment:expr) => { #[cfg(all(not(feature = "f16"), not(feature = "serde")))] doc_comment! { $comment; pub trait FixedOptionalFeatures {} } #[cfg(all(feature = "f16", not(feature = "serde")))] doc_comment! { $comment; pub trait FixedOptionalFeatures: PartialOrd<f16> {} } #[cfg(all(not(feature = "f16"), feature = "serde"))] doc_comment! { $comment; pub trait FixedOptionalFeatures: Serialize + for<'de> Deserialize<'de> {} } #[cfg(all(feature = "f16", feature = "serde"))] doc_comment! { $comment; pub trait FixedOptionalFeatures where Self: PartialOrd<f16>, Self: Serialize + for<'de> Deserialize<'de>, { } } }; } comment_features! { "This trait is used to provide supertraits to the [`Fixed`] trait depending on the crate’s [optional features]. 1. If the `f16` feature is enabled, <code>[PartialOrd][`PartialOrd`]<[f16][`f16`]></code> is a supertrait of [`Fixed`]. 2. If the `serde` feature is enabled, [`Serialize`] and [`Deserialize`] are supertraits of [`Fixed`]. [`Deserialize`]: https://docs.rs/serde/^1/serde/de/trait.Deserialize.html [`Fixed`]: trait.Fixed.html [`PartialOrd`]: https://doc.rust-lang.org/nightly/std/cmp/trait.PartialOrd.html [`Serialize`]: https://docs.rs/serde/^1/serde/ser/trait.Serialize.html [`f16`]: https://docs.rs/half/^1/half/struct.f16.html [optional features]: ../index.html#optional-features " } /// This trait provides common methods to all fixed-point numbers. pub trait Fixed where Self: Copy + Default + Hash + Ord, Self: Debug + Display + Binary + Octal + LowerHex + UpperHex, Self: FromFixed + ToFixed + sealed::Fixed, Self: Add<Output = Self> + Sub<Output = Self> + Mul<Output = Self> + Div<Output = Self>, Self: BitAnd<Output = Self> + BitOr<Output = Self> + BitXor<Output = Self>, Self: Not<Output = Self> + Shl<u32, Output = Self> + Shr<u32, Output = Self>, Self: AddAssign + SubAssign + MulAssign + DivAssign, Self: BitAndAssign + BitOrAssign + BitXorAssign + ShlAssign<u32> + ShrAssign<u32>, Self: PartialOrd<i8> + PartialOrd<i16> + PartialOrd<i32>, Self: PartialOrd<i64> + PartialOrd<i128> + PartialOrd<isize>, Self: PartialOrd<u8> + PartialOrd<u16> + PartialOrd<u32>, Self: PartialOrd<u64> + PartialOrd<u128> + PartialOrd<usize>, Self: PartialOrd<f32> + PartialOrd<f64>, Self: FixedOptionalFeatures, { /// The primitive integer underlying type. type Bits; /// Returns the smallest value that can be represented. fn min_value() -> Self; /// Returns the largest value that can be represented. fn max_value() -> Self; /// Returns the number of integer bits. fn int_nbits() -> u32; /// Returns the number of fractional bits. fn frac_nbits() -> u32; /// Creates a fixed-point number that has a bitwise representation /// identical to the given integer. fn from_bits(bits: Self::Bits) -> Self; /// Creates an integer that has a bitwise representation identical /// to the given fixed-point number. fn to_bits(self) -> Self::Bits; /// Creates a fixed-point number from an integer. /// /// Returns the same value as [`val.to_fixed()`][`to_fixed`]. /// /// [`to_fixed`]: trait.ToFixed.html#tymethod.to_fixed fn from_int<I: Int>(val: I) -> Self; /// Converts a fixed-point number to an integer. /// /// Returns the same value as [`I::from_fixed(self)`][`from_fixed`]. /// /// [`from_fixed`]: trait.FromFixed.html#tymethod.from_fixed fn to_int<I: Int>(self) -> I; /// Creates a fixed-point number from a floating-point number. /// /// Returns the same value as [`val.to_fixed()`][`to_fixed`]. /// /// [`to_fixed`]: trait.ToFixed.html#tymethod.to_fixed fn from_float<F: Float>(val: F) -> Self; /// Converts a fixed-point number to a floating-point number. /// /// Returns the same value as [`F::from_fixed(self)`][`from_fixed`]. /// /// [`from_fixed`]: trait.FromFixed.html#tymethod.from_fixed fn to_float<F: Float>(self) -> F; /// Creates a fixed-point number from an integer if it fits, /// otherwise returns [`None`]. /// /// Returns the same value as [`val.checked_to_fixed()`][`checked_to_fixed`]. /// /// [`checked_to_fixed`]: trait.ToFixed.html#tymethod.checked_to_fixed fn checked_from_int<I: Int>(val: I) -> Option<Self>; /// Converts a fixed-point number to an integer if it fits, /// otherwise returns [`None`]. /// /// Returns the same value as [`I::checked_from_fixed(self)`][`checked_from_fixed`]. /// /// [`checked_from_fixed`]: trait.FromFixed.html#tymethod.checked_from_fixed fn checked_to_int<I: Int>(self) -> Option<I>; /// Creates a fixed-point number from a floating-point number if /// it fits, otherwise returns [`None`]. /// /// Returns the same value as [`val.checked_to_fixed()`][`checked_to_fixed`]. /// /// [`checked_to_fixed`]: trait.ToFixed.html#tymethod.checked_to_fixed fn checked_from_float<F: Float>(val: F) -> Option<Self>; /// Creates a fixed-point number from an integer, saturating the /// value if it does not fit. /// /// Returns the same value as [`val.saturating_to_fixed()`][`saturating_to_fixed`]. /// /// [`saturating_to_fixed`]: trait.ToFixed.html#tymethod.saturating_to_fixed fn saturating_from_int<I: Int>(val: I) -> Self; /// Converts a fixed-point number to an integer, saturating the /// value if it does not fit. /// /// Returns the same value as [`I::saturating_from_fixed(self)`][`saturating_from_fixed`]. /// /// [`saturating_from_fixed`]: trait.FromFixed.html#tymethod.saturating_from_fixed fn saturating_to_int<I: Int>(self) -> I; /// Creates a fixed-point number from a floating-point number, /// saturating the value if it does not fit. /// /// Returns the same value as [`val.saturating_to_fixed()`][`saturating_to_fixed`]. /// /// [`saturating_to_fixed`]: trait.ToFixed.html#tymethod.saturating_to_fixed fn saturating_from_float<F: Float>(val: F) -> Self; /// Creates a fixed-point number from an integer, wrapping the /// value on overflow. /// /// Returns the same value as [`val.wrapping_to_fixed()`][`wrapping_to_fixed`]. /// /// [`wrapping_to_fixed`]: trait.ToFixed.html#tymethod.wrapping_to_fixed fn wrapping_from_int<I: Int>(val: I) -> Self; /// Converts a fixed-point number to an integer, wrapping the /// value on overflow. /// /// Returns the same value as [`I::wrapping_from_fixed(self)`][`wrapping_from_fixed`]. /// /// [`wrapping_from_fixed`]: trait.FromFixed.html#tymethod.wrapping_from_fixed fn wrapping_to_int<I: Int>(self) -> I; /// Creates a fixed-point number from a floating-point number, /// wrapping the value on overflow. /// /// Returns the same value as [`val.wrapping_to_fixed()`][`wrapping_to_fixed`]. /// /// [`wrapping_to_fixed`]: trait.ToFixed.html#tymethod.wrapping_to_fixed fn wrapping_from_float<F: Float>(val: F) -> Self; /// Creates a fixed-point number from an integer. /// /// Returns the same value as [`val.overflowing_to_fixed()`][`overflowing_to_fixed`]. /// /// [`overflowing_to_fixed`]: trait.ToFixed.html#tymethod.overflowing_to_fixed fn overflowing_from_int<I: Int>(val: I) -> (Self, bool); /// Converts a fixed-point number to an integer. /// /// Returns the same value as [`I::overflowing_from_fixed(self)`][`overflowing_from_fixed`]. /// /// [`overflowing_from_fixed`]: trait.FromFixed.html#tymethod.overflowing_from_fixed fn overflowing_to_int<I: Int>(self) -> (I, bool); /// Creates a fixed-point number from a floating-point number. /// /// Returns the same value as [`val.overflowing_to_fixed()`][`overflowing_to_fixed`]. /// /// [`overflowing_to_fixed`]: trait.ToFixed.html#tymethod.overflowing_to_fixed fn overflowing_from_float<F: Float>(val: F) -> (Self, bool); /// Returns the integer part. fn int(self) -> Self; /// Returns the fractional part. fn frac(self) -> Self; /// Rounds to the next integer towards +∞. fn ceil(self) -> Self; /// Rounds to the next integer towards −∞. fn floor(self) -> Self; /// Rounds to the nearest integer, with ties rounded away from zero. fn round(self) -> Self; /// Checked ceil. Rounds to the next integer towards +∞, returning /// [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_ceil(self) -> Option<Self>; /// Checked floor. Rounds to the next integer towards −∞, returning /// [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_floor(self) -> Option<Self>; /// Checked round. Rounds to the nearest integer, with ties /// rounded away from zero, returning [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_round(self) -> Option<Self>; /// Saturating ceil. Rounds to the next integer towards +∞, /// saturating on overflow. fn saturating_ceil(self) -> Self; /// Saturating floor. Rounds to the next integer towards −∞, /// saturating on overflow. fn saturating_floor(self) -> Self; /// Saturating round. Rounds to the nearest integer, with ties /// rounded away from zero, and saturating on overflow. fn saturating_round(self) -> Self; /// Wrapping ceil. Rounds to the next integer towards +∞, wrapping /// on overflow. fn wrapping_ceil(self) -> Self; /// Wrapping floor. Rounds to the next integer towards −∞, /// wrapping on overflow. fn wrapping_floor(self) -> Self; /// Wrapping round. Rounds to the next integer to the nearest, /// with ties rounded away from zero, and wrapping on overflow. fn wrapping_round(self) -> Self; /// Overflowing ceil. Rounds to the next integer towards +∞. /// /// Returns a [tuple] of the fixed-point number and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_ceil(self) -> (Self, bool); /// Overflowing floor. Rounds to the next integer towards −∞. /// /// Returns a [tuple] of the fixed-point number and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_floor(self) -> (Self, bool); /// Overflowing round. Rounds to the next integer to the nearest, /// with ties rounded away from zero. /// /// Returns a [tuple] of the fixed-point number and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_round(self) -> (Self, bool); /// Returns the number of ones in the binary representation. fn count_ones(self) -> u32; /// Returns the number of zeros in the binary representation. fn count_zeros(self) -> u32; /// Returns the number of leading zeros in the binary representation. fn leading_zeros(self) -> u32; /// Returns the number of trailing zeros in the binary representation. fn trailing_zeros(self) -> u32; /// Shifts to the left by *n* bits, wrapping the truncated bits to the right end. fn rotate_left(self, n: u32) -> Self; /// Shifts to the right by *n* bits, wrapping the truncated bits to the left end. fn rotate_right(self, n: u32) -> Self; /// Multiplication by an integer. fn mul_int(self, rhs: Self::Bits) -> Self; /// Division by an integer. fn div_int(self, rhs: Self::Bits) -> Self; /// Remainder for division by an integer. fn rem_int(self, rhs: Self::Bits) -> Self; /// Checked negation. Returns the negated value, or [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_neg(self) -> Option<Self>; /// Checked addition. Returns the sum, or [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_add(self, rhs: Self) -> Option<Self>; /// Checked subtraction. Returns the difference, or [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_sub(self, rhs: Self) -> Option<Self>; /// Checked multiplication. Returns the product, or [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_mul(self, rhs: Self) -> Option<Self>; /// Checked division. Returns the quotient, or [`None`] if the /// divisor is zero or on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_div(self, rhs: Self) -> Option<Self>; /// Checked multiplication by an integer. Returns the product, or /// [`None`] on overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self>; /// Checked division by an integer. Returns the quotient, or /// [`None`] if the divisor is zero or if the division results in /// overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_div_int(self, rhs: Self::Bits) -> Option<Self>; /// Checked fixed-point remainder for division by an integer. /// Returns the remainder, or [`None`] if the divisor is zero or /// if the division results in overflow. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self>; /// Checked shift left. Returns the shifted number, or [`None`] if /// `rhs` ≥ the number of bits. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_shl(self, rhs: u32) -> Option<Self>; /// Checked shift right. Returns the shifted number, or [`None`] /// if `rhs` ≥ the number of bits. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_shr(self, rhs: u32) -> Option<Self>; /// Saturated negation. Returns the negated value, saturating on overflow. fn saturating_neg(self) -> Self; /// Saturating addition. Returns the sum, saturating on overflow. fn saturating_add(self, rhs: Self) -> Self; /// Saturating subtraction. Returns the difference, saturating on overflow. fn saturating_sub(self, rhs: Self) -> Self; /// Saturating multiplication. Returns the product, saturating on overflow. fn saturating_mul(self, rhs: Self) -> Self; /// Saturating division. Returns the quotient, saturating on overflow. /// /// # Panics /// /// Panics if the divisor is zero. fn saturating_div(self, rhs: Self) -> Self; /// Saturating multiplication by an integer. Returns the product, saturating on overflow. fn saturating_mul_int(self, rhs: Self::Bits) -> Self; /// Wrapping negation. Returns the negated value, wrapping on overflow. fn wrapping_neg(self) -> Self; /// Wrapping addition. Returns the sum, wrapping on overflow. fn wrapping_add(self, rhs: Self) -> Self; /// Wrapping subtraction. Returns the difference, wrapping on overflow. fn wrapping_sub(self, rhs: Self) -> Self; /// Wrapping multiplication. Returns the product, wrapping on overflow. fn wrapping_mul(self, rhs: Self) -> Self; /// Wrapping division. Returns the quotient, wrapping on overflow. /// /// # Panics /// /// Panics if the divisor is zero. fn wrapping_div(self, rhs: Self) -> Self; /// Wrapping multiplication by an integer. Returns the product, wrapping on overflow. fn wrapping_mul_int(self, rhs: Self::Bits) -> Self; /// Wrapping division by an integer. Returns the quotient, wrapping on overflow. /// /// Overflow can only occur when dividing the minimum value by −1. /// /// # Panics /// /// Panics if the divisor is zero. fn wrapping_div_int(self, rhs: Self::Bits) -> Self; /// Wrapping fixed-point remainder for division by an integer. /// Returns the remainder, wrapping on overflow. /// /// Overflow can only occur when dividing the minimum value by −1. /// /// # Panics /// /// Panics if the divisor is zero. fn wrapping_rem_int(self, rhs: Self::Bits) -> Self; /// Wrapping shift left. Wraps `rhs` if `rhs` ≥ the number of /// bits, then shifts and returns the number. fn wrapping_shl(self, rhs: u32) -> Self; /// Wrapping shift right. Wraps `rhs` if `rhs` ≥ the number of /// bits, then shifts and returns the number. fn wrapping_shr(self, rhs: u32) -> Self; /// Overflowing negation. /// /// Returns a [tuple] of the negated value and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_neg(self) -> (Self, bool); /// Overflowing addition. /// /// Returns a [tuple] of the sum and a [`bool`], indicating whether /// an overflow has occurred. On overflow, the wrapped value is /// returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_add(self, rhs: Self) -> (Self, bool); /// Overflowing subtraction. /// /// Returns a [tuple] of the difference and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_sub(self, rhs: Self) -> (Self, bool); /// Overflowing multiplication. /// /// Returns a [tuple] of the product and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_mul(self, rhs: Self) -> (Self, bool); /// Overflowing division. /// /// Returns a [tuple] of the quotient and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. /// /// # Panics /// /// Panics if the divisor is zero. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_div(self, rhs: Self) -> (Self, bool); /// Overflowing multiplication by an integer. /// /// Returns a [tuple] of the product and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool); /// Overflowing division by an integer. /// /// Returns a [tuple] of the quotient and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. /// /// # Panics /// /// Panics if the divisor is zero. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool); /// Overflowing fixed-point remainder for division by an integer. /// /// Returns a [tuple] of the remainder and a [`bool`], indicating /// whether an overflow has occurred. On overflow, the wrapped /// value is returned. Overflow can only occur when dividing the /// minimum value by −1. /// /// # Panics /// /// Panics if the divisor is zero. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_rem_int(self, rhs: Self::Bits) -> (Self, bool); /// Overflowing shift left. /// /// Returns a [tuple] of the shifted value and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_shl(self, rhs: u32) -> (Self, bool); /// Overflowing shift right. /// /// Returns a [tuple] of the shifted value and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_shr(self, rhs: u32) -> (Self, bool); } /// This trait provides common methods to all signed fixed-point numbers. pub trait FixedSigned: Fixed + Neg<Output = Self> { /// Returns the absolute value. fn abs(self) -> Self; /// Returns a number representing the sign of `self`. /// /// # Panics /// /// When debug assertions are enabled, this method panics /// * if the value is positive and the fixed-point number has /// zero or one integer bits such that it cannot hold the /// value 1. /// * if the value is negative and the fixed-point number has /// zero integer bits, such that it cannot hold the value −1. /// /// When debug assertions are not enabled, the wrapped value can /// be returned in those cases, but it is not considered a /// breaking change if in the future it panics; using this method /// when 1 and −1 cannot be represented is almost certainly a bug. fn signum(self) -> Self; /// Checked absolute value. Returns the absolute value, or [`None`] on overflow. /// /// Overflow can only occur when trying to find the absolute value of the minimum value. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_abs(self) -> Option<Self>; /// Saturating absolute value. Returns the absolute value, saturating on overflow. /// /// Overflow can only occur when trying to find the absolute value of the minimum value. fn saturating_abs(self) -> Self; /// 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. fn wrapping_abs(self) -> Self; /// Overflowing absolute value. /// /// Returns a [tuple] of the fixed-point number and a [`bool`], /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_abs(self) -> (Self, bool); /// Returns [`true`][`bool`] if the number is > 0. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn is_positive(self) -> bool; /// Returns [`true`][`bool`] if the number is < 0. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn is_negative(self) -> bool; } /// This trait provides common methods to all unsigned fixed-point numbers. pub trait FixedUnsigned: Fixed { /// Returns [`true`][`bool`] if the fixed-point number is /// 2<sup><i>k</i></sup> for some integer <i>k</i>. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn is_power_of_two(self) -> bool; /// Returns the smallest power of two ≥ `self`. fn next_power_of_two(self) -> Self; /// Returns the smallest power of two ≥ `self`, or [`None`] if the /// next power of two is too large to represent. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_next_power_of_two(self) -> Option<Self>; } /// This trait provides infallible conversions that might be lossy. /// /// This trait is implemented for conversions between integer /// primitives, floating-point primitives and fixed-point numbers. /// /// # Examples /// /// ```rust /// use fixed::traits::LossyFrom; /// use fixed::types::{I12F4, I4F60}; /// // original is 0x1.234 /// let original = I4F60::from_bits(0x1234i64 << (60 - 12)); /// let lossy = I12F4::lossy_from(original); /// assert_eq!(lossy, I12F4::from_bits(0x0012)); /// ``` pub trait LossyFrom<Src> { /// Performs the conversion. fn lossy_from(src: Src) -> Self; } /// This trait provides infallible conversions that might be lossy. /// This is the reciprocal of [`LossyFrom`]. /// /// Usually [`LossyFrom`] should be implemented instead of this trait; /// there is a blanket implementation which provides this trait when /// [`LossyFrom`] is implemented (similar to [`Into`] and [`From`]). /// /// # Examples /// /// ```rust /// use fixed::traits::LossyInto; /// use fixed::types::{I12F4, I4F12}; /// // original is 0x1.234 /// let original = I4F12::from_bits(0x1234); /// let lossy: I12F4 = original.lossy_into(); /// assert_eq!(lossy, I12F4::from_bits(0x0012)); /// ``` /// /// [`From`]: https://doc.rust-lang.org/nightly/std/convert/trait.From.html /// [`Into`]: https://doc.rust-lang.org/nightly/std/convert/trait.Into.html /// [`LossyFrom`]: trait.LossyFrom.html pub trait LossyInto<Dst> { /// Performs the conversion. fn lossy_into(self) -> Dst; } impl<Src, Dst> LossyInto<Dst> for Src where Dst: LossyFrom<Src>, { fn lossy_into(self) -> Dst { Dst::lossy_from(self) } } /// This trait provides checked conversions from fixed-point numbers. /// /// This trait is implemented for conversions between integer /// primitives, floating-point primitives and fixed-point numbers. /// /// # Examples /// /// ```rust /// use fixed::traits::FromFixed; /// use fixed::types::U8F8; /// // 0x87.65 /// let f = U8F8::from_bits(0x8765); /// assert_eq!(f32::from_fixed(f), f32::from(0x8765u16) / 256.0); /// assert_eq!(i32::checked_from_fixed(f), Some(0x87)); /// assert_eq!(u8::saturating_from_fixed(f), 0x87); /// // no fit /// assert_eq!(i8::checked_from_fixed(f), None); /// assert_eq!(i8::saturating_from_fixed(f), i8::max_value()); /// assert_eq!(i8::wrapping_from_fixed(f), 0x87u8 as i8); /// assert_eq!(i8::overflowing_from_fixed(f), (0x87u8 as i8, true)); /// ``` pub trait FromFixed { /// Converts from a fixed-point number. /// /// Any extra fractional bits are truncated. /// /// # Panics /// /// When debug assertions are enabled, panics if the value does /// not fit. When debug assertions are not enabled, the wrapped /// value can be returned, but it is not considered a breaking /// change if in the future it panics; if wrapping is required use /// [`wrapping_from_fixed`] instead. /// /// [`wrapping_from_fixed`]: #method.wrapping_from_fixed fn from_fixed<F: sealed::Fixed>(val: F) -> Self; /// Converts from a fixed-point number if it fits, otherwise returns [`None`]. /// /// Any extra fractional bits are truncated. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> where Self: Sized; /// Converts from a fixed-point number, saturating if it does not fit. /// /// Any extra fractional bits are truncated. fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self; /// Converts from a fixed-point number, wrapping if it does not fit. /// /// Any extra fractional bits are truncated. fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self; /// Converts from a fixed-point number. /// /// Returns a [tuple] of the value and a [`bool`] indicating whether /// an overflow has occurred. On overflow, the wrapped value is /// returned. /// /// Any extra fractional bits are truncated. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) where Self: Sized; } /// This trait provides checked conversions to fixed-point numbers. /// /// This trait is implemented for conversions between integer /// primitives, floating-point primitives and fixed-point numbers. /// /// # Examples /// /// ```rust /// use fixed::traits::ToFixed; /// use fixed::types::{U8F8, U16F16}; /// let f: U8F8 = 13.5f32.to_fixed(); /// assert_eq!(f, U8F8::from_bits((13 << 8) | (1 << 7))); /// // 0x1234.5678 is too large and can be wrapped to 0x34.56 /// let too_large = U16F16::from_bits(0x1234_5678); /// let checked: Option<U8F8> = too_large.checked_to_fixed(); /// assert_eq!(checked, None); /// let saturating: U8F8 = too_large.saturating_to_fixed(); /// assert_eq!(saturating, U8F8::max_value()); /// let wrapping: U8F8 = too_large.wrapping_to_fixed(); /// assert_eq!(wrapping, U8F8::from_bits(0x3456)); /// let overflowing: (U8F8, bool) = too_large.overflowing_to_fixed(); /// assert_eq!(overflowing, (U8F8::from_bits(0x3456), true)); /// ``` pub trait ToFixed { /// Converts to a fixed-point number. /// /// Any extra fractional bits are truncated. /// /// # Panics /// /// Panics if `self` is a floating-point number that is not [finite]. /// /// When debug assertions are enabled, also panics if the value /// does not fit. When debug assertions are not enabled, the /// wrapped value can be returned, but it is not considered a /// breaking change if in the future it panics; if wrapping is /// required use [`wrapping_to_fixed`] instead. /// /// [`wrapping_to_fixed`]: #method.wrapping_to_fixed /// [finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite fn to_fixed<F: sealed::Fixed>(self) -> F; /// Converts to a fixed-point number if it fits, otherwise returns [`None`]. /// /// Any extra fractional bits are truncated. /// /// [`None`]: https://doc.rust-lang.org/nightly/std/option/enum.Option.html#variant.None fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F>; /// Converts to a fixed-point number, saturating if it does not fit. /// /// Any extra fractional bits are truncated. /// /// # Panics /// /// Panics if `self` is a floating-point number that is [NaN]. /// /// [NaN]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_nan fn saturating_to_fixed<F: sealed::Fixed>(self) -> F; /// Converts to a fixed-point number, wrapping if it does not fit. /// /// Any extra fractional bits are truncated. /// /// # Panics /// /// Panics if `self` is a floating-point number that is not [finite]. /// /// [finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F; /// Converts from a fixed-point number. /// /// Returns a [tuple] of the fixed-point number and a [`bool`] /// indicating whether an overflow has occurred. On overflow, the /// wrapped value is returned. /// /// Any extra fractional bits are truncated. /// /// # Panics /// /// Panics if `self` is a floating-point number that is not [finite]. /// /// [`bool`]: https://doc.rust-lang.org/nightly/std/primitive.bool.html /// [finite]: https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_finite /// [tuple]: https://doc.rust-lang.org/nightly/std/primitive.tuple.html fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool); } impl ToFixed for bool { #[inline] fn to_fixed<F: sealed::Fixed>(self) -> F { ToFixed::to_fixed(u8::from(self)) } #[inline] fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> { ToFixed::checked_to_fixed(u8::from(self)) } #[inline] fn saturating_to_fixed<F: sealed::Fixed>(self) -> F { ToFixed::saturating_to_fixed(u8::from(self)) } #[inline] fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F { ToFixed::wrapping_to_fixed(u8::from(self)) } #[inline] fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) { ToFixed::overflowing_to_fixed(u8::from(self)) } } macro_rules! impl_int { ($Int:ident) => { impl FromFixed for $Int { #[inline] fn from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedInt::from_fixed(val) } #[inline] fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> { SealedInt::checked_from_fixed(val) } #[inline] fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedInt::saturating_from_fixed(val) } #[inline] fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedInt::wrapping_from_fixed(val) } #[inline] fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) { SealedInt::overflowing_from_fixed(val) } } impl ToFixed for $Int { #[inline] fn to_fixed<F: sealed::Fixed>(self) -> F { SealedInt::to_fixed(self) } #[inline] fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> { SealedInt::checked_to_fixed(self) } #[inline] fn saturating_to_fixed<F: sealed::Fixed>(self) -> F { SealedInt::saturating_to_fixed(self) } #[inline] fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F { SealedInt::wrapping_to_fixed(self) } #[inline] fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) { SealedInt::overflowing_to_fixed(self) } } }; } impl_int! { i8 } impl_int! { i16 } impl_int! { i32 } impl_int! { i64 } impl_int! { i128 } impl_int! { isize } impl_int! { u8 } impl_int! { u16 } impl_int! { u32 } impl_int! { u64 } impl_int! { u128 } impl_int! { usize } macro_rules! impl_float { ($Float:ty) => { impl FromFixed for $Float { #[inline] fn from_fixed<F: sealed::Fixed>(val: F) -> Self { val.to_float() } #[inline] fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> { Some(val.to_float()) } #[inline] fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self { val.to_float() } #[inline] fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self { val.to_float() } #[inline] fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) { (val.to_float(), false) } } impl ToFixed for $Float { #[inline] fn to_fixed<F: sealed::Fixed>(self) -> F { SealedFloat::to_fixed(self) } #[inline] fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> { SealedFloat::checked_to_fixed(self) } #[inline] fn saturating_to_fixed<F: sealed::Fixed>(self) -> F { SealedFloat::saturating_to_fixed(self) } #[inline] fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F { SealedFloat::wrapping_to_fixed(self) } #[inline] fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) { SealedFloat::overflowing_to_fixed(self) } } }; } #[cfg(feature = "f16")] impl_float! { f16 } impl_float! { f32 } impl_float! { f64 } macro_rules! trait_delegate { (fn $method:ident() -> $Ret:ty) => { #[inline] fn $method() -> $Ret { Self::$method() } }; (fn $method:ident(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => { #[inline] fn $method(self $(, $param: $Param)*) -> $Ret { self.$method($($param),*) } }; (fn $method:ident<$Gen:ident: $Trait:ident>($param:ident: $Param:ty) -> $Ret:ty) => { #[inline] fn $method<$Gen: $Trait>($param: $Param) -> $Ret { Self::$method($param) } }; (fn $method:ident<$Gen:ident: $Trait:ident>(self $(, $param:ident: $Param:ty)*) -> $Ret:ty) => { #[inline] fn $method<$Gen: $Trait>(self $(, $param: $Param)*) -> $Ret { self.$method($($param),*) } }; } macro_rules! impl_fixed { ($Fixed:ident, $NBits:ident, $Bits:ident) => { impl<Frac> FixedOptionalFeatures for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { // this comment to work around rustfmt bug } impl<Frac> Fixed for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { type Bits = $Bits; trait_delegate! { fn min_value() -> Self } trait_delegate! { fn max_value() -> Self } trait_delegate! { fn int_nbits() -> u32 } trait_delegate! { fn frac_nbits() -> u32 } #[inline] fn from_bits(bits: Self::Bits) -> Self { Self::from_bits(bits) } trait_delegate! { fn to_bits(self) -> Self::Bits } trait_delegate! { fn from_int<I: Int>(val: I) -> Self } trait_delegate! { fn to_int<I: Int>(self) -> I } trait_delegate! { fn from_float<F: Float>(val: F) -> Self } trait_delegate! { fn to_float<F: Float>(self) -> F } trait_delegate! { fn checked_from_int<I: Int>(val: I) -> Option<Self> } trait_delegate! { fn checked_to_int<I: Int>(self) -> Option<I> } trait_delegate! { fn checked_from_float<F: Float>(val: F) -> Option<Self> } trait_delegate! { fn saturating_from_int<I: Int>(val: I) -> Self } trait_delegate! { fn saturating_to_int<I: Int>(self) -> I } trait_delegate! { fn saturating_from_float<F: Float>(val: F) -> Self } trait_delegate! { fn wrapping_from_int<I: Int>(val: I) -> Self } trait_delegate! { fn wrapping_to_int<I: Int>(self) -> I } trait_delegate! { fn wrapping_from_float<F: Float>(val: F) -> Self } trait_delegate! { fn overflowing_from_int<I: Int>(val: I) -> (Self, bool) } trait_delegate! { fn overflowing_to_int<I: Int>(self) -> (I, bool) } trait_delegate! { fn overflowing_from_float<F: Float>(val: F) -> (Self, bool) } trait_delegate! { fn int(self) -> Self } trait_delegate! { fn frac(self) -> Self } trait_delegate! { fn ceil(self) -> Self } trait_delegate! { fn floor(self) -> Self } trait_delegate! { fn round(self) -> Self } trait_delegate! { fn checked_ceil(self) -> Option<Self> } trait_delegate! { fn checked_floor(self) -> Option<Self> } trait_delegate! { fn checked_round(self) -> Option<Self> } trait_delegate! { fn saturating_ceil(self) -> Self } trait_delegate! { fn saturating_floor(self) -> Self } trait_delegate! { fn saturating_round(self) -> Self } trait_delegate! { fn wrapping_ceil(self) -> Self } trait_delegate! { fn wrapping_floor(self) -> Self } trait_delegate! { fn wrapping_round(self) -> Self } trait_delegate! { fn overflowing_ceil(self) -> (Self, bool) } trait_delegate! { fn overflowing_floor(self) -> (Self, bool) } trait_delegate! { fn overflowing_round(self) -> (Self, bool) } trait_delegate! { fn count_ones(self) -> u32 } trait_delegate! { fn count_zeros(self) -> u32 } trait_delegate! { fn leading_zeros(self) -> u32 } trait_delegate! { fn trailing_zeros(self) -> u32 } trait_delegate! { fn rotate_left(self, n: u32) -> Self } trait_delegate! { fn rotate_right(self, n: u32) -> Self } #[inline] fn mul_int(self, rhs: Self::Bits) -> Self { self * rhs } #[inline] fn div_int(self, rhs: Self::Bits) -> Self { self / rhs } #[inline] fn rem_int(self, rhs: Self::Bits) -> Self { self % rhs } trait_delegate! { fn checked_neg(self) -> Option<Self> } trait_delegate! { fn checked_add(self, rhs: Self) -> Option<Self> } trait_delegate! { fn checked_sub(self, rhs: Self) -> Option<Self> } trait_delegate! { fn checked_mul(self, rhs: Self) -> Option<Self> } trait_delegate! { fn checked_div(self, rhs: Self) -> Option<Self> } trait_delegate! { fn checked_mul_int(self, rhs: Self::Bits) -> Option<Self> } trait_delegate! { fn checked_div_int(self, rhs: Self::Bits) -> Option<Self> } trait_delegate! { fn checked_rem_int(self, rhs: Self::Bits) -> Option<Self> } trait_delegate! { fn checked_shl(self, rhs: u32) -> Option<Self> } trait_delegate! { fn checked_shr(self, rhs: u32) -> Option<Self> } trait_delegate! { fn saturating_neg(self) -> Self } trait_delegate! { fn saturating_add(self, rhs: Self) -> Self } trait_delegate! { fn saturating_sub(self, rhs: Self) -> Self } trait_delegate! { fn saturating_mul(self, rhs: Self) -> Self } trait_delegate! { fn saturating_div(self, rhs: Self) -> Self } trait_delegate! { fn saturating_mul_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_neg(self) -> Self } trait_delegate! { fn wrapping_add(self, rhs: Self) -> Self } trait_delegate! { fn wrapping_sub(self, rhs: Self) -> Self } trait_delegate! { fn wrapping_mul(self, rhs: Self) -> Self } trait_delegate! { fn wrapping_div(self, rhs: Self) -> Self } trait_delegate! { fn wrapping_mul_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_div_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_rem_int(self, rhs: Self::Bits) -> Self } trait_delegate! { fn wrapping_shl(self, rhs: u32) -> Self } trait_delegate! { fn wrapping_shr(self, rhs: u32) -> Self } trait_delegate! { fn overflowing_neg(self) -> (Self, bool) } trait_delegate! { fn overflowing_add(self, rhs: Self) -> (Self, bool) } trait_delegate! { fn overflowing_sub(self, rhs: Self) -> (Self, bool) } trait_delegate! { fn overflowing_mul(self, rhs: Self) -> (Self, bool) } trait_delegate! { fn overflowing_div(self, rhs: Self) -> (Self, bool) } trait_delegate! { fn overflowing_mul_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_div_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_rem_int(self, rhs: Self::Bits) -> (Self, bool) } trait_delegate! { fn overflowing_shl(self, rhs: u32) -> (Self, bool) } trait_delegate! { fn overflowing_shr(self, rhs: u32) -> (Self, bool) } } impl<Frac> FromFixed for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { #[inline] fn from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedFixed::from_fixed(val) } #[inline] fn checked_from_fixed<F: sealed::Fixed>(val: F) -> Option<Self> { SealedFixed::checked_from_fixed(val) } #[inline] fn saturating_from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedFixed::saturating_from_fixed(val) } #[inline] fn wrapping_from_fixed<F: sealed::Fixed>(val: F) -> Self { SealedFixed::wrapping_from_fixed(val) } #[inline] fn overflowing_from_fixed<F: sealed::Fixed>(val: F) -> (Self, bool) { SealedFixed::overflowing_from_fixed(val) } } impl<Frac> ToFixed for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { #[inline] fn to_fixed<F: sealed::Fixed>(self) -> F { SealedFixed::from_fixed(self) } #[inline] fn checked_to_fixed<F: sealed::Fixed>(self) -> Option<F> { SealedFixed::checked_from_fixed(self) } #[inline] fn saturating_to_fixed<F: sealed::Fixed>(self) -> F { SealedFixed::saturating_from_fixed(self) } #[inline] fn wrapping_to_fixed<F: sealed::Fixed>(self) -> F { SealedFixed::wrapping_from_fixed(self) } #[inline] fn overflowing_to_fixed<F: sealed::Fixed>(self) -> (F, bool) { SealedFixed::overflowing_from_fixed(self) } } }; } macro_rules! impl_fixed_signed { ($Fixed:ident, $NBits:ident, $Bits:ident) => { impl_fixed! { $Fixed, $NBits, $Bits } impl<Frac> FixedSigned for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { trait_delegate! { fn abs(self) -> Self } trait_delegate! { fn signum(self) -> Self } trait_delegate! { fn checked_abs(self) -> Option<Self> } trait_delegate! { fn saturating_abs(self) -> Self } trait_delegate! { fn wrapping_abs(self) -> Self } trait_delegate! { fn overflowing_abs(self) -> (Self, bool) } trait_delegate! { fn is_positive(self) -> bool } trait_delegate! { fn is_negative(self) -> bool } } }; } macro_rules! impl_fixed_unsigned { ($Fixed:ident, $NBits:ident, $Bits:ident) => { impl_fixed! { $Fixed, $NBits, $Bits } impl<Frac> FixedUnsigned for $Fixed<Frac> where Frac: Unsigned + IsLessOrEqual<$NBits, Output = True>, { trait_delegate! { fn is_power_of_two(self) -> bool } trait_delegate! { fn next_power_of_two(self) -> Self } trait_delegate! { fn checked_next_power_of_two(self) -> Option<Self> } } }; } impl_fixed_signed! { FixedI8, U8, i8 } impl_fixed_signed! { FixedI16, U16, i16 } impl_fixed_signed! { FixedI32, U32, i32 } impl_fixed_signed! { FixedI64, U64, i64 } impl_fixed_signed! { FixedI128, U128, i128 } impl_fixed_unsigned! { FixedU8, U8, u8 } impl_fixed_unsigned! { FixedU16, U16, u16 } impl_fixed_unsigned! { FixedU32, U32, u32 } impl_fixed_unsigned! { FixedU64, U64, u64 } impl_fixed_unsigned! { FixedU128, U128, u128 }