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