fixed/
unwrapped.rs

1// Copyright © 2018–2025 Trevor Spiteri
2
3// This library is free software: you can redistribute it and/or
4// modify it under the terms of either
5//
6//   * the Apache License, Version 2.0 or
7//   * the MIT License
8//
9// at your option.
10//
11// You should have recieved copies of the Apache License and the MIT
12// License along with the library. If not, see
13// <https://www.apache.org/licenses/LICENSE-2.0> and
14// <https://opensource.org/licenses/MIT>.
15
16use crate::from_str::ParseFixedError;
17use crate::traits::{Fixed, FixedSigned, FixedUnsigned, FromFixed, ToFixed};
18use crate::types::extra::{LeEqU8, LeEqU16, LeEqU32, LeEqU64, LeEqU128};
19use crate::{
20    FixedI8, FixedI16, FixedI32, FixedI64, FixedI128, FixedU8, FixedU16, FixedU32, FixedU64,
21    FixedU128,
22};
23use core::fmt::{
24    Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, Result as FmtResult, UpperExp,
25    UpperHex,
26};
27use core::iter::{Product, Sum};
28use core::ops::{
29    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
30    Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
31};
32use core::str::FromStr;
33
34/// Provides arithmetic operations that panic on overflow even when
35/// debug assertions are disabled.
36///
37/// The underlying value can be retrieved through the `.0` index.
38///
39/// # Examples
40///
41/// This panics even when debug assertions are disabled.
42///
43/// ```rust,should_panic
44/// use fixed::types::I16F16;
45/// use fixed::Unwrapped;
46/// let max = Unwrapped(I16F16::MAX);
47/// let delta = Unwrapped(I16F16::DELTA);
48/// let _overflow = max + delta;
49/// ```
50#[repr(transparent)]
51#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
52pub struct Unwrapped<F>(pub F);
53
54impl<F: Fixed> Unwrapped<F> {
55    /// Zero.
56    ///
57    /// See also <code>FixedI32::[ZERO][FixedI32::ZERO]</code> and
58    /// <code>FixedU32::[ZERO][FixedU32::ZERO]</code>.
59    ///
60    /// # Examples
61    ///
62    /// ```rust
63    /// use fixed::types::I16F16;
64    /// use fixed::Unwrapped;
65    /// assert_eq!(Unwrapped::<I16F16>::ZERO, Unwrapped(I16F16::ZERO));
66    /// ```
67    pub const ZERO: Unwrapped<F> = Unwrapped(F::ZERO);
68
69    /// The difference between any two successive representable numbers, <i>Δ</i>.
70    ///
71    /// See also <code>FixedI32::[DELTA][FixedI32::DELTA]</code> and
72    /// <code>FixedU32::[DELTA][FixedU32::DELTA]</code>.
73    ///
74    /// # Examples
75    ///
76    /// ```rust
77    /// use fixed::types::I16F16;
78    /// use fixed::Unwrapped;
79    /// assert_eq!(Unwrapped::<I16F16>::DELTA, Unwrapped(I16F16::DELTA));
80    /// ```
81    pub const DELTA: Unwrapped<F> = Unwrapped(F::DELTA);
82
83    /// The smallest value that can be represented.
84    ///
85    /// See also <code>FixedI32::[MIN][FixedI32::MIN]</code> and
86    /// <code>FixedU32::[MIN][FixedU32::MIN]</code>.
87    ///
88    /// # Examples
89    ///
90    /// ```rust
91    /// use fixed::types::I16F16;
92    /// use fixed::Unwrapped;
93    /// assert_eq!(Unwrapped::<I16F16>::MIN, Unwrapped(I16F16::MIN));
94    /// ```
95    pub const MIN: Unwrapped<F> = Unwrapped(F::MIN);
96
97    /// The largest value that can be represented.
98    ///
99    /// See also <code>FixedI32::[MAX][FixedI32::MAX]</code> and
100    /// <code>FixedU32::[MAX][FixedU32::MAX]</code>.
101    ///
102    /// # Examples
103    ///
104    /// ```rust
105    /// use fixed::types::I16F16;
106    /// use fixed::Unwrapped;
107    /// assert_eq!(Unwrapped::<I16F16>::MAX, Unwrapped(I16F16::MAX));
108    /// ```
109    pub const MAX: Unwrapped<F> = Unwrapped(F::MAX);
110
111    /// [`true`] if the type is signed.
112    ///
113    /// See also <code>FixedI32::[IS\_SIGNED][FixedI32::IS_SIGNED]</code> and
114    /// <code>FixedU32::[IS\_SIGNED][FixedU32::IS_SIGNED]</code>.
115    ///
116    /// # Examples
117    ///
118    /// ```rust
119    /// use fixed::types::{I16F16, U16F16};
120    /// use fixed::Unwrapped;
121    /// assert!(Unwrapped::<I16F16>::IS_SIGNED);
122    /// assert!(!Unwrapped::<U16F16>::IS_SIGNED);
123    /// ```
124    pub const IS_SIGNED: bool = F::IS_SIGNED;
125
126    /// The number of integer bits.
127    ///
128    /// See also <code>FixedI32::[INT\_NBITS][FixedI32::INT_NBITS]</code> and
129    /// <code>FixedU32::[INT\_NBITS][FixedU32::INT_NBITS]</code>.
130    ///
131    /// # Examples
132    ///
133    /// ```rust
134    /// use fixed::types::I16F16;
135    /// use fixed::Unwrapped;
136    /// assert_eq!(Unwrapped::<I16F16>::INT_NBITS, I16F16::INT_NBITS);
137    /// ```
138    pub const INT_NBITS: u32 = F::INT_NBITS;
139
140    /// The number of fractional bits.
141    ///
142    /// See also <code>FixedI32::[FRAC\_NBITS][FixedI32::FRAC_NBITS]</code> and
143    /// <code>FixedU32::[FRAC\_NBITS][FixedU32::FRAC_NBITS]</code>.
144    ///
145    /// # Examples
146    ///
147    /// ```rust
148    /// use fixed::types::I16F16;
149    /// use fixed::Unwrapped;
150    /// assert_eq!(Unwrapped::<I16F16>::FRAC_NBITS, I16F16::FRAC_NBITS);
151    /// ```
152    pub const FRAC_NBITS: u32 = F::FRAC_NBITS;
153
154    /// Creates a fixed-point number that has a bitwise representation
155    /// identical to the given integer.
156    ///
157    /// See also <code>FixedI32::[from\_bits][FixedI32::from_bits]</code> and
158    /// <code>FixedU32::[from\_bits][FixedU32::from_bits]</code>.
159    ///
160    /// # Examples
161    ///
162    /// ```rust
163    /// use fixed::types::I16F16;
164    /// use fixed::Unwrapped;
165    /// assert_eq!(Unwrapped::<I16F16>::from_bits(0x1C), Unwrapped(I16F16::from_bits(0x1C)));
166    /// ```
167    #[inline]
168    pub fn from_bits(bits: F::Bits) -> Unwrapped<F> {
169        Unwrapped(F::from_bits(bits))
170    }
171
172    /// Creates an integer that has a bitwise representation identical
173    /// to the given fixed-point number.
174    ///
175    /// See also <code>FixedI32::[to\_bits][FixedI32::to_bits]</code> and
176    /// <code>FixedU32::[to\_bits][FixedU32::to_bits]</code>.
177    ///
178    /// # Examples
179    ///
180    /// ```rust
181    /// use fixed::types::I16F16;
182    /// use fixed::Unwrapped;
183    /// let w = Unwrapped(I16F16::from_bits(0x1C));
184    /// assert_eq!(w.to_bits(), 0x1C);
185    /// ```
186    #[inline]
187    pub fn to_bits(self) -> F::Bits {
188        self.0.to_bits()
189    }
190
191    /// Converts a fixed-point number from big endian to the target’s
192    /// endianness.
193    ///
194    /// See also <code>FixedI32::[from\_be][FixedI32::from_be]</code> and
195    /// <code>FixedU32::[from\_be][FixedU32::from_be]</code>.
196    ///
197    /// # Examples
198    ///
199    /// ```rust
200    /// use fixed::types::I16F16;
201    /// use fixed::Unwrapped;
202    /// let w = Unwrapped(I16F16::from_bits(0x1234_5678));
203    /// if cfg!(target_endian = "big") {
204    ///     assert_eq!(Unwrapped::from_be(w), w);
205    /// } else {
206    ///     assert_eq!(Unwrapped::from_be(w), w.swap_bytes());
207    /// }
208    /// ```
209    #[inline]
210    pub fn from_be(w: Self) -> Self {
211        Unwrapped(F::from_be(w.0))
212    }
213
214    /// Converts a fixed-point number from little endian to the
215    /// target’s endianness.
216    ///
217    /// See also <code>FixedI32::[from\_le][FixedI32::from_le]</code> and
218    /// <code>FixedU32::[from\_le][FixedU32::from_le]</code>.
219    ///
220    /// # Examples
221    ///
222    /// ```rust
223    /// use fixed::types::I16F16;
224    /// use fixed::Unwrapped;
225    /// let w = Unwrapped(I16F16::from_bits(0x1234_5678));
226    /// if cfg!(target_endian = "little") {
227    ///     assert_eq!(Unwrapped::from_le(w), w);
228    /// } else {
229    ///     assert_eq!(Unwrapped::from_le(w), w.swap_bytes());
230    /// }
231    /// ```
232    #[inline]
233    pub fn from_le(w: Self) -> Self {
234        Unwrapped(F::from_le(w.0))
235    }
236
237    /// Converts `self` to big endian from the target’s endianness.
238    ///
239    /// See also <code>FixedI32::[to\_be][FixedI32::to_be]</code> and
240    /// <code>FixedU32::[to\_be][FixedU32::to_be]</code>.
241    ///
242    /// # Examples
243    ///
244    /// ```rust
245    /// use fixed::types::I16F16;
246    /// use fixed::Unwrapped;
247    /// let w = Unwrapped(I16F16::from_bits(0x1234_5678));
248    /// if cfg!(target_endian = "big") {
249    ///     assert_eq!(w.to_be(), w);
250    /// } else {
251    ///     assert_eq!(w.to_be(), w.swap_bytes());
252    /// }
253    /// ```
254    #[inline]
255    #[must_use]
256    pub fn to_be(self) -> Self {
257        Unwrapped(self.0.to_be())
258    }
259
260    /// Converts `self` to little endian from the target’s endianness.
261    ///
262    /// See also <code>FixedI32::[to\_le][FixedI32::to_le]</code> and
263    /// <code>FixedU32::[to\_le][FixedU32::to_le]</code>.
264    ///
265    /// # Examples
266    ///
267    /// ```rust
268    /// use fixed::types::I16F16;
269    /// use fixed::Unwrapped;
270    /// let w = Unwrapped(I16F16::from_bits(0x1234_5678));
271    /// if cfg!(target_endian = "little") {
272    ///     assert_eq!(w.to_le(), w);
273    /// } else {
274    ///     assert_eq!(w.to_le(), w.swap_bytes());
275    /// }
276    /// ```
277    #[inline]
278    #[must_use]
279    pub fn to_le(self) -> Self {
280        Unwrapped(self.0.to_le())
281    }
282
283    /// Reverses the byte order of the fixed-point number.
284    ///
285    /// See also <code>FixedI32::[swap\_bytes][FixedI32::swap_bytes]</code> and
286    /// <code>FixedU32::[swap\_bytes][FixedU32::swap_bytes]</code>.
287    ///
288    /// # Examples
289    ///
290    /// ```rust
291    /// use fixed::types::I16F16;
292    /// use fixed::Unwrapped;
293    /// let w = Unwrapped(I16F16::from_bits(0x1234_5678));
294    /// let swapped = Unwrapped(I16F16::from_bits(0x7856_3412));
295    /// assert_eq!(w.swap_bytes(), swapped);
296    /// ```
297    #[inline]
298    #[must_use]
299    pub fn swap_bytes(self) -> Self {
300        Unwrapped(self.0.swap_bytes())
301    }
302
303    /// Creates a fixed-point number from its representation
304    /// as a byte array in big endian.
305    ///
306    /// See also
307    /// <code>FixedI32::[from\_be\_bytes][FixedI32::from_be_bytes]</code> and
308    /// <code>FixedU32::[from\_be\_bytes][FixedU32::from_be_bytes]</code>.
309    ///
310    /// # Examples
311    ///
312    /// ```rust
313    /// use fixed::types::I16F16;
314    /// use fixed::Unwrapped;
315    /// let bytes = [0x12, 0x34, 0x56, 0x78];
316    /// assert_eq!(
317    ///     Unwrapped::<I16F16>::from_be_bytes(bytes),
318    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678)
319    /// );
320    /// ```
321    #[inline]
322    pub fn from_be_bytes(bytes: F::Bytes) -> Self {
323        Unwrapped(F::from_be_bytes(bytes))
324    }
325
326    /// Creates a fixed-point number from its representation
327    /// as a byte array in little endian.
328    ///
329    /// See also
330    /// <code>FixedI32::[from\_le\_bytes][FixedI32::from_le_bytes]</code> and
331    /// <code>FixedU32::[from\_le\_bytes][FixedU32::from_le_bytes]</code>.
332    ///
333    /// # Examples
334    ///
335    /// ```rust
336    /// use fixed::types::I16F16;
337    /// use fixed::Unwrapped;
338    /// let bytes = [0x78, 0x56, 0x34, 0x12];
339    /// assert_eq!(
340    ///     Unwrapped::<I16F16>::from_le_bytes(bytes),
341    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678)
342    /// );
343    /// ```
344    #[inline]
345    pub fn from_le_bytes(bytes: F::Bytes) -> Self {
346        Unwrapped(F::from_le_bytes(bytes))
347    }
348
349    /// Creates a fixed-point number from its representation
350    /// as a byte array in native endian.
351    ///
352    /// See also
353    /// <code>FixedI32::[from\_ne\_bytes][FixedI32::from_ne_bytes]</code> and
354    /// <code>FixedU32::[from\_ne\_bytes][FixedU32::from_ne_bytes]</code>.
355    ///
356    /// # Examples
357    ///
358    /// ```rust
359    /// use fixed::types::I16F16;
360    /// use fixed::Unwrapped;
361    /// let bytes = if cfg!(target_endian = "big") {
362    ///     [0x12, 0x34, 0x56, 0x78]
363    /// } else {
364    ///     [0x78, 0x56, 0x34, 0x12]
365    /// };
366    /// assert_eq!(
367    ///     Unwrapped::<I16F16>::from_ne_bytes(bytes),
368    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678)
369    /// );
370    /// ```
371    #[inline]
372    pub fn from_ne_bytes(bytes: F::Bytes) -> Self {
373        Unwrapped(F::from_ne_bytes(bytes))
374    }
375
376    /// Returns the memory representation of this fixed-point
377    /// number as a byte array in big-endian byte order.
378    ///
379    /// See also <code>FixedI32::[to\_be\_bytes][FixedI32::to_be_bytes]</code>
380    /// and <code>FixedU32::[to\_be\_bytes][FixedU32::to_be_bytes]</code>.
381    ///
382    /// # Examples
383    ///
384    /// ```rust
385    /// use fixed::types::I16F16;
386    /// use fixed::Unwrapped;
387    /// assert_eq!(
388    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678).to_be_bytes(),
389    ///     [0x12, 0x34, 0x56, 0x78]
390    /// );
391    /// ```
392    #[inline]
393    pub fn to_be_bytes(self) -> F::Bytes {
394        self.0.to_be_bytes()
395    }
396
397    /// Returns the memory representation of this fixed-point
398    /// number as a byte array in little-endian byte order.
399    ///
400    /// See also <code>FixedI32::[to\_le\_bytes][FixedI32::to_le_bytes]</code>
401    /// and <code>FixedU32::[to\_le\_bytes][FixedU32::to_le_bytes]</code>.
402    ///
403    /// # Examples
404    ///
405    /// ```rust
406    /// use fixed::types::I16F16;
407    /// use fixed::Unwrapped;
408    /// assert_eq!(
409    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678).to_le_bytes(),
410    ///     [0x78, 0x56, 0x34, 0x12]
411    /// );
412    /// ```
413    #[inline]
414    pub fn to_le_bytes(self) -> F::Bytes {
415        self.0.to_le_bytes()
416    }
417
418    /// Returns the memory representation of this fixed-point
419    /// number as a byte array in native-endian byte order.
420    ///
421    /// See also <code>FixedI32::[to\_ne\_bytes][FixedI32::to_ne_bytes]</code>
422    /// and <code>FixedU32::[to\_ne\_bytes][FixedU32::to_ne_bytes]</code>.
423    ///
424    /// # Examples
425    ///
426    /// ```rust
427    /// use fixed::types::I16F16;
428    /// use fixed::Unwrapped;
429    /// let bytes = if cfg!(target_endian = "big") {
430    ///     [0x12, 0x34, 0x56, 0x78]
431    /// } else {
432    ///     [0x78, 0x56, 0x34, 0x12]
433    /// };
434    /// assert_eq!(
435    ///     Unwrapped::<I16F16>::from_bits(0x1234_5678).to_ne_bytes(),
436    ///     bytes
437    /// );
438    /// ```
439    #[inline]
440    pub fn to_ne_bytes(self) -> F::Bytes {
441        self.0.to_ne_bytes()
442    }
443
444    /// Unwrapped conversion from another number.
445    ///
446    /// The other number can be:
447    ///
448    ///   * A fixed-point number. Any extra fractional bits are
449    ///     discarded, which rounds towards &minus;∞.
450    ///   * An integer of type [`i8`], [`i16`], [`i32`], [`i64`], [`i128`],
451    ///     [`isize`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], or
452    ///     [`usize`].
453    ///   * A floating-point number of type
454    ///     <code>[half]::[f16][half::f16]</code>,
455    ///     <code>[half]::[bf16][half::bf16]</code>, [`f32`], [`f64`] or
456    ///     [`F128`]. For this conversion, the method rounds to the nearest,
457    ///     with ties rounding to even.
458    ///   * Any other number `src` for which [`ToFixed`] is
459    ///     implemented, in which case this method returns
460    ///     <code>[Unwrapped]\(src.[unwrapped\_to\_fixed][ToFixed::unwrapped_to_fixed]\())</code>.
461    ///
462    /// See also
463    /// <code>FixedI32::[unwrapped\_from\_num][FixedI32::unwrapped_from_num]</code>
464    /// and
465    /// <code>FixedU32::[unwrapped\_from\_num][FixedU32::unwrapped_from_num]</code>.
466    ///
467    /// # Panics
468    ///
469    /// Panics if the value does not fit.
470    ///
471    /// For floating-point numbers, also panics if the value is not [finite].
472    ///
473    /// # Examples
474    ///
475    /// ```rust
476    /// use fixed::types::{I4F4, I16F16};
477    /// use fixed::Unwrapped;
478    /// let src = I16F16::from_num(1.75);
479    /// let dst = Unwrapped::<I4F4>::from_num(src);
480    /// assert_eq!(dst, Unwrapped(I4F4::from_num(1.75)));
481    /// ```
482    ///
483    /// The following panics even when debug assertions are disabled.
484    ///
485    /// ```should_panic
486    /// use fixed::types::{I4F4, I16F16};
487    /// use fixed::Unwrapped;
488    /// let src = I16F16::from_bits(0x1234_5678);
489    /// let _overflow = Unwrapped::<I4F4>::from_num(src);
490    /// ```
491    ///
492    /// [`F128`]: crate::F128
493    /// [finite]: f64::is_finite
494    #[inline]
495    #[track_caller]
496    pub fn from_num<Src: ToFixed>(src: Src) -> Unwrapped<F> {
497        Unwrapped(src.unwrapped_to_fixed())
498    }
499
500    /// Converts a fixed-point number to another number, panicking on
501    /// overflow.
502    ///
503    /// The other number can be:
504    ///
505    ///   * Another fixed-point number. Any extra fractional bits are
506    ///     discarded, which rounds towards &minus;∞.
507    ///   * An integer of type [`i8`], [`i16`], [`i32`], [`i64`], [`i128`],
508    ///     [`isize`], [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], or
509    ///     [`usize`]. Any fractional bits are discarded, which rounds
510    ///     towards &minus;∞.
511    ///   * A floating-point number of type
512    ///     <code>[half]::[f16][half::f16]</code>,
513    ///     <code>[half]::[bf16][half::bf16]</code>, [`f32`], [`f64`] or
514    ///     [`F128`]. For this conversion, the method rounds to the nearest,
515    ///     with ties rounding to even.
516    ///   * Any other type `Dst` for which [`FromFixed`] is
517    ///     implemented, in which case this method returns
518    ///     <code>Dst::[unwrapped\_from\_fixed][FromFixed::unwrapped_from_fixed]\(self.0)</code>.
519    ///
520    /// See also
521    /// <code>FixedI32::[unwrapped\_to\_num][FixedI32::unwrapped_to_num]</code>
522    /// and
523    /// <code>FixedU32::[unwrapped\_to\_num][FixedU32::unwrapped_to_num]</code>.
524    ///
525    /// # Examples
526    ///
527    /// ```rust
528    /// use fixed::types::{I16F16, I4F4};
529    /// use fixed::Unwrapped;
530    /// let src = Unwrapped(I4F4::from_num(1.75));
531    /// assert_eq!(src.to_num::<I16F16>(), I16F16::from_num(1.75));
532    /// ```
533    ///
534    /// The following panics even when debug assertions are disabled.
535    ///
536    /// ```should_panic
537    /// use fixed::types::{I2F6, I4F4};
538    /// use fixed::Unwrapped;
539    /// let src = Unwrapped(I4F4::MAX);
540    /// let _overflow = src.to_num::<I2F6>();
541    /// ```
542    ///
543    /// [`F128`]: crate::F128
544    #[inline]
545    #[track_caller]
546    pub fn to_num<Dst: FromFixed>(self) -> Dst {
547        Dst::unwrapped_from_fixed(self.0)
548    }
549
550    /// Parses a string slice containing decimal digits to return a fixed-point number.
551    ///
552    /// Rounding is to the nearest, with ties rounded to even.
553    ///
554    /// See also
555    /// <code>FixedI32::[unwrapped\_from\_str][FixedI32::unwrapped_from_str]</code>
556    /// and
557    /// <code>FixedU32::[unwrapped\_from\_str][FixedU32::unwrapped_from_str]</code>.
558    ///
559    /// # Panics
560    ///
561    /// Panics if the value does not fit or if there is a parsing error.
562    ///
563    /// # Examples
564    ///
565    /// ```rust
566    /// use fixed::types::I8F8;
567    /// use fixed::Unwrapped;
568    /// // 16 + 3/4 = 16.75
569    /// let check = Unwrapped(I8F8::from_bits((16 << 8) + (3 << 8) / 4));
570    /// assert_eq!(Unwrapped::<I8F8>::from_str_dec("16.75"), check);
571    /// ```
572    ///
573    /// The following panics because of a parsing error.
574    ///
575    /// ```rust,should_panic
576    /// use fixed::types::I8F8;
577    /// use fixed::Unwrapped;
578    /// let _error = Unwrapped::<I8F8>::from_str_dec("1.2.");
579    /// ```
580    #[inline]
581    #[track_caller]
582    #[must_use]
583    pub fn from_str_dec(src: &str) -> Unwrapped<F> {
584        Unwrapped(F::unwrapped_from_str(src))
585    }
586
587    /// Parses a string slice containing binary digits to return a fixed-point number.
588    ///
589    /// Rounding is to the nearest, with ties rounded to even.
590    ///
591    /// See also
592    /// <code>FixedI32::[unwrapped\_from\_str\_binary][FixedI32::unwrapped_from_str_binary]</code>
593    /// and
594    /// <code>FixedU32::[unwrapped\_from\_str\_binary][FixedU32::unwrapped_from_str_binary]</code>.
595    ///
596    /// # Panics
597    ///
598    /// Panics if the value does not fit or if there is a parsing error.
599    ///
600    /// # Examples
601    ///
602    /// ```rust
603    /// use fixed::types::I8F8;
604    /// use fixed::Unwrapped;
605    /// let check = Unwrapped(I8F8::from_bits(0b1110001 << (8 - 1)));
606    /// assert_eq!(Unwrapped::<I8F8>::from_str_binary("111000.1"), Ok(check));
607    /// ```
608    ///
609    /// The following panics because of a parsing error.
610    ///
611    /// ```rust,should_panic
612    /// use fixed::types::I8F8;
613    /// use fixed::Unwrapped;
614    /// let _error = Unwrapped::<I8F8>::from_str_binary("1.2");
615    /// ```
616    ///
617    /// # Compatibility note
618    ///
619    /// This method either returns [`Ok`] or panics, and never returns [`Err`].
620    /// In version 2, this method will not return a [`Result`], but will return
621    /// the fixed-point number directly similarly to [`from_str_dec`].
622    ///
623    /// [`from_str_dec`]: Self::from_str_dec
624    #[inline]
625    #[track_caller]
626    pub fn from_str_binary(src: &str) -> Result<Unwrapped<F>, ParseFixedError> {
627        Ok(Unwrapped(F::unwrapped_from_str_binary(src)))
628    }
629
630    /// Parses a string slice containing octal digits to return a fixed-point number.
631    ///
632    /// Rounding is to the nearest, with ties rounded to even.
633    ///
634    /// See also
635    /// <code>FixedI32::[unwrapped\_from\_str\_octal][FixedI32::unwrapped_from_str_octal]</code>
636    /// and
637    /// <code>FixedU32::[unwrapped\_from\_str\_octal][FixedU32::unwrapped_from_str_octal]</code>.
638    ///
639    /// # Panics
640    ///
641    /// Panics if the value does not fit or if there is a parsing error.
642    ///
643    /// # Examples
644    ///
645    /// ```rust
646    /// use fixed::types::I8F8;
647    /// use fixed::Unwrapped;
648    /// let check = Unwrapped(I8F8::from_bits(0o1654 << (8 - 3)));
649    /// assert_eq!(Unwrapped::<I8F8>::from_str_octal("165.4"), Ok(check));
650    /// ```
651    ///
652    /// The following panics because of a parsing error.
653    ///
654    /// ```rust,should_panic
655    /// use fixed::types::I8F8;
656    /// use fixed::Unwrapped;
657    /// let _error = Unwrapped::<I8F8>::from_str_octal("1.8");
658    /// ```
659    ///
660    /// # Compatibility note
661    ///
662    /// This method either returns [`Ok`] or panics, and never returns [`Err`].
663    /// In version 2, this method will not return a [`Result`], but will return
664    /// the fixed-point number directly similarly to [`from_str_dec`].
665    ///
666    /// [`from_str_dec`]: Self::from_str_dec
667    #[inline]
668    #[track_caller]
669    pub fn from_str_octal(src: &str) -> Result<Unwrapped<F>, ParseFixedError> {
670        Ok(Unwrapped(F::unwrapped_from_str_octal(src)))
671    }
672
673    /// Parses a string slice containing hexadecimal digits to return a fixed-point number.
674    ///
675    /// Rounding is to the nearest, with ties rounded to even.
676    ///
677    /// See also
678    /// <code>FixedI32::[unwrapped\_from\_str\_hex][FixedI32::unwrapped_from_str_hex]</code>
679    /// and
680    /// <code>FixedU32::[unwrapped\_from\_str\_hex][FixedU32::unwrapped_from_str_hex]</code>.
681    ///
682    /// # Panics
683    ///
684    /// Panics if the value does not fit or if there is a parsing error.
685    ///
686    /// # Examples
687    ///
688    /// ```rust
689    /// use fixed::types::I8F8;
690    /// use fixed::Unwrapped;
691    /// let check = Unwrapped(I8F8::from_bits(0xFFE));
692    /// assert_eq!(Unwrapped::<I8F8>::from_str_hex("F.FE"), Ok(check));
693    /// ```
694    ///
695    /// The following panics because of a parsing error.
696    ///
697    /// ```rust,should_panic
698    /// use fixed::types::I8F8;
699    /// use fixed::Unwrapped;
700    /// let _error = Unwrapped::<I8F8>::from_str_hex("1.G");
701    /// ```
702    ///
703    /// # Compatibility note
704    ///
705    /// This method either returns [`Ok`] or panics, and never returns [`Err`].
706    /// In version 2, this method will not return a [`Result`], but will return
707    /// the fixed-point number directly similarly to [`from_str_dec`].
708    ///
709    /// [`from_str_dec`]: Self::from_str_dec
710    #[inline]
711    #[track_caller]
712    pub fn from_str_hex(src: &str) -> Result<Unwrapped<F>, ParseFixedError> {
713        Ok(Unwrapped(F::unwrapped_from_str_hex(src)))
714    }
715
716    /// Parses an ASCII-byte slice containing decimal digits to return a fixed-point number.
717    ///
718    /// Rounding is to the nearest, with ties rounded to even.
719    ///
720    /// See also
721    /// <code>FixedI32::[unwrapped\_from\_ascii][FixedI32::unwrapped_from_ascii]</code>
722    /// and
723    /// <code>FixedU32::[unwrapped\_from\_ascii][FixedU32::unwrapped_from_ascii]</code>.
724    ///
725    /// # Panics
726    ///
727    /// Panics if the value does not fit or if there is a parsing error.
728    ///
729    /// # Examples
730    ///
731    /// ```rust
732    /// use fixed::types::I8F8;
733    /// use fixed::Unwrapped;
734    /// // 16 + 3/4 = 16.75
735    /// let check = Unwrapped(I8F8::from_bits((16 << 8) + (3 << 8) / 4));
736    /// assert_eq!(Unwrapped::<I8F8>::from_ascii(b"16.75"), check);
737    /// ```
738    ///
739    /// The following panics because of a parsing error.
740    ///
741    /// ```rust,should_panic
742    /// use fixed::types::I8F8;
743    /// use fixed::Unwrapped;
744    /// let _error = Unwrapped::<I8F8>::from_ascii(b"1.2.");
745    /// ```
746    #[inline]
747    #[track_caller]
748    #[must_use]
749    pub fn from_ascii(src: &[u8]) -> Unwrapped<F> {
750        Unwrapped(F::unwrapped_from_ascii(src))
751    }
752
753    /// Parses an ASCII-byte slice containing binary digits to return a fixed-point number.
754    ///
755    /// Rounding is to the nearest, with ties rounded to even.
756    ///
757    /// See also
758    /// <code>FixedI32::[unwrapped\_from\_ascii\_binary][FixedI32::unwrapped_from_ascii_binary]</code>
759    /// and
760    /// <code>FixedU32::[unwrapped\_from\_ascii\_binary][FixedU32::unwrapped_from_ascii_binary]</code>.
761    ///
762    /// # Panics
763    ///
764    /// Panics if the value does not fit or if there is a parsing error.
765    ///
766    /// # Examples
767    ///
768    /// ```rust
769    /// use fixed::types::I8F8;
770    /// use fixed::Unwrapped;
771    /// let check = Unwrapped(I8F8::from_bits(0b1110001 << (8 - 1)));
772    /// assert_eq!(Unwrapped::<I8F8>::from_ascii_binary(b"111000.1"), check);
773    /// ```
774    ///
775    /// The following panics because of a parsing error.
776    ///
777    /// ```rust,should_panic
778    /// use fixed::types::I8F8;
779    /// use fixed::Unwrapped;
780    /// let _error = Unwrapped::<I8F8>::from_ascii_binary(b"1.2");
781    /// ```
782    #[inline]
783    #[track_caller]
784    pub fn from_ascii_binary(src: &[u8]) -> Unwrapped<F> {
785        Unwrapped(F::unwrapped_from_ascii_binary(src))
786    }
787
788    /// Parses an ASCII-byte slice containing octal digits to return a fixed-point number.
789    ///
790    /// Rounding is to the nearest, with ties rounded to even.
791    ///
792    /// See also
793    /// <code>FixedI32::[unwrapped\_from\_ascii\_octal][FixedI32::unwrapped_from_ascii_octal]</code>
794    /// and
795    /// <code>FixedU32::[unwrapped\_from\_ascii\_octal][FixedU32::unwrapped_from_ascii_octal]</code>.
796    ///
797    /// # Panics
798    ///
799    /// Panics if the value does not fit or if there is a parsing error.
800    ///
801    /// # Examples
802    ///
803    /// ```rust
804    /// use fixed::types::I8F8;
805    /// use fixed::Unwrapped;
806    /// let check = Unwrapped(I8F8::from_bits(0o1654 << (8 - 3)));
807    /// assert_eq!(Unwrapped::<I8F8>::from_ascii_octal(b"165.4"), check);
808    /// ```
809    ///
810    /// The following panics because of a parsing error.
811    ///
812    /// ```rust,should_panic
813    /// use fixed::types::I8F8;
814    /// use fixed::Unwrapped;
815    /// let _error = Unwrapped::<I8F8>::from_ascii_octal(b"1.8");
816    /// ```
817    #[inline]
818    #[track_caller]
819    pub fn from_ascii_octal(src: &[u8]) -> Unwrapped<F> {
820        Unwrapped(F::unwrapped_from_ascii_octal(src))
821    }
822
823    /// Parses an ASCII-byte slice containing hexadecimal digits to return a fixed-point number.
824    ///
825    /// Rounding is to the nearest, with ties rounded to even.
826    ///
827    /// See also
828    /// <code>FixedI32::[unwrapped\_from\_ascii\_hex][FixedI32::unwrapped_from_ascii_hex]</code>
829    /// and
830    /// <code>FixedU32::[unwrapped\_from\_ascii\_hex][FixedU32::unwrapped_from_ascii_hex]</code>.
831    ///
832    /// # Panics
833    ///
834    /// Panics if the value does not fit or if there is a parsing error.
835    ///
836    /// # Examples
837    ///
838    /// ```rust
839    /// use fixed::types::I8F8;
840    /// use fixed::Unwrapped;
841    /// let check = Unwrapped(I8F8::from_bits(0xFFE));
842    /// assert_eq!(Unwrapped::<I8F8>::from_ascii_hex(b"F.FE"), check);
843    /// ```
844    ///
845    /// The following panics because of a parsing error.
846    ///
847    /// ```rust,should_panic
848    /// use fixed::types::I8F8;
849    /// use fixed::Unwrapped;
850    /// let _error = Unwrapped::<I8F8>::from_ascii_hex(b"1.G");
851    /// ```
852    #[inline]
853    #[track_caller]
854    pub fn from_ascii_hex(src: &[u8]) -> Unwrapped<F> {
855        Unwrapped(F::unwrapped_from_ascii_hex(src))
856    }
857
858    /// Returns the integer part.
859    ///
860    /// Note that since the numbers are stored in two’s complement,
861    /// negative numbers with non-zero fractional parts will be
862    /// rounded towards &minus;∞, except in the case where there are no
863    /// integer bits, for example for the type
864    /// <code>[Unwrapped]&lt;[I0F16]&gt;</code>, where the return
865    /// value is always zero.
866    ///
867    /// See also <code>FixedI32::[int][FixedI32::int]</code> and
868    /// <code>FixedU32::[int][FixedU32::int]</code>.
869    ///
870    /// # Examples
871    ///
872    /// ```rust
873    /// use fixed::types::I16F16;
874    /// use fixed::Unwrapped;
875    /// assert_eq!(Unwrapped(I16F16::from_num(12.25)).int(), Unwrapped(I16F16::from_num(12)));
876    /// assert_eq!(Unwrapped(I16F16::from_num(-12.25)).int(), Unwrapped(I16F16::from_num(-13)));
877    /// ```
878    ///
879    /// [I0F16]: crate::types::I0F16
880    #[inline]
881    #[must_use]
882    pub fn int(self) -> Unwrapped<F> {
883        Unwrapped(self.0.int())
884    }
885
886    /// Returns the fractional part.
887    ///
888    /// Note that since the numbers are stored in two’s complement,
889    /// the returned fraction will be non-negative for negative
890    /// numbers, except in the case where there are no integer bits,
891    /// for example for the type
892    /// <code>[Unwrapped]&lt;[I0F16]&gt;</code>, where the return
893    /// value is always equal to `self`.
894    ///
895    /// See also <code>FixedI32::[frac][FixedI32::frac]</code> and
896    /// <code>FixedU32::[frac][FixedU32::frac]</code>.
897    ///
898    /// # Examples
899    ///
900    /// ```rust
901    /// use fixed::types::I16F16;
902    /// use fixed::Unwrapped;
903    /// assert_eq!(Unwrapped(I16F16::from_num(12.25)).frac(), Unwrapped(I16F16::from_num(0.25)));
904    /// assert_eq!(Unwrapped(I16F16::from_num(-12.25)).frac(), Unwrapped(I16F16::from_num(0.75)));
905    /// ```
906    ///
907    /// [I0F16]: crate::types::I0F16
908    #[inline]
909    #[must_use]
910    pub fn frac(self) -> Unwrapped<F> {
911        Unwrapped(self.0.frac())
912    }
913
914    /// Rounds to the next integer towards 0.
915    ///
916    /// See also
917    /// <code>FixedI32::[round\_to\_zero][FixedI32::round_to_zero]</code> and
918    /// <code>FixedU32::[round\_to\_zero][FixedU32::round_to_zero]</code>.
919    ///
920    /// # Examples
921    ///
922    /// ```rust
923    /// use fixed::types::I16F16;
924    /// use fixed::Unwrapped;
925    /// let three = Unwrapped(I16F16::from_num(3));
926    /// assert_eq!(Unwrapped(I16F16::from_num(3.9)).round_to_zero(), three);
927    /// assert_eq!(Unwrapped(I16F16::from_num(-3.9)).round_to_zero(), -three);
928    /// ```
929    #[inline]
930    #[must_use]
931    pub fn round_to_zero(self) -> Unwrapped<F> {
932        Unwrapped(self.0.round_to_zero())
933    }
934
935    /// Unwrapped ceil. Rounds to the next integer towards +∞, panicking
936    /// on overflow.
937    ///
938    /// See also
939    /// <code>FixedI32::[unwrapped\_ceil][FixedI32::unwrapped_ceil]</code> and
940    /// <code>FixedU32::[unwrapped\_ceil][FixedU32::unwrapped_ceil]</code>.
941    ///
942    /// # Panics
943    ///
944    /// Panics if the result does not fit.
945    ///
946    /// # Examples
947    ///
948    /// ```rust
949    /// use fixed::types::I16F16;
950    /// use fixed::Unwrapped;
951    /// let two_half = Unwrapped(I16F16::from_num(5) / 2);
952    /// assert_eq!(two_half.ceil(), Unwrapped(I16F16::from_num(3)));
953    /// ```
954    ///
955    /// The following panics because of overflow.
956    ///
957    /// ```should_panic
958    /// use fixed::types::I16F16;
959    /// use fixed::Unwrapped;
960    /// let _overflow = Unwrapped(I16F16::MAX).ceil();
961    /// ```
962    #[inline]
963    #[track_caller]
964    #[must_use]
965    pub fn ceil(self) -> Unwrapped<F> {
966        Unwrapped(self.0.unwrapped_ceil())
967    }
968
969    /// Unwrapped floor. Rounds to the next integer towards &minus;∞,
970    /// panicking on overflow.
971    ///
972    /// Overflow can only occur for signed numbers with zero integer
973    /// bits.
974    ///
975    /// See also
976    /// <code>FixedI32::[unwrapped\_floor][FixedI32::unwrapped_floor]</code> and
977    /// <code>FixedU32::[unwrapped\_floor][FixedU32::unwrapped_floor]</code>.
978    ///
979    /// # Panics
980    ///
981    /// Panics if the result does not fit.
982    ///
983    /// # Examples
984    ///
985    /// ```rust
986    /// use fixed::types::I16F16;
987    /// use fixed::Unwrapped;
988    /// let two_half = Unwrapped(I16F16::from_num(5) / 2);
989    /// assert_eq!(two_half.floor(), Unwrapped(I16F16::from_num(2)));
990    /// ```
991    ///
992    /// The following panics because of overflow.
993    ///
994    /// ```should_panic
995    /// use fixed::types::I0F32;
996    /// use fixed::Unwrapped;
997    /// let _overflow = Unwrapped(I0F32::MIN).floor();
998    /// ```
999    #[inline]
1000    #[track_caller]
1001    #[must_use]
1002    pub fn floor(self) -> Unwrapped<F> {
1003        Unwrapped(self.0.unwrapped_floor())
1004    }
1005
1006    /// Unwrapped round. Rounds to the next integer to the nearest,
1007    /// with ties rounded away from zero, and panics on overflow.
1008    ///
1009    /// See also
1010    /// <code>FixedI32::[unwrapped\_round][FixedI32::unwrapped_round]</code> and
1011    /// <code>FixedU32::[unwrapped\_round][FixedU32::unwrapped_round]</code>.
1012    ///
1013    /// # Panics
1014    ///
1015    /// Panics if the result does not fit.
1016    ///
1017    /// # Examples
1018    ///
1019    /// ```rust
1020    /// use fixed::types::I16F16;
1021    /// use fixed::Unwrapped;
1022    /// let two_half = Unwrapped(I16F16::from_num(5) / 2);
1023    /// assert_eq!(two_half.round(), Unwrapped(I16F16::from_num(3)));
1024    /// assert_eq!((-two_half).round(), Unwrapped(I16F16::from_num(-3)));
1025    /// ```
1026    ///
1027    /// The following panics because of overflow.
1028    ///
1029    /// ```should_panic
1030    /// use fixed::types::I16F16;
1031    /// use fixed::Unwrapped;
1032    /// let _overflow = Unwrapped(I16F16::MAX).round();
1033    /// ```
1034    #[inline]
1035    #[track_caller]
1036    #[must_use]
1037    pub fn round(self) -> Unwrapped<F> {
1038        Unwrapped(self.0.unwrapped_round())
1039    }
1040
1041    /// Unwrapped round. Rounds to the next integer to the nearest, with ties
1042    /// rounded to even, and panics on overflow.
1043    ///
1044    /// See also
1045    /// <code>FixedI32::[unwrapped\_round\_ties\_even][FixedI32::unwrapped_round_ties_even]</code>
1046    /// and
1047    /// <code>FixedU32::[unwrapped\_round\_ties\_even][FixedU32::unwrapped_round_ties_even]</code>.
1048    ///
1049    /// # Panics
1050    ///
1051    /// Panics if the result does not fit.
1052    ///
1053    /// # Examples
1054    ///
1055    /// ```rust
1056    /// use fixed::types::I16F16;
1057    /// use fixed::Unwrapped;
1058    /// let two_half = Unwrapped(I16F16::from_num(2.5));
1059    /// assert_eq!(two_half.round_ties_even(), Unwrapped(I16F16::from_num(2)));
1060    /// let three_half = Unwrapped(I16F16::from_num(3.5));
1061    /// assert_eq!(three_half.round_ties_even(), Unwrapped(I16F16::from_num(4)));
1062    /// ```
1063    ///
1064    /// The following panics because of overflow.
1065    ///
1066    /// ```should_panic
1067    /// use fixed::types::I16F16;
1068    /// use fixed::Unwrapped;
1069    /// let max = Unwrapped(I16F16::MAX);
1070    /// let _overflow = max.round_ties_even();
1071    /// ```
1072    #[inline]
1073    #[track_caller]
1074    #[must_use]
1075    pub fn round_ties_even(self) -> Unwrapped<F> {
1076        Unwrapped(self.0.unwrapped_round_ties_even())
1077    }
1078
1079    /// Returns the number of ones in the binary representation.
1080    ///
1081    /// See also <code>FixedI32::[count\_ones][FixedI32::count_ones]</code> and
1082    /// <code>FixedU32::[count\_ones][FixedU32::count_ones]</code>.
1083    ///
1084    /// # Examples
1085    ///
1086    /// ```rust
1087    /// use fixed::types::I16F16;
1088    /// use fixed::Unwrapped;
1089    /// let w = Unwrapped(I16F16::from_bits(0x00FF_FF00));
1090    /// assert_eq!(w.count_ones(), w.0.count_ones());
1091    /// ```
1092    #[inline]
1093    #[doc(alias("popcount", "popcnt"))]
1094    pub fn count_ones(self) -> u32 {
1095        self.0.count_ones()
1096    }
1097
1098    /// Returns the number of zeros in the binary representation.
1099    ///
1100    /// See also <code>FixedI32::[count\_zeros][FixedI32::count_zeros]</code>
1101    /// and <code>FixedU32::[count\_zeros][FixedU32::count_zeros]</code>.
1102    ///
1103    /// # Examples
1104    ///
1105    /// ```rust
1106    /// use fixed::types::I16F16;
1107    /// use fixed::Unwrapped;
1108    /// let w = Unwrapped(I16F16::from_bits(0x00FF_FF00));
1109    /// assert_eq!(w.count_zeros(), w.0.count_zeros());
1110    /// ```
1111    #[inline]
1112    pub fn count_zeros(self) -> u32 {
1113        self.0.count_zeros()
1114    }
1115
1116    /// Returns the number of leading ones in the binary representation.
1117    ///
1118    /// See also <code>FixedI32::[leading\_ones][FixedI32::leading_ones]</code>
1119    /// and <code>FixedU32::[leading\_ones][FixedU32::leading_ones]</code>.
1120    ///
1121    /// # Examples
1122    ///
1123    /// ```rust
1124    /// use fixed::types::U16F16;
1125    /// use fixed::Unwrapped;
1126    /// let w = Unwrapped(U16F16::from_bits(0xFF00_00FF));
1127    /// assert_eq!(w.leading_ones(), w.0.leading_ones());
1128    /// ```
1129    #[inline]
1130    pub fn leading_ones(self) -> u32 {
1131        self.0.leading_ones()
1132    }
1133
1134    /// Returns the number of leading zeros in the binary representation.
1135    ///
1136    /// See also
1137    /// <code>FixedI32::[leading\_zeros][FixedI32::leading_zeros]</code> and
1138    /// <code>FixedU32::[leading\_zeros][FixedU32::leading_zeros]</code>.
1139    ///
1140    /// # Examples
1141    ///
1142    /// ```rust
1143    /// use fixed::types::I16F16;
1144    /// use fixed::Unwrapped;
1145    /// let w = Unwrapped(I16F16::from_bits(0x00FF_FF00));
1146    /// assert_eq!(w.leading_zeros(), w.0.leading_zeros());
1147    /// ```
1148    #[inline]
1149    pub fn leading_zeros(self) -> u32 {
1150        self.0.leading_zeros()
1151    }
1152
1153    /// Returns the number of trailing ones in the binary representation.
1154    ///
1155    /// See also
1156    /// <code>FixedI32::[trailing\_ones][FixedI32::trailing_ones]</code> and
1157    /// <code>FixedU32::[trailing\_ones][FixedU32::trailing_ones]</code>.
1158    ///
1159    /// # Examples
1160    ///
1161    /// ```rust
1162    /// use fixed::types::U16F16;
1163    /// use fixed::Unwrapped;
1164    /// let w = Unwrapped(U16F16::from_bits(0xFF00_00FF));
1165    /// assert_eq!(w.trailing_ones(), w.0.trailing_ones());
1166    /// ```
1167    #[inline]
1168    pub fn trailing_ones(self) -> u32 {
1169        self.0.trailing_ones()
1170    }
1171
1172    /// Returns the number of trailing zeros in the binary representation.
1173    ///
1174    /// See also
1175    /// <code>FixedI32::[trailing\_zeros][FixedI32::trailing_zeros]</code> and
1176    /// <code>FixedU32::[trailing\_zeros][FixedU32::trailing_zeros]</code>.
1177    ///
1178    /// # Examples
1179    ///
1180    /// ```rust
1181    /// use fixed::types::I16F16;
1182    /// use fixed::Unwrapped;
1183    /// let w = Unwrapped(I16F16::from_bits(0x00FF_FF00));
1184    /// assert_eq!(w.trailing_zeros(), w.0.trailing_zeros());
1185    /// ```
1186    #[inline]
1187    pub fn trailing_zeros(self) -> u32 {
1188        self.0.trailing_zeros()
1189    }
1190
1191    /// Returns the square root.
1192    ///
1193    /// See also
1194    /// <code>FixedI32::[unwrapped\_sqrt][FixedI32::unwrapped_sqrt]</code> and
1195    /// <code>FixedU32::[unwrapped\_sqrt][FixedU32::unwrapped_sqrt]</code>.
1196    ///
1197    /// # Panics
1198    ///
1199    /// Panics if the number is negative, or on overflow.
1200    ///
1201    /// # Examples
1202    ///
1203    /// ```rust
1204    /// use fixed::types::I0F32;
1205    /// use fixed::Unwrapped;
1206    /// assert_eq!(Unwrapped(I0F32::lit("0b0.0001")).sqrt().0, I0F32::lit("0b0.01"));
1207    /// ```
1208    ///
1209    /// The following panics because the input value is negative.
1210    ///
1211    /// ```should_panic
1212    /// use fixed::types::I16F16;
1213    /// use fixed::Unwrapped;
1214    /// let neg = Unwrapped(I16F16::from_num(-1));
1215    /// let _sqrt_neg = neg.sqrt();
1216    /// ```
1217    ///
1218    /// The following panics because of overflow.
1219    ///
1220    /// ```should_panic
1221    /// use fixed::types::I0F32;
1222    /// use fixed::Unwrapped;
1223    /// let u = Unwrapped(I0F32::from_num(0.25));
1224    /// let _overflow = u.sqrt();
1225    /// ```
1226    #[inline]
1227    #[track_caller]
1228    pub fn sqrt(self) -> Self {
1229        Unwrapped(self.0.unwrapped_sqrt())
1230    }
1231
1232    /// Integer base-2 logarithm, rounded down.
1233    ///
1234    /// See also <code>FixedI32::[int\_log2][FixedI32::int_log2]</code> and
1235    /// <code>FixedU32::[int\_log2][FixedU32::int_log2]</code>.
1236    ///
1237    /// # Panics
1238    ///
1239    /// Panics if the fixed-point number is ≤&nbsp;0.
1240    #[inline]
1241    #[track_caller]
1242    #[doc(alias("ilog2"))]
1243    pub fn int_log2(self) -> i32 {
1244        self.0.int_log2()
1245    }
1246
1247    /// Integer base-10 logarithm, rounded down.
1248    ///
1249    /// See also <code>FixedI32::[int\_log10][FixedI32::int_log10]</code> and
1250    /// <code>FixedU32::[int\_log10][FixedU32::int_log10]</code>.
1251    ///
1252    /// # Panics
1253    ///
1254    /// Panics if the fixed-point number is ≤&nbsp;0.
1255    #[inline]
1256    #[track_caller]
1257    #[doc(alias("ilog10"))]
1258    pub fn int_log10(self) -> i32 {
1259        self.0.int_log10()
1260    }
1261
1262    /// Integer logarithm to the specified base, rounded down.
1263    ///
1264    /// See also <code>FixedI32::[int\_log][FixedI32::int_log]</code> and
1265    /// <code>FixedU32::[int\_log][FixedU32::int_log]</code>.
1266    ///
1267    /// # Panics
1268    ///
1269    /// Panics if the fixed-point number is ≤&nbsp;0 or if the base is <&nbsp;2.
1270    #[inline]
1271    #[track_caller]
1272    #[doc(alias("ilog"))]
1273    pub fn int_log(self, base: u32) -> i32 {
1274        self.0.int_log(base)
1275    }
1276
1277    /// Reverses the order of the bits of the fixed-point number.
1278    ///
1279    /// See also <code>FixedI32::[reverse\_bits][FixedI32::reverse_bits]</code>
1280    /// and <code>FixedU32::[reverse\_bits][FixedU32::reverse_bits]</code>.
1281    ///
1282    /// # Examples
1283    ///
1284    /// ```rust
1285    /// use fixed::types::I16F16;
1286    /// use fixed::Unwrapped;
1287    /// let i = I16F16::from_bits(0x1234_5678);
1288    /// assert_eq!(Unwrapped(i).reverse_bits(), Unwrapped(i.reverse_bits()));
1289    /// ```
1290    #[inline]
1291    #[must_use = "this returns the result of the operation, without modifying the original"]
1292    pub fn reverse_bits(self) -> Unwrapped<F> {
1293        Unwrapped(self.0.reverse_bits())
1294    }
1295
1296    /// Shifts to the left by `n` bits, unwrapped the truncated bits to the right end.
1297    ///
1298    /// See also <code>FixedI32::[rotate\_left][FixedI32::rotate_left]</code>
1299    /// and <code>FixedU32::[rotate\_left][FixedU32::rotate_left]</code>.
1300    ///
1301    /// # Examples
1302    ///
1303    /// ```rust
1304    /// use fixed::types::I16F16;
1305    /// use fixed::Unwrapped;
1306    /// let i = I16F16::from_bits(0x00FF_FF00);
1307    /// assert_eq!(Unwrapped(i).rotate_left(12), Unwrapped(i.rotate_left(12)));
1308    /// ```
1309    #[inline]
1310    #[must_use = "this returns the result of the operation, without modifying the original"]
1311    pub fn rotate_left(self, n: u32) -> Unwrapped<F> {
1312        Unwrapped(self.0.rotate_left(n))
1313    }
1314
1315    /// Shifts to the right by `n` bits, unwrapped the truncated bits to the left end.
1316    ///
1317    /// See also <code>FixedI32::[rotate\_right][FixedI32::rotate_right]</code>
1318    /// and <code>FixedU32::[rotate\_right][FixedU32::rotate_right]</code>.
1319    ///
1320    /// # Examples
1321    ///
1322    /// ```rust
1323    /// use fixed::types::I16F16;
1324    /// use fixed::Unwrapped;
1325    /// let i = I16F16::from_bits(0x00FF_FF00);
1326    /// assert_eq!(Unwrapped(i).rotate_right(12), Unwrapped(i.rotate_right(12)));
1327    /// ```
1328    #[inline]
1329    #[must_use = "this returns the result of the operation, without modifying the original"]
1330    pub fn rotate_right(self, n: u32) -> Unwrapped<F> {
1331        Unwrapped(self.0.rotate_right(n))
1332    }
1333
1334    /// Returns [`true`] if the number is zero.
1335    ///
1336    /// See also <code>FixedI32::[is\_zero][FixedI32::is_zero]</code> and
1337    /// <code>FixedU32::[is\_zero][FixedU32::is_zero]</code>.
1338    ///
1339    /// # Examples
1340    ///
1341    /// ```rust
1342    /// use fixed::types::I16F16;
1343    /// use fixed::Unwrapped;
1344    /// assert!(Unwrapped(I16F16::ZERO).is_zero());
1345    /// assert!(!Unwrapped(I16F16::from_num(4.3)).is_zero());
1346    /// ```
1347    #[inline]
1348    pub fn is_zero(self) -> bool {
1349        self.0.is_zero()
1350    }
1351
1352    /// Returns the distance from `self` to `other`.
1353    ///
1354    /// See also
1355    /// <code>FixedI32::[unwrapped\_dist][FixedI32::unwrapped_dist]</code> and
1356    /// <code>FixedU32::[unwrapped\_dist][FixedU32::unwrapped_dist]</code>.
1357    ///
1358    /// # Panics
1359    ///
1360    /// Panics on overflow.
1361    ///
1362    /// # Examples
1363    ///
1364    /// ```rust
1365    /// use fixed::types::I16F16;
1366    /// use fixed::Unwrapped;
1367    /// type Unwr = Unwrapped<I16F16>;
1368    /// assert_eq!(Unwr::from_num(-1).dist(Unwr::from_num(4)), Unwr::from_num(5));
1369    /// ```
1370    ///
1371    /// The following panics because of overflow.
1372    ///
1373    /// ```should_panic
1374    /// use fixed::types::I16F16;
1375    /// use fixed::Unwrapped;
1376    /// type Unwr = Unwrapped<I16F16>;
1377    /// let _overflow = Unwr::MIN.dist(Unwr::ZERO);
1378    /// ```
1379    #[inline]
1380    #[track_caller]
1381    #[must_use = "this returns the result of the operation, without modifying the original"]
1382    pub fn dist(self, other: Unwrapped<F>) -> Unwrapped<F> {
1383        Unwrapped(self.0.unwrapped_dist(other.0))
1384    }
1385
1386    /// Returns the mean of `self` and `other`.
1387    ///
1388    /// See also <code>FixedI32::[mean][FixedI32::mean]</code> and
1389    /// <code>FixedU32::[mean][FixedU32::mean]</code>.
1390    ///
1391    /// # Examples
1392    ///
1393    /// ```rust
1394    /// use fixed::types::I16F16;
1395    /// use fixed::Unwrapped;
1396    /// let three = Unwrapped(I16F16::from_num(3));
1397    /// let four = Unwrapped(I16F16::from_num(4));
1398    /// assert_eq!(three.mean(four), Unwrapped(I16F16::from_num(3.5)));
1399    /// assert_eq!(three.mean(-four), Unwrapped(I16F16::from_num(-0.5)));
1400    /// ```
1401    #[inline]
1402    #[must_use = "this returns the result of the operation, without modifying the original"]
1403    pub fn mean(self, other: Unwrapped<F>) -> Unwrapped<F> {
1404        Unwrapped(self.0.mean(other.0))
1405    }
1406
1407    /// Compute the hypotenuse of a right triange.
1408    ///
1409    /// See also
1410    /// <code>FixedI32::[unwrapped\_hypot][FixedI32::unwrapped_hypot]</code> and
1411    /// <code>FixedU32::[unwrapped\_hypot][FixedU32::unwrapped_hypot]</code>.
1412    ///
1413    /// # Examples
1414    ///
1415    /// ```rust
1416    /// use fixed::types::I8F8;
1417    /// use fixed::Unwrapped;
1418    /// type Unwr = Unwrapped<I8F8>;
1419    /// // hypot(3, 4) == 5
1420    /// assert_eq!(Unwr::from_num(3).hypot(Unwr::from_num(4)), Unwr::from_num(5));
1421    /// ```
1422    ///
1423    /// The following panics because of overflow.
1424    ///
1425    /// ```should_panic
1426    /// use fixed::types::I8F8;
1427    /// use fixed::Unwrapped;
1428    /// type Unwr = Unwrapped<I8F8>;
1429    /// // hypot(88, 105) == 137, which does not fit
1430    /// let _overflow = Unwr::from_num(88).hypot(Unwr::from_num(105));
1431    /// ```
1432    #[inline]
1433    #[must_use = "this returns the result of the operation, without modifying the original"]
1434    pub fn hypot(self, other: Unwrapped<F>) -> Unwrapped<F> {
1435        Unwrapped(self.0.unwrapped_hypot(other.0))
1436    }
1437
1438    /// Returns the reciprocal (inverse), 1/`self`.
1439    ///
1440    /// See also
1441    /// <code>FixedI32::[unwrapped\_recip][FixedI32::unwrapped_recip]</code> and
1442    /// <code>FixedU32::[unwrapped\_recip][FixedU32::unwrapped_recip]</code>.
1443    ///
1444    /// # Panics
1445    ///
1446    /// Panics if `self` is zero or on overflow.
1447    ///
1448    /// # Examples
1449    ///
1450    /// ```rust
1451    /// use fixed::types::I8F24;
1452    /// use fixed::Unwrapped;
1453    /// let quarter = Unwrapped(I8F24::from_num(0.25));
1454    /// assert_eq!(quarter.recip(), Unwrapped(I8F24::from_num(4)));
1455    /// ```
1456    ///
1457    /// The following panics because of overflow.
1458    ///
1459    /// ```should_panic
1460    /// use fixed::types::I8F24;
1461    /// use fixed::Unwrapped;
1462    /// let frac_1_512 = Unwrapped(I8F24::ONE / 512);
1463    /// let _overflow = frac_1_512.recip();
1464    /// ```
1465    #[inline]
1466    #[track_caller]
1467    #[must_use]
1468    pub fn recip(self) -> Unwrapped<F> {
1469        Unwrapped(self.0.unwrapped_recip())
1470    }
1471
1472    /// Returns the next multiple of `other`.
1473    ///
1474    /// See also
1475    /// <code>FixedI32::[unwrapped\_next\_multiple\_of][FixedI32::unwrapped_next_multiple_of]</code>
1476    /// and
1477    /// <code>FixedU32::[unwrapped\_next\_multiple\_of][FixedU32::unwrapped_next_multiple_of]</code>.
1478    ///
1479    /// # Panics
1480    ///
1481    /// Panics if `other` is zero or on overflow.
1482    ///
1483    /// # Examples
1484    ///
1485    /// ```rust
1486    /// use fixed::types::I16F16;
1487    /// use fixed::Unwrapped;
1488    /// let one_point_5 = Unwrapped::<I16F16>::from_num(1.5);
1489    /// let four = Unwrapped::<I16F16>::from_num(4);
1490    /// let four_point_5 = Unwrapped::<I16F16>::from_num(4.5);
1491    /// assert_eq!(four.next_multiple_of(one_point_5), four_point_5);
1492    /// ```
1493    ///
1494    /// The following panics because of overflow.
1495    ///
1496    /// ```rust,should_panic
1497    /// use fixed::types::I16F16;
1498    /// use fixed::Unwrapped;
1499    /// let two = Unwrapped::<I16F16>::from_num(2);
1500    /// let max = Unwrapped::<I16F16>::MAX;
1501    /// let _overflow = max.next_multiple_of(two);
1502    /// ```
1503    #[inline]
1504    #[track_caller]
1505    #[must_use]
1506    pub fn next_multiple_of(self, other: Unwrapped<F>) -> Unwrapped<F> {
1507        Unwrapped(self.0.unwrapped_next_multiple_of(other.0))
1508    }
1509
1510    /// Multiply and add. Returns `self` × `mul` + `add`.
1511    ///
1512    /// See also
1513    /// <code>FixedI32::[unwrapped\_mul\_add][FixedI32::unwrapped_mul_add]</code>
1514    /// and
1515    /// <code>FixedU32::[unwrapped\_mul\_add][FixedU32::unwrapped_mul_add]</code>.
1516    ///
1517    /// # Panics
1518    ///
1519    /// Panics if the result does not fit.
1520    ///
1521    /// # Examples
1522    ///
1523    /// ```rust
1524    /// use fixed::types::I16F16;
1525    /// use fixed::Unwrapped;
1526    /// let half = Unwrapped(I16F16::from_num(0.5));
1527    /// let three = Unwrapped(I16F16::from_num(3));
1528    /// let four = Unwrapped(I16F16::from_num(4));
1529    /// assert_eq!(three.mul_add(half, four), Unwrapped(I16F16::from_num(5.5)));
1530    /// // max × 1.5 - max = max / 2, which does not overflow
1531    /// let max = Unwrapped(I16F16::MAX);
1532    /// assert_eq!(max.mul_add(Unwrapped(I16F16::from_num(1.5)), -max), max / 2);
1533    /// ```
1534    ///
1535    /// The following panics because of overflow.
1536    ///
1537    /// ```should_panic
1538    /// use fixed::types::I16F16;
1539    /// use fixed::Unwrapped;
1540    /// let one = Unwrapped(I16F16::ONE);
1541    /// let max = Unwrapped(I16F16::MAX);
1542    /// let _overflow = max.mul_add(one, one);
1543    /// ```
1544    #[inline]
1545    #[track_caller]
1546    #[must_use = "this returns the result of the operation, without modifying the original"]
1547    pub fn mul_add(self, mul: Unwrapped<F>, add: Unwrapped<F>) -> Unwrapped<F> {
1548        Unwrapped(self.0.unwrapped_mul_add(mul.0, add.0))
1549    }
1550
1551    /// Adds `self` to the product `a`&nbsp;×&nbsp;`b`.
1552    ///
1553    /// See also
1554    /// <code>FixedI32::[unwrapped\_add\_prod][FixedI32::unwrapped_add_prod]</code>
1555    /// and
1556    /// <code>FixedU32::[unwrapped\_add\_prod][FixedU32::unwrapped_add_prod]</code>.
1557    ///
1558    /// # Panics
1559    ///
1560    /// Panics if the result does not fit.
1561    ///
1562    /// # Examples
1563    ///
1564    /// ```rust
1565    /// use fixed::types::I16F16;
1566    /// use fixed::Unwrapped;
1567    /// let half = Unwrapped(I16F16::from_num(0.5));
1568    /// let three = Unwrapped(I16F16::from_num(3));
1569    /// let four = Unwrapped(I16F16::from_num(4));
1570    /// assert_eq!(three.add_prod(four, half), Unwrapped(I16F16::from_num(5)));
1571    /// ```
1572    ///
1573    /// The following panics because of overflow.
1574    ///
1575    /// ```should_panic
1576    /// use fixed::types::I16F16;
1577    /// use fixed::Unwrapped;
1578    /// let max = Unwrapped(I16F16::MAX);
1579    /// let _overflow = max.add_prod(max, Unwrapped(I16F16::from_num(3)));
1580    /// ```
1581    #[inline]
1582    #[track_caller]
1583    #[must_use]
1584    pub fn add_prod(self, a: Unwrapped<F>, b: Unwrapped<F>) -> Unwrapped<F> {
1585        Unwrapped(self.0.unwrapped_add_prod(a.0, b.0))
1586    }
1587
1588    /// Multiply and accumulate. Adds (`a` × `b`) to `self`.
1589    ///
1590    /// See also
1591    /// <code>FixedI32::[unwrapped\_mul\_acc][FixedI32::unwrapped_mul_acc]</code>
1592    /// and
1593    /// <code>FixedU32::[unwrapped\_mul\_acc][FixedU32::unwrapped_mul_acc]</code>.
1594    ///
1595    /// # Panics
1596    ///
1597    /// Panics if the result does not fit.
1598    ///
1599    /// # Examples
1600    ///
1601    /// ```rust
1602    /// use fixed::types::I16F16;
1603    /// use fixed::Unwrapped;
1604    /// let mut acc = Unwrapped(I16F16::from_num(3));
1605    /// acc.mul_acc(Unwrapped(I16F16::from_num(4)), Unwrapped(I16F16::from_num(0.5)));
1606    /// assert_eq!(acc, Unwrapped(I16F16::from_num(5)));
1607    /// ```
1608    ///
1609    /// The following panics because of overflow.
1610    ///
1611    /// ```should_panic
1612    /// use fixed::types::I16F16;
1613    /// use fixed::Unwrapped;
1614    /// let mut acc = Unwrapped(I16F16::MAX);
1615    /// acc.mul_acc(Unwrapped(I16F16::MAX), Unwrapped(I16F16::from_num(3)));
1616    /// ```
1617    #[inline]
1618    #[track_caller]
1619    pub fn mul_acc(&mut self, a: Unwrapped<F>, b: Unwrapped<F>) {
1620        self.0.unwrapped_mul_acc(a.0, b.0);
1621    }
1622
1623    /// Euclidean division.
1624    ///
1625    /// See also
1626    /// <code>FixedI32::[unwrapped\_div\_euclid][FixedI32::unwrapped_div_euclid]</code>
1627    /// and
1628    /// <code>FixedU32::[unwrapped\_div\_euclid][FixedU32::unwrapped_div_euclid]</code>.
1629    ///
1630    /// # Panics
1631    ///
1632    /// Panics if the divisor is zero, or if the division results in overflow.
1633    ///
1634    /// # Examples
1635    ///
1636    /// ```rust
1637    /// use fixed::types::I16F16;
1638    /// use fixed::Unwrapped;
1639    /// let num = Unwrapped(I16F16::from_num(7.5));
1640    /// let den = Unwrapped(I16F16::from_num(2));
1641    /// assert_eq!(num.div_euclid(den), Unwrapped(I16F16::from_num(3)));
1642    /// ```
1643    ///
1644    /// The following panics because of overflow.
1645    ///
1646    /// ```should_panic
1647    /// use fixed::types::I16F16;
1648    /// use fixed::Unwrapped;
1649    /// let quarter = Unwrapped(I16F16::from_num(0.25));
1650    /// let _overflow = Unwrapped(I16F16::MAX).div_euclid(quarter);
1651    /// ```
1652    #[inline]
1653    #[track_caller]
1654    #[must_use = "this returns the result of the operation, without modifying the original"]
1655    pub fn div_euclid(self, divisor: Unwrapped<F>) -> Unwrapped<F> {
1656        Unwrapped(self.0.unwrapped_div_euclid(divisor.0))
1657    }
1658
1659    /// Remainder for Euclidean division.
1660    ///
1661    /// See also
1662    /// <code>FixedI32::[unwrapped\_rem\_euclid][FixedI32::unwrapped_rem_euclid]</code>
1663    /// and
1664    /// <code>FixedU32::[unwrapped\_rem\_euclid][FixedU32::unwrapped_rem_euclid]</code>.
1665    ///
1666    /// # Panics
1667    ///
1668    /// Panics if the divisor is zero.
1669    ///
1670    /// # Examples
1671    ///
1672    /// ```rust
1673    /// use fixed::types::I16F16;
1674    /// use fixed::Unwrapped;
1675    /// let num = Unwrapped(I16F16::from_num(7.5));
1676    /// let den = Unwrapped(I16F16::from_num(2));
1677    /// assert_eq!(num.rem_euclid(den), Unwrapped(I16F16::from_num(1.5)));
1678    /// assert_eq!((-num).rem_euclid(den), Unwrapped(I16F16::from_num(0.5)));
1679    /// ```
1680    #[inline]
1681    #[track_caller]
1682    #[must_use = "this returns the result of the operation, without modifying the original"]
1683    pub fn rem_euclid(self, divisor: Unwrapped<F>) -> Unwrapped<F> {
1684        Unwrapped(self.0.unwrapped_rem_euclid(divisor.0))
1685    }
1686
1687    /// Euclidean division by an integer.
1688    ///
1689    /// See also
1690    /// <code>FixedI32::[unwrapped\_div\_euclid\_int][FixedI32::unwrapped_div_euclid_int]</code>
1691    /// and
1692    /// <code>FixedU32::[unwrapped\_div\_euclid\_int][FixedU32::unwrapped_div_euclid_int]</code>.
1693    ///
1694    /// # Panics
1695    ///
1696    /// Panics if the divisor is zero or if the division results in overflow.
1697    ///
1698    /// # Examples
1699    ///
1700    /// ```rust
1701    /// use fixed::types::I16F16;
1702    /// use fixed::Unwrapped;
1703    /// let num = Unwrapped(I16F16::from_num(7.5));
1704    /// assert_eq!(num.div_euclid_int(2), Unwrapped(I16F16::from_num(3)));
1705    /// ```
1706    ///
1707    /// The following panics because of overflow.
1708    ///
1709    /// ```should_panic
1710    /// use fixed::types::I16F16;
1711    /// use fixed::Unwrapped;
1712    /// let min = Unwrapped(I16F16::MIN);
1713    /// let _overflow = min.div_euclid_int(-1);
1714    /// ```
1715    #[inline]
1716    #[track_caller]
1717    #[must_use = "this returns the result of the operation, without modifying the original"]
1718    pub fn div_euclid_int(self, divisor: F::Bits) -> Unwrapped<F> {
1719        Unwrapped(self.0.unwrapped_div_euclid_int(divisor))
1720    }
1721
1722    /// Remainder for Euclidean division.
1723    ///
1724    /// See also
1725    /// <code>FixedI32::[unwrapped\_rem\_euclid\_int][FixedI32::unwrapped_rem_euclid_int]</code>
1726    /// and
1727    /// <code>FixedU32::[unwrapped\_rem\_euclid\_int][FixedU32::unwrapped_rem_euclid_int]</code>.
1728    ///
1729    /// # Panics
1730    ///
1731    /// Panics if the divisor is zero.
1732    ///
1733    /// # Examples
1734    ///
1735    /// ```rust
1736    /// use fixed::types::I16F16;
1737    /// use fixed::Unwrapped;
1738    /// let num = Unwrapped(I16F16::from_num(7.5));
1739    /// assert_eq!(num.rem_euclid_int(2), Unwrapped(I16F16::from_num(1.5)));
1740    /// assert_eq!((-num).rem_euclid_int(2), Unwrapped(I16F16::from_num(0.5)));
1741    /// ```
1742    ///
1743    /// The following panics because of overflow.
1744    ///
1745    /// ```should_panic
1746    /// use fixed::types::I8F8;
1747    /// use fixed::Unwrapped;
1748    /// let num = Unwrapped(I8F8::from_num(-7.5));
1749    /// // -128 ≤ Fix < 128, so the answer 192.5 overflows
1750    /// let _overflow = num.rem_euclid_int(200);
1751    /// ```
1752    #[inline]
1753    #[track_caller]
1754    #[must_use = "this returns the result of the operation, without modifying the original"]
1755    pub fn rem_euclid_int(self, divisor: F::Bits) -> Unwrapped<F> {
1756        Unwrapped(self.0.unwrapped_rem_euclid_int(divisor))
1757    }
1758
1759    /// Unbounded shift left. Computes `self << rhs`, without bounding the value
1760    /// of `rhs`.
1761    ///
1762    /// See also
1763    /// <code>FixedI32::[unbounded\_shl][FixedI32::unbounded_shl]</code> and
1764    /// <code>FixedU32::[unbounded\_shl][FixedU32::unbounded_shl]</code>.
1765    ///
1766    /// # Examples
1767    ///
1768    /// ```rust
1769    /// use fixed::types::I16F16;
1770    /// use fixed::Unwrapped;
1771    /// type Unwr = Unwrapped<I16F16>;
1772    /// let num = Unwrapped(I16F16::from_num(1.5));
1773    /// assert_eq!(num.unbounded_shl(5), num << 5);
1774    /// assert_eq!(num.unbounded_shl(32), Unwr::ZERO);
1775    /// ```
1776    #[must_use = "this returns the result of the operation, without modifying the original"]
1777    #[inline]
1778    pub fn unbounded_shl(self, rhs: u32) -> Unwrapped<F> {
1779        Unwrapped(self.0.unbounded_shl(rhs))
1780    }
1781
1782    /// Unbounded shift right. Computes `self >> rhs`, without bounding the
1783    /// value of `rhs`.
1784    ///
1785    /// See also
1786    /// <code>FixedI32::[unbounded\_shr][FixedI32::unbounded_shr]</code> and
1787    /// <code>FixedU32::[unbounded\_shr][FixedU32::unbounded_shr]</code>.
1788    ///
1789    /// # Examples
1790    ///
1791    /// ```rust
1792    /// use fixed::types::I16F16;
1793    /// use fixed::Unwrapped;
1794    /// type Unwr = Unwrapped<I16F16>;
1795    /// let num = Unwrapped(I16F16::from_num(1.5));
1796    /// assert_eq!(num.unbounded_shr(5), num >> 5);
1797    /// assert_eq!(num.unbounded_shr(32), Unwr::ZERO);
1798    /// assert_eq!((-num).unbounded_shr(5), (-num) >> 5);
1799    /// assert_eq!((-num).unbounded_shr(32), -Unwr::DELTA);
1800    /// ```
1801    #[must_use = "this returns the result of the operation, without modifying the original"]
1802    #[inline]
1803    pub fn unbounded_shr(self, rhs: u32) -> Unwrapped<F> {
1804        Unwrapped(self.0.unbounded_shr(rhs))
1805    }
1806
1807    /// Linear interpolation between `start` and `end`.
1808    ///
1809    /// See also
1810    /// <code>FixedI32::[unwrapped\_lerp][FixedI32::unwrapped_lerp]</code> and
1811    /// <code>FixedU32::[unwrapped\_lerp][FixedU32::unwrapped_lerp]</code>.
1812    ///
1813    /// # Panics
1814    ///
1815    /// Panics on overflow.
1816    ///
1817    /// # Examples
1818    ///
1819    /// ```rust
1820    /// use fixed::types::I16F16;
1821    /// use fixed::Unwrapped;
1822    /// type Unwr = Unwrapped<I16F16>;
1823    /// assert_eq!(Unwr::from_num(0.5).lerp(Unwr::ZERO, Unwr::MAX), Unwr::MAX / 2);
1824    /// ```
1825    ///
1826    /// The following panics because of overflow.
1827    ///
1828    /// ```should_panic
1829    /// use fixed::types::I16F16;
1830    /// use fixed::Unwrapped;
1831    /// type Unwr = Unwrapped<I16F16>;
1832    /// let _overflow = Unwr::from_num(1.5).lerp(Unwr::ZERO, Unwr::MAX);
1833    /// ```
1834    #[inline]
1835    #[track_caller]
1836    #[must_use = "this returns the result of the operation, without modifying the original"]
1837    pub fn lerp(self, start: Unwrapped<F>, end: Unwrapped<F>) -> Unwrapped<F> {
1838        Unwrapped(self.0.unwrapped_lerp(start.0, end.0))
1839    }
1840
1841    /// Inverse linear interpolation between `start` and `end`.
1842    ///
1843    /// See also
1844    /// <code>FixedI32::[unwrapped\_inv\_lerp][FixedI32::unwrapped_inv_lerp]</code> and
1845    /// <code>FixedU32::[unwrapped\_inv\_lerp][FixedU32::unwrapped_inv_lerp]</code>.
1846    ///
1847    /// # Panics
1848    ///
1849    /// Panics when `start`&nbsp;=&nbsp;`end` or when the results overflows.
1850    ///
1851    /// # Examples
1852    ///
1853    /// ```rust
1854    /// use fixed::types::I16F16;
1855    /// use fixed::Unwrapped;
1856    /// type Unwr = Unwrapped<I16F16>;
1857    /// assert_eq!(
1858    ///     Unwr::from_num(25).inv_lerp(Unwr::from_num(20), Unwr::from_num(40)),
1859    ///     Unwr::from_num(0.25)
1860    /// );
1861    /// ```
1862    ///
1863    /// The following panics because `start`&nbsp;=&nbsp;`end`.
1864    ///
1865    /// ```should_panic
1866    /// use fixed::types::I16F16;
1867    /// use fixed::Unwrapped;
1868    /// type Unwr = Unwrapped<I16F16>;
1869    /// let two = Unwr::from_num(2);
1870    /// let _zero_range = two.inv_lerp(two, two);
1871    /// ```
1872    ///
1873    /// The following panics because of overflow.
1874    ///
1875    /// ```should_panic
1876    /// use fixed::types::I16F16;
1877    /// use fixed::Unwrapped;
1878    /// type Unwr = Unwrapped<I16F16>;
1879    /// let _overflow = Unwr::MAX.inv_lerp(Unwr::ZERO, Unwr::from_num(0.5));
1880    /// ```
1881    #[inline]
1882    #[track_caller]
1883    #[must_use]
1884    pub fn inv_lerp(self, start: Unwrapped<F>, end: Unwrapped<F>) -> Unwrapped<F> {
1885        Unwrapped(self.0.unwrapped_inv_lerp(start.0, end.0))
1886    }
1887
1888    /// Unwrapped round. Rounds to the next integer to the nearest, with ties
1889    /// rounded to even, and panics on overflow.
1890    ///
1891    /// # Panics
1892    ///
1893    /// Panics if the result does not fit.
1894    #[inline]
1895    #[track_caller]
1896    #[must_use]
1897    #[deprecated(since = "1.28.0", note = "renamed to `round_ties_even`")]
1898    pub fn round_ties_to_even(self) -> Unwrapped<F> {
1899        self.round_ties_even()
1900    }
1901}
1902
1903impl<F: FixedSigned> Unwrapped<F> {
1904    /// Returns the bit pattern of `self` reinterpreted as an unsigned
1905    /// fixed-point number of the same size.
1906    ///
1907    /// See also <code>FixedI32::[cast\_unsigned][FixedU32::cast_unsigned]</code>.
1908    ///
1909    /// # Examples
1910    ///
1911    /// ```rust
1912    /// use fixed::types::{I16F16, U16F16};
1913    /// use fixed::Unwrapped;
1914    ///
1915    /// let n = Unwrapped(-I16F16::DELTA);
1916    /// assert_eq!(n.cast_unsigned(), Unwrapped(U16F16::MAX));
1917    /// ```
1918    #[must_use]
1919    #[inline]
1920    pub fn cast_unsigned(self) -> Unwrapped<F::Unsigned> {
1921        Unwrapped(self.0.cast_unsigned())
1922    }
1923
1924    /// Returns the number of bits required to represent the value.
1925    ///
1926    /// The number of bits required includes an initial one for
1927    /// negative numbers, and an initial zero for non-negative
1928    /// numbers.
1929    ///
1930    /// See also <code>FixedI32::[signed\_bits][FixedI32::signed_bits]</code>.
1931    ///
1932    /// # Examples
1933    ///
1934    /// ```rust
1935    /// use fixed::types::I4F4;
1936    /// use fixed::Unwrapped;
1937    /// assert_eq!(Unwrapped(I4F4::from_num(-3)).signed_bits(), 7);      // “_101.0000”
1938    /// assert_eq!(Unwrapped(I4F4::from_num(-1)).signed_bits(), 5);      // “___1.0000”
1939    /// assert_eq!(Unwrapped(I4F4::from_num(-0.0625)).signed_bits(), 1); // “____.___1”
1940    /// assert_eq!(Unwrapped(I4F4::from_num(0)).signed_bits(), 1);       // “____.___0”
1941    /// assert_eq!(Unwrapped(I4F4::from_num(0.0625)).signed_bits(), 2);  // “____.__01”
1942    /// assert_eq!(Unwrapped(I4F4::from_num(1)).signed_bits(), 6);       // “__01.0000”
1943    /// assert_eq!(Unwrapped(I4F4::from_num(3)).signed_bits(), 7);       // “_011.0000”
1944    /// ```
1945    #[inline]
1946    pub fn signed_bits(self) -> u32 {
1947        self.0.signed_bits()
1948    }
1949
1950    /// Returns [`true`] if the number is >&nbsp;0.
1951    ///
1952    /// See also <code>FixedI32::[is\_positive][FixedI32::is_positive]</code>.
1953    ///
1954    /// # Examples
1955    ///
1956    /// ```rust
1957    /// use fixed::types::I16F16;
1958    /// use fixed::Unwrapped;
1959    /// assert!(Unwrapped(I16F16::from_num(4.3)).is_positive());
1960    /// assert!(!Unwrapped(I16F16::ZERO).is_positive());
1961    /// assert!(!Unwrapped(I16F16::from_num(-4.3)).is_positive());
1962    /// ```
1963    #[inline]
1964    pub fn is_positive(self) -> bool {
1965        self.0.is_positive()
1966    }
1967
1968    /// Returns [`true`] if the number is <&nbsp;0.
1969    ///
1970    /// See also <code>FixedI32::[is\_negative][FixedI32::is_negative]</code>.
1971    ///
1972    /// # Examples
1973    ///
1974    /// ```rust
1975    /// use fixed::types::I16F16;
1976    /// use fixed::Unwrapped;
1977    /// assert!(!Unwrapped(I16F16::from_num(4.3)).is_negative());
1978    /// assert!(!Unwrapped(I16F16::ZERO).is_negative());
1979    /// assert!(Unwrapped(I16F16::from_num(-4.3)).is_negative());
1980    /// ```
1981    #[inline]
1982    pub fn is_negative(self) -> bool {
1983        self.0.is_negative()
1984    }
1985
1986    /// Unwrapped absolute value. Returns the absolute value, panicking
1987    /// on overflow.
1988    ///
1989    /// Overflow can only occur when trying to find the absolute value
1990    /// of the minimum value.
1991    ///
1992    /// See also
1993    /// <code>FixedI32::[unwrapped\_abs][FixedI32::unwrapped_abs]</code>.
1994    ///
1995    /// # Panics
1996    ///
1997    /// Panics if the result does not fit.
1998    ///
1999    /// # Examples
2000    ///
2001    /// ```rust
2002    /// use fixed::types::I16F16;
2003    /// use fixed::Unwrapped;
2004    /// assert_eq!(Unwrapped(I16F16::from_num(-5)).abs(), Unwrapped(I16F16::from_num(5)));
2005    /// ```
2006    ///
2007    /// The following panics because of overflow.
2008    ///
2009    /// ```should_panic
2010    /// use fixed::types::I16F16;
2011    /// use fixed::Unwrapped;
2012    /// let _overflow = Unwrapped(I16F16::MIN).abs();
2013    /// ```
2014    #[inline]
2015    #[track_caller]
2016    #[must_use]
2017    pub fn abs(self) -> Unwrapped<F> {
2018        Unwrapped(self.0.unwrapped_abs())
2019    }
2020
2021    /// Returns a number representing the sign of `self`.
2022    ///
2023    /// See also
2024    /// <code>FixedI32::[unwrapped\_signum][FixedI32::unwrapped_signum]</code>.
2025    ///
2026    /// # Panics
2027    ///
2028    /// Panics
2029    ///   * if the value is positive and the fixed-point number has zero
2030    ///     or one integer bits such that it cannot hold the value 1.
2031    ///   * if the value is negative and the fixed-point number has zero
2032    ///     integer bits, such that it cannot hold the value &minus;1.
2033    ///
2034    /// # Examples
2035    ///
2036    /// ```rust
2037    /// use fixed::types::I16F16;
2038    /// use fixed::Unwrapped;
2039    /// assert_eq!(Unwrapped(<I16F16>::from_num(-3.9)).signum(), Unwrapped(I16F16::NEG_ONE));
2040    /// assert_eq!(Unwrapped(<I16F16>::ZERO).signum(), Unwrapped(I16F16::ZERO));
2041    /// assert_eq!(Unwrapped(<I16F16>::from_num(3.9)).signum(), Unwrapped(I16F16::ONE));
2042    /// ```
2043    ///
2044    /// The following panics because of overflow.
2045    ///
2046    /// ```should_panic
2047    /// use fixed::types::I1F31;
2048    /// use fixed::Unwrapped;
2049    /// let _overflow = Unwrapped(<I1F31>::from_num(0.5)).signum();
2050    /// ```
2051    #[inline]
2052    #[track_caller]
2053    #[must_use]
2054    pub fn signum(self) -> Unwrapped<F> {
2055        Unwrapped(self.0.unwrapped_signum())
2056    }
2057
2058    /// Addition with an unsigned fixed-point number.
2059    ///
2060    /// See also
2061    /// <code>FixedI32::[unwrapped\_add\_unsigned][FixedI32::unwrapped_add_unsigned]</code>.
2062    ///
2063    /// # Panics
2064    ///
2065    /// Panics if the result does not fit.
2066    ///
2067    /// # Examples
2068    ///
2069    /// ```rust
2070    /// use fixed::types::{I16F16, U16F16};
2071    /// use fixed::Unwrapped;
2072    /// assert_eq!(
2073    ///     Unwrapped::<I16F16>::from_num(-5).add_unsigned(U16F16::from_num(3)),
2074    ///     Unwrapped::<I16F16>::from_num(-2)
2075    /// );
2076    /// ```
2077    ///
2078    /// The following panics because of overflow.
2079    ///
2080    /// ```rust,should_panic
2081    /// use fixed::types::{I16F16, U16F16};
2082    /// use fixed::Unwrapped;
2083    /// let _overflow = Unwrapped::<I16F16>::ZERO.add_unsigned(U16F16::MAX);
2084    /// ```
2085    #[inline]
2086    #[track_caller]
2087    #[must_use]
2088    pub fn add_unsigned(self, rhs: F::Unsigned) -> Unwrapped<F> {
2089        Unwrapped(self.0.unwrapped_add_unsigned(rhs))
2090    }
2091
2092    /// Subtraction with an unsigned fixed-point number.
2093    ///
2094    /// See also
2095    /// <code>FixedI32::[unwrapped\_sub\_unsigned][FixedI32::unwrapped_sub_unsigned]</code>.
2096    ///
2097    /// # Panics
2098    ///
2099    /// Panics if the result does not fit.
2100    ///
2101    /// # Examples
2102    ///
2103    /// ```rust
2104    /// use fixed::types::{I16F16, U16F16};
2105    /// use fixed::Unwrapped;
2106    /// assert_eq!(
2107    ///     Unwrapped::<I16F16>::from_num(3).sub_unsigned(U16F16::from_num(5)),
2108    ///     Unwrapped::<I16F16>::from_num(-2)
2109    /// );
2110    /// ```
2111    ///
2112    /// The following panics because of overflow.
2113    ///
2114    /// ```rust,should_panic
2115    /// use fixed::types::{I16F16, U16F16};
2116    /// use fixed::Unwrapped;
2117    /// let _overflow = Unwrapped::<I16F16>::ZERO.sub_unsigned(U16F16::MAX);
2118    /// ```
2119    #[inline]
2120    #[track_caller]
2121    #[must_use]
2122    pub fn sub_unsigned(self, rhs: F::Unsigned) -> Unwrapped<F> {
2123        Unwrapped(self.0.unwrapped_sub_unsigned(rhs))
2124    }
2125}
2126
2127impl<F: FixedUnsigned> Unwrapped<F> {
2128    /// Returns the bit pattern of `self` reinterpreted as a signed fixed-point
2129    /// number of the same size.
2130    ///
2131    /// See also <code>FixedU32::[cast\_signed][FixedU32::cast_signed]</code>.
2132    ///
2133    /// # Examples
2134    ///
2135    /// ```rust
2136    /// use fixed::types::{I16F16, U16F16};
2137    /// use fixed::Unwrapped;
2138    ///
2139    /// let n = Unwrapped(U16F16::MAX);
2140    /// assert_eq!(n.cast_signed(), Unwrapped(-I16F16::DELTA));
2141    /// ```
2142    #[must_use]
2143    #[inline]
2144    pub fn cast_signed(self) -> Unwrapped<F::Signed> {
2145        Unwrapped(self.0.cast_signed())
2146    }
2147
2148    /// Returns the number of bits required to represent the value.
2149    ///
2150    /// See also
2151    /// <code>FixedU32::[significant\_bits][FixedU32::significant_bits]</code>.
2152    ///
2153    /// # Examples
2154    ///
2155    /// ```rust
2156    /// use fixed::types::U4F4;
2157    /// use fixed::Unwrapped;
2158    /// assert_eq!(Unwrapped(U4F4::from_num(0)).significant_bits(), 0);      // “____.____”
2159    /// assert_eq!(Unwrapped(U4F4::from_num(0.0625)).significant_bits(), 1); // “____.___1”
2160    /// assert_eq!(Unwrapped(U4F4::from_num(1)).significant_bits(), 5);      // “___1.0000”
2161    /// assert_eq!(Unwrapped(U4F4::from_num(3)).significant_bits(), 6);      // “__11.0000”
2162    /// ```
2163    #[inline]
2164    pub fn significant_bits(self) -> u32 {
2165        self.0.significant_bits()
2166    }
2167
2168    /// Returns [`true`] if the fixed-point number is
2169    /// 2<sup><i>k</i></sup> for some integer <i>k</i>.
2170    ///
2171    /// See also
2172    /// <code>FixedU32::[is\_power\_of\_two][FixedU32::is_power_of_two]</code>.
2173    ///
2174    /// # Examples
2175    ///
2176    /// ```rust
2177    /// use fixed::types::U16F16;
2178    /// use fixed::Unwrapped;
2179    /// assert!(Unwrapped(U16F16::from_num(0.5)).is_power_of_two());
2180    /// assert!(Unwrapped(U16F16::from_num(4)).is_power_of_two());
2181    /// assert!(!Unwrapped(U16F16::from_num(5)).is_power_of_two());
2182    /// ```
2183    #[inline]
2184    pub fn is_power_of_two(self) -> bool {
2185        self.0.is_power_of_two()
2186    }
2187
2188    /// Returns the highest one in the binary representation, or zero
2189    /// if `self` is zero.
2190    ///
2191    /// If `self`&nbsp;>&nbsp;0, the highest one is equal to the largest power
2192    /// of two that is ≤&nbsp;`self`.
2193    ///
2194    /// See also <code>FixedU32::[highest\_one][FixedU32::highest_one]</code>.
2195    ///
2196    /// # Examples
2197    ///
2198    /// ```rust
2199    /// use fixed::types::U16F16;
2200    /// use fixed::Unwrapped;
2201    /// type T = Unwrapped<U16F16>;
2202    /// assert_eq!(T::from_bits(0b11_0010).highest_one(), T::from_bits(0b10_0000));
2203    /// assert_eq!(T::from_num(0.3).highest_one(), T::from_num(0.25));
2204    /// assert_eq!(T::from_num(4).highest_one(), T::from_num(4));
2205    /// assert_eq!(T::from_num(6.5).highest_one(), T::from_num(4));
2206    /// assert_eq!(T::ZERO.highest_one(), T::ZERO);
2207    /// ```
2208    #[inline]
2209    #[must_use]
2210    pub fn highest_one(self) -> Unwrapped<F> {
2211        Unwrapped(self.0.highest_one())
2212    }
2213
2214    /// Returns the smallest power of two that is ≥&nbsp;`self`.
2215    ///
2216    /// See also
2217    /// <code>FixedU32::[unwrapped\_next\_power\_of\_two][FixedU32::unwrapped_next_power_of_two]</code>.
2218    ///
2219    /// # Panics
2220    ///
2221    /// Panics if the next power of two is too large to fit.
2222    ///
2223    /// # Examples
2224    ///
2225    /// ```rust
2226    /// use fixed::types::U16F16;
2227    /// use fixed::Unwrapped;
2228    /// type T = Unwrapped<U16F16>;
2229    /// assert_eq!(T::from_bits(0b11_0010).next_power_of_two(), T::from_bits(0b100_0000));
2230    /// assert_eq!(T::from_num(0.3).next_power_of_two(), T::from_num(0.5));
2231    /// assert_eq!(T::from_num(4).next_power_of_two(), T::from_num(4));
2232    /// assert_eq!(T::from_num(6.5).next_power_of_two(), T::from_num(8));
2233    /// ```
2234    ///
2235    /// The following panics because of overflow.
2236    ///
2237    /// ```should_panic
2238    /// use fixed::types::U16F16;
2239    /// use fixed::Unwrapped;
2240    /// let _overflow = Unwrapped(U16F16::MAX).next_power_of_two();
2241    /// ```
2242    #[inline]
2243    #[track_caller]
2244    #[must_use]
2245    pub fn next_power_of_two(self) -> Unwrapped<F> {
2246        Unwrapped(self.0.unwrapped_next_power_of_two())
2247    }
2248
2249    /// Addition with an signed fixed-point number.
2250    ///
2251    /// See also
2252    /// <code>FixedU32::[unwrapped\_add\_signed][FixedU32::unwrapped_add_signed]</code>.
2253    ///
2254    /// # Panics
2255    ///
2256    /// Panics if the result does not fit.
2257    ///
2258    /// # Examples
2259    ///
2260    /// ```rust
2261    /// use fixed::types::{I16F16, U16F16};
2262    /// use fixed::Unwrapped;
2263    /// assert_eq!(
2264    ///     Unwrapped::<U16F16>::from_num(5).add_signed(I16F16::from_num(-3)),
2265    ///     Unwrapped::<U16F16>::from_num(2)
2266    /// );
2267    /// ```
2268    ///
2269    /// The following panics because of overflow.
2270    ///
2271    /// ```rust,should_panic
2272    /// use fixed::types::{I16F16, U16F16};
2273    /// use fixed::Unwrapped;
2274    /// let _overflow = Unwrapped::<U16F16>::ZERO.add_signed(-I16F16::DELTA);
2275    /// ```
2276    #[inline]
2277    #[track_caller]
2278    #[must_use]
2279    pub fn add_signed(self, rhs: F::Signed) -> Unwrapped<F> {
2280        Unwrapped(self.0.unwrapped_add_signed(rhs))
2281    }
2282
2283    /// Subtraction with an signed fixed-point number.
2284    ///
2285    /// See also
2286    /// <code>FixedU32::[unwrapped\_sub\_signed][FixedU32::unwrapped_sub_signed]</code>.
2287    ///
2288    /// # Panics
2289    ///
2290    /// Panics if the result does not fit.
2291    ///
2292    /// # Examples
2293    ///
2294    /// ```rust
2295    /// use fixed::types::{I16F16, U16F16};
2296    /// use fixed::Unwrapped;
2297    /// assert_eq!(
2298    ///     Unwrapped::<U16F16>::from_num(5).sub_signed(I16F16::from_num(-3)),
2299    ///     Unwrapped::<U16F16>::from_num(8)
2300    /// );
2301    /// ```
2302    ///
2303    /// The following panics because of overflow.
2304    ///
2305    /// ```rust,should_panic
2306    /// use fixed::types::{I16F16, U16F16};
2307    /// use fixed::Unwrapped;
2308    /// let _overflow = Unwrapped::<U16F16>::ZERO.sub_signed(I16F16::DELTA);
2309    /// ```
2310    #[inline]
2311    #[track_caller]
2312    #[must_use]
2313    pub fn sub_signed(self, rhs: F::Signed) -> Unwrapped<F> {
2314        Unwrapped(self.0.unwrapped_sub_signed(rhs))
2315    }
2316}
2317
2318impl<F: Fixed> Display for Unwrapped<F> {
2319    #[inline]
2320    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2321        Display::fmt(&self.0, f)
2322    }
2323}
2324
2325impl<F: Fixed> Debug for Unwrapped<F> {
2326    #[inline]
2327    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2328        Debug::fmt(&self.0, f)
2329    }
2330}
2331
2332impl<F: Fixed> Binary for Unwrapped<F> {
2333    #[inline]
2334    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2335        Binary::fmt(&self.0, f)
2336    }
2337}
2338
2339impl<F: Fixed> Octal for Unwrapped<F> {
2340    #[inline]
2341    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2342        Octal::fmt(&self.0, f)
2343    }
2344}
2345
2346impl<F: Fixed> LowerHex for Unwrapped<F> {
2347    #[inline]
2348    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2349        LowerHex::fmt(&self.0, f)
2350    }
2351}
2352
2353impl<F: Fixed> UpperHex for Unwrapped<F> {
2354    #[inline]
2355    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2356        UpperHex::fmt(&self.0, f)
2357    }
2358}
2359
2360impl<F: Fixed> LowerExp for Unwrapped<F> {
2361    #[inline]
2362    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2363        LowerExp::fmt(&self.0, f)
2364    }
2365}
2366
2367impl<F: Fixed> UpperExp for Unwrapped<F> {
2368    #[inline]
2369    fn fmt(&self, f: &mut Formatter) -> FmtResult {
2370        UpperExp::fmt(&self.0, f)
2371    }
2372}
2373
2374impl<F: Fixed> From<F> for Unwrapped<F> {
2375    /// Wraps a fixed-point number.
2376    #[inline]
2377    fn from(src: F) -> Unwrapped<F> {
2378        Unwrapped(src)
2379    }
2380}
2381
2382impl<F: Fixed> FromStr for Unwrapped<F> {
2383    type Err = ParseFixedError;
2384    /// Parses a string slice containing decimal digits to return a fixed-point number.
2385    ///
2386    /// Rounding is to the nearest, with ties rounded to even.
2387    ///
2388    /// This method either returns [`Ok`] or panics, and never returns [`Err`].
2389    /// The inherent method
2390    /// <code>[Unwrapped]&lt;F>::[from\_str\_dec][Unwrapped::from_str_dec]</code>
2391    /// returns the value directly instead of a [`Result`].
2392    ///
2393    /// # Panics
2394    ///
2395    /// Panics if the value does not fit or if there is a parsing error.
2396    #[inline]
2397    #[track_caller]
2398    fn from_str(s: &str) -> Result<Self, Self::Err> {
2399        Ok(Unwrapped(F::unwrapped_from_str(s)))
2400    }
2401}
2402
2403macro_rules! op {
2404    ($unwrapped:ident, $Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
2405        impl<F: Fixed> $Op<Unwrapped<F>> for Unwrapped<F> {
2406            type Output = Unwrapped<F>;
2407            #[inline]
2408            #[track_caller]
2409            fn $op(self, other: Unwrapped<F>) -> Unwrapped<F> {
2410                Unwrapped((self.0).$unwrapped(other.0))
2411            }
2412        }
2413        impl<F: Fixed> $Op<Unwrapped<F>> for &Unwrapped<F> {
2414            type Output = Unwrapped<F>;
2415            #[inline]
2416            #[track_caller]
2417            fn $op(self, other: Unwrapped<F>) -> Unwrapped<F> {
2418                Unwrapped((self.0).$unwrapped(other.0))
2419            }
2420        }
2421        impl<F: Fixed> $Op<&Unwrapped<F>> for Unwrapped<F> {
2422            type Output = Unwrapped<F>;
2423            #[inline]
2424            #[track_caller]
2425            fn $op(self, other: &Unwrapped<F>) -> Unwrapped<F> {
2426                Unwrapped((self.0).$unwrapped(other.0))
2427            }
2428        }
2429        impl<F: Fixed> $Op<&Unwrapped<F>> for &Unwrapped<F> {
2430            type Output = Unwrapped<F>;
2431            #[inline]
2432            #[track_caller]
2433            fn $op(self, other: &Unwrapped<F>) -> Unwrapped<F> {
2434                Unwrapped((self.0).$unwrapped(other.0))
2435            }
2436        }
2437        impl<F: Fixed> $OpAssign<Unwrapped<F>> for Unwrapped<F> {
2438            #[inline]
2439            #[track_caller]
2440            fn $op_assign(&mut self, other: Unwrapped<F>) {
2441                self.0 = (self.0).$unwrapped(other.0);
2442            }
2443        }
2444        impl<F: Fixed> $OpAssign<&Unwrapped<F>> for Unwrapped<F> {
2445            #[inline]
2446            #[track_caller]
2447            fn $op_assign(&mut self, other: &Unwrapped<F>) {
2448                self.0 = (self.0).$unwrapped(other.0);
2449            }
2450        }
2451        impl<F: Fixed> $OpAssign<F> for Unwrapped<F> {
2452            #[inline]
2453            #[track_caller]
2454            fn $op_assign(&mut self, other: F) {
2455                self.0 = (self.0).$unwrapped(other);
2456            }
2457        }
2458        impl<F: Fixed> $OpAssign<&F> for Unwrapped<F> {
2459            #[inline]
2460            #[track_caller]
2461            fn $op_assign(&mut self, other: &F) {
2462                self.0 = (self.0).$unwrapped(*other);
2463            }
2464        }
2465    };
2466}
2467
2468macro_rules! op_bitwise {
2469    ($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
2470        impl<F> $Op<Unwrapped<F>> for Unwrapped<F>
2471        where
2472            F: $Op<F, Output = F>,
2473        {
2474            type Output = Unwrapped<F>;
2475            #[inline]
2476            fn $op(self, other: Unwrapped<F>) -> Unwrapped<F> {
2477                Unwrapped((self.0).$op(other.0))
2478            }
2479        }
2480        impl<F> $Op<Unwrapped<F>> for &Unwrapped<F>
2481        where
2482            for<'a> &'a F: $Op<F, Output = F>,
2483        {
2484            type Output = Unwrapped<F>;
2485            #[inline]
2486            fn $op(self, other: Unwrapped<F>) -> Unwrapped<F> {
2487                Unwrapped((self.0).$op(other.0))
2488            }
2489        }
2490        impl<F> $Op<&Unwrapped<F>> for Unwrapped<F>
2491        where
2492            for<'a> F: $Op<&'a F, Output = F>,
2493        {
2494            type Output = Unwrapped<F>;
2495            #[inline]
2496            fn $op(self, other: &Unwrapped<F>) -> Unwrapped<F> {
2497                Unwrapped((self.0).$op(&other.0))
2498            }
2499        }
2500        impl<F> $Op<&Unwrapped<F>> for &Unwrapped<F>
2501        where
2502            for<'a, 'b> &'a F: $Op<&'b F, Output = F>,
2503        {
2504            type Output = Unwrapped<F>;
2505            #[inline]
2506            fn $op(self, other: &Unwrapped<F>) -> Unwrapped<F> {
2507                Unwrapped((self.0).$op(&other.0))
2508            }
2509        }
2510        impl<F> $OpAssign<Unwrapped<F>> for Unwrapped<F>
2511        where
2512            F: $OpAssign<F>,
2513        {
2514            #[inline]
2515            fn $op_assign(&mut self, other: Unwrapped<F>) {
2516                (self.0).$op_assign(other.0);
2517            }
2518        }
2519        impl<F> $OpAssign<&Unwrapped<F>> for Unwrapped<F>
2520        where
2521            for<'a> F: $OpAssign<&'a F>,
2522        {
2523            #[inline]
2524            fn $op_assign(&mut self, other: &Unwrapped<F>) {
2525                (self.0).$op_assign(&other.0);
2526            }
2527        }
2528        impl<F> $OpAssign<F> for Unwrapped<F>
2529        where
2530            F: $OpAssign<F>,
2531        {
2532            #[inline]
2533            fn $op_assign(&mut self, other: F) {
2534                (self.0).$op_assign(other);
2535            }
2536        }
2537        impl<F> $OpAssign<&F> for Unwrapped<F>
2538        where
2539            for<'a> F: $OpAssign<&'a F>,
2540        {
2541            #[inline]
2542            fn $op_assign(&mut self, other: &F) {
2543                (self.0).$op_assign(other);
2544            }
2545        }
2546    };
2547}
2548
2549macro_rules! op_shift {
2550    (
2551        $Op:ident $op:ident, $OpAssign:ident $op_assign:ident;
2552        $($Rhs:ident),*
2553    ) => { $(
2554        impl<F> $Op<$Rhs> for Unwrapped<F>
2555        where
2556            F: $Op<u32, Output = F>,
2557        {
2558            type Output = Unwrapped<F>;
2559            #[inline]
2560            #[track_caller]
2561            fn $op(self, other: $Rhs) -> Unwrapped<F> {
2562                let nbits = size_of::<F>() as u32 * 8;
2563                let checked = other as u32 % nbits;
2564                assert!(checked as $Rhs == other, "overflow");
2565                Unwrapped((self.0).$op(checked))
2566            }
2567        }
2568        impl<F> $Op<$Rhs> for &Unwrapped<F>
2569        where
2570            for<'a> &'a F: $Op<u32, Output = F>,
2571        {
2572            type Output = Unwrapped<F>;
2573            #[inline]
2574            #[track_caller]
2575            fn $op(self, other: $Rhs) -> Unwrapped<F> {
2576                let nbits = size_of::<F>() as u32 * 8;
2577                let checked = other as u32 % nbits;
2578                assert!(checked as $Rhs == other, "overflow");
2579                Unwrapped((self.0).$op(checked))
2580            }
2581        }
2582        impl<F> $Op<&$Rhs> for Unwrapped<F>
2583        where
2584            F: $Op<u32, Output = F>,
2585        {
2586            type Output = Unwrapped<F>;
2587            #[inline]
2588            #[track_caller]
2589            fn $op(self, other: &$Rhs) -> Unwrapped<F> {
2590                let nbits = size_of::<F>() as u32 * 8;
2591                let checked = *other as u32 % nbits;
2592                assert!(checked as $Rhs == *other, "overflow");
2593                Unwrapped((self.0).$op(checked))
2594            }
2595        }
2596        impl<F> $Op<&$Rhs> for &Unwrapped<F>
2597        where
2598            for<'a> &'a F: $Op<u32, Output = F>,
2599        {
2600            type Output = Unwrapped<F>;
2601            #[inline]
2602            #[track_caller]
2603            fn $op(self, other: &$Rhs) -> Unwrapped<F> {
2604                let nbits = size_of::<F>() as u32 * 8;
2605                let checked = *other as u32 % nbits;
2606                assert!(checked as $Rhs == *other, "overflow");
2607                Unwrapped((self.0).$op(checked))
2608            }
2609        }
2610        impl<F> $OpAssign<$Rhs> for Unwrapped<F>
2611        where
2612            F: $OpAssign<u32>,
2613        {
2614            #[inline]
2615            #[track_caller]
2616            fn $op_assign(&mut self, other: $Rhs) {
2617                let nbits = size_of::<F>() as u32 * 8;
2618                let checked = other as u32 % nbits;
2619                assert!(checked as $Rhs == other, "overflow");
2620                (self.0).$op_assign(checked);
2621            }
2622        }
2623        impl<F> $OpAssign<&$Rhs> for Unwrapped<F>
2624        where
2625            F: $OpAssign<u32>,
2626        {
2627            #[inline]
2628            #[track_caller]
2629            fn $op_assign(&mut self, other: &$Rhs) {
2630                let nbits = size_of::<F>() as u32 * 8;
2631                let checked = *other as u32 % nbits;
2632                assert!(checked as $Rhs == *other, "overflow");
2633                (self.0).$op_assign(checked);
2634            }
2635        }
2636    )* };
2637}
2638
2639impl<F: Fixed> Neg for Unwrapped<F> {
2640    type Output = Unwrapped<F>;
2641    #[inline]
2642    #[track_caller]
2643    fn neg(self) -> Unwrapped<F> {
2644        Unwrapped((self.0).unwrapped_neg())
2645    }
2646}
2647
2648impl<F: Fixed> Neg for &Unwrapped<F> {
2649    type Output = Unwrapped<F>;
2650    #[inline]
2651    #[track_caller]
2652    fn neg(self) -> Unwrapped<F> {
2653        Unwrapped((self.0).unwrapped_neg())
2654    }
2655}
2656op! { unwrapped_add, Add add, AddAssign add_assign }
2657op! { unwrapped_sub, Sub sub, SubAssign sub_assign }
2658op! { unwrapped_mul, Mul mul, MulAssign mul_assign }
2659op! { unwrapped_div, Div div, DivAssign div_assign }
2660op! { unwrapped_rem, Rem rem, RemAssign rem_assign }
2661
2662impl<F> Not for Unwrapped<F>
2663where
2664    F: Not<Output = F>,
2665{
2666    type Output = Unwrapped<F>;
2667    #[inline]
2668    fn not(self) -> Unwrapped<F> {
2669        Unwrapped((self.0).not())
2670    }
2671}
2672impl<F> Not for &Unwrapped<F>
2673where
2674    for<'a> &'a F: Not<Output = F>,
2675{
2676    type Output = Unwrapped<F>;
2677    #[inline]
2678    fn not(self) -> Unwrapped<F> {
2679        Unwrapped((self.0).not())
2680    }
2681}
2682op_bitwise! { BitAnd bitand, BitAndAssign bitand_assign }
2683op_bitwise! { BitOr bitor, BitOrAssign bitor_assign }
2684op_bitwise! { BitXor bitxor, BitXorAssign bitxor_assign }
2685
2686op_shift! {
2687    Shl shl, ShlAssign shl_assign;
2688    i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
2689}
2690op_shift! {
2691    Shr shr, ShrAssign shr_assign;
2692    i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
2693}
2694
2695impl<F: Fixed> Sum<Unwrapped<F>> for Unwrapped<F> {
2696    #[track_caller]
2697    fn sum<I>(iter: I) -> Unwrapped<F>
2698    where
2699        I: Iterator<Item = Unwrapped<F>>,
2700    {
2701        iter.fold(Unwrapped(F::ZERO), Add::add)
2702    }
2703}
2704
2705impl<'a, F: 'a + Fixed> Sum<&'a Unwrapped<F>> for Unwrapped<F> {
2706    #[track_caller]
2707    fn sum<I>(iter: I) -> Unwrapped<F>
2708    where
2709        I: Iterator<Item = &'a Unwrapped<F>>,
2710    {
2711        iter.fold(Unwrapped(F::ZERO), Add::add)
2712    }
2713}
2714
2715impl<F: Fixed> Product<Unwrapped<F>> for Unwrapped<F> {
2716    #[track_caller]
2717    fn product<I>(mut iter: I) -> Unwrapped<F>
2718    where
2719        I: Iterator<Item = Unwrapped<F>>,
2720    {
2721        match iter.next() {
2722            None => match 1.overflowing_to_fixed() {
2723                (_, true) => panic!("overflow"),
2724                (ans, false) => Unwrapped(ans),
2725            },
2726            Some(first) => iter.fold(first, Mul::mul),
2727        }
2728    }
2729}
2730
2731impl<'a, F: 'a + Fixed> Product<&'a Unwrapped<F>> for Unwrapped<F> {
2732    #[track_caller]
2733    fn product<I>(mut iter: I) -> Unwrapped<F>
2734    where
2735        I: Iterator<Item = &'a Unwrapped<F>>,
2736    {
2737        match iter.next() {
2738            None => match 1.overflowing_to_fixed() {
2739                (_, true) => panic!("overflow"),
2740                (ans, false) => Unwrapped(ans),
2741            },
2742            Some(first) => iter.fold(*first, Mul::mul),
2743        }
2744    }
2745}
2746
2747// The following cannot be implemented for Unwrapped<F> where F: Fixed,
2748// otherwise there will be a conflicting implementation error. For
2749// example we cannot implement both these without triggering E0119:
2750//
2751//     impl<F: Fixed> Op<F::Bits> for Unwrapped<F> { /* ... */ }
2752//     impl<F: Fixed> Op<&F::Bits> for Unwrapped<F> { /* ... */ }
2753//
2754// To work around this, we provide implementations like this:
2755//
2756//     impl<Frac> Op<i8> for Unwrapped<FixedI8<Frac>> { /* ... */ }
2757//     impl<Frac> Op<&i8> for Unwrapped<FixedI8<Frac>> { /* ... */ }
2758//     impl<Frac> Op<i16> for Unwrapped<FixedI16<Frac>> { /* ... */ }
2759//     impl<Frac> Op<&i16> for Unwrapped<FixedI16<Frac>> { /* ... */ }
2760//     ...
2761
2762macro_rules! op_bits {
2763    (
2764        $Fixed:ident($Bits:ident $(, $LeEqU:ident)*)::$unwrapped:ident,
2765        $Op:ident $op:ident,
2766        $OpAssign:ident $op_assign:ident
2767    ) => {
2768        impl<Frac $(: $LeEqU)*> $Op<$Bits> for Unwrapped<$Fixed<Frac>> {
2769            type Output = Unwrapped<$Fixed<Frac>>;
2770            #[inline]
2771            #[track_caller]
2772            fn $op(self, other: $Bits) -> Unwrapped<$Fixed<Frac>> {
2773                Unwrapped((self.0).$unwrapped(other))
2774            }
2775        }
2776        impl<Frac $(: $LeEqU)*> $Op<$Bits> for &Unwrapped<$Fixed<Frac>> {
2777            type Output = Unwrapped<$Fixed<Frac>>;
2778            #[inline]
2779            #[track_caller]
2780            fn $op(self, other: $Bits) -> Unwrapped<$Fixed<Frac>> {
2781                Unwrapped((self.0).$unwrapped(other))
2782            }
2783        }
2784        impl<Frac $(: $LeEqU)*> $Op<&$Bits> for Unwrapped<$Fixed<Frac>> {
2785            type Output = Unwrapped<$Fixed<Frac>>;
2786            #[inline]
2787            #[track_caller]
2788            fn $op(self, other: &$Bits) -> Unwrapped<$Fixed<Frac>> {
2789                Unwrapped((self.0).$unwrapped(*other))
2790            }
2791        }
2792        impl<Frac $(: $LeEqU)*> $Op<&$Bits> for &Unwrapped<$Fixed<Frac>> {
2793            type Output = Unwrapped<$Fixed<Frac>>;
2794            #[inline]
2795            #[track_caller]
2796            fn $op(self, other: &$Bits) -> Unwrapped<$Fixed<Frac>> {
2797                Unwrapped((self.0).$unwrapped(*other))
2798            }
2799        }
2800        impl<Frac $(: $LeEqU)*> $OpAssign<$Bits> for Unwrapped<$Fixed<Frac>> {
2801            #[inline]
2802            #[track_caller]
2803            fn $op_assign(&mut self, other: $Bits) {
2804                self.0 = (self.0).$unwrapped(other);
2805            }
2806        }
2807        impl<Frac $(: $LeEqU)*> $OpAssign<&$Bits> for Unwrapped<$Fixed<Frac>> {
2808            #[inline]
2809            #[track_caller]
2810            fn $op_assign(&mut self, other: &$Bits) {
2811                self.0 = (self.0).$unwrapped(*other);
2812            }
2813        }
2814    };
2815}
2816
2817macro_rules! ops {
2818    ($Fixed:ident($Bits:ident, $LeEqU:ident)) => {
2819        op_bits! { $Fixed($Bits)::unwrapped_mul_int, Mul mul, MulAssign mul_assign }
2820        op_bits! { $Fixed($Bits)::unwrapped_div_int, Div div, DivAssign div_assign }
2821        op_bits! { $Fixed($Bits, $LeEqU)::unwrapped_rem_int, Rem rem, RemAssign rem_assign }
2822    };
2823}
2824ops! { FixedI8(i8, LeEqU8) }
2825ops! { FixedI16(i16, LeEqU16) }
2826ops! { FixedI32(i32, LeEqU32) }
2827ops! { FixedI64(i64, LeEqU64) }
2828ops! { FixedI128(i128, LeEqU128) }
2829ops! { FixedU8(u8, LeEqU8) }
2830ops! { FixedU16(u16, LeEqU16) }
2831ops! { FixedU32(u32, LeEqU32) }
2832ops! { FixedU64(u64, LeEqU64) }
2833ops! { FixedU128(u128, LeEqU128) }