rust/fpnum/src/lib.rs
changeset 14632 2e2b31cf0871
parent 14212 bb2f301d4fe0
child 14669 def1b9870078
equal deleted inserted replaced
14631:ab6be7d200b0 14632:2e2b31cf0871
     1 use std::{
     1 use std::{cmp, ops, ops::Shl};
     2     cmp,
       
     3     ops,
       
     4     ops::Shl
       
     5 };
       
     6 
     2 
     7 #[derive(Clone, Debug, Copy)]
     3 #[derive(Clone, Debug, Copy)]
     8 pub struct FPNum {
     4 pub struct FPNum {
     9     is_negative: bool,
     5     is_negative: bool,
    10     value: u64,
     6     value: u64,
    24             1
    20             1
    25         }
    21         }
    26     }
    22     }
    27 
    23 
    28     #[inline]
    24     #[inline]
    29     pub fn is_negative(&self) -> bool {
    25     pub const fn is_negative(&self) -> bool {
    30         self.is_negative
    26         self.is_negative
    31     }
    27     }
    32 
    28 
    33     #[inline]
    29     #[inline]
    34     pub fn is_positive(&self) -> bool {
    30     pub const fn is_positive(&self) -> bool {
    35         !self.is_negative
    31         !self.is_negative
    36     }
    32     }
    37 
    33 
    38     #[inline]
    34     #[inline]
    39     pub fn is_zero(&self) -> bool {
    35     pub const fn is_zero(&self) -> bool {
    40         self.value == 0
    36         self.value == 0
    41     }
    37     }
    42 
    38 
    43     #[inline]
    39     #[inline]
    44     pub fn abs(&self) -> Self {
    40     pub const fn abs(&self) -> Self {
    45         Self {
    41         Self {
    46             is_negative: false,
    42             is_negative: false,
    47             value: self.value,
    43             value: self.value,
    48         }
    44         }
    49     }
    45     }
    56             (self.value >> 32) as i32
    52             (self.value >> 32) as i32
    57         }
    53         }
    58     }
    54     }
    59 
    55 
    60     #[inline]
    56     #[inline]
    61     pub fn abs_round(&self) -> u32 {
    57     pub const fn abs_round(&self) -> u32 {
    62         (self.value >> 32) as u32
    58         (self.value >> 32) as u32
    63     }
    59     }
    64 
    60 
    65     #[inline]
    61     #[inline]
    66     pub fn sqr(&self) -> Self {
    62     pub fn sqr(&self) -> Self {
    93             value: r << 16,
    89             value: r << 16,
    94         }
    90         }
    95     }
    91     }
    96 
    92 
    97     #[inline]
    93     #[inline]
    98     pub fn with_sign(&self, is_negative: bool) -> FPNum {
    94     pub const fn with_sign(&self, is_negative: bool) -> FPNum {
    99         FPNum {
    95         FPNum {
   100             is_negative,
    96             is_negative,
   101             ..*self
    97             ..*self
   102         }
    98         }
   103     }
    99     }
   104 
   100 
   105     #[inline]
   101     #[inline]
   106     pub fn with_sign_as(self, other: FPNum) -> FPNum {
   102     pub const fn with_sign_as(self, other: FPNum) -> FPNum {
   107         self.with_sign(other.is_negative)
   103         self.with_sign(other.is_negative)
   108     }
   104     }
   109 
   105 
   110     #[inline]
   106     #[inline]
   111     pub fn point(self) -> FPPoint {
   107     pub const fn point(self) -> FPPoint {
   112         FPPoint::new(self, self)
   108         FPPoint::new(self, self)
   113     }
   109     }
   114 }
   110 }
   115 
   111 
   116 impl From<i32> for FPNum {
   112 impl From<i32> for FPNum {
   323     y_value: u64,
   319     y_value: u64,
   324 }
   320 }
   325 
   321 
   326 impl FPPoint {
   322 impl FPPoint {
   327     #[inline]
   323     #[inline]
   328     pub fn new(x: FPNum, y: FPNum) -> Self {
   324     pub const fn new(x: FPNum, y: FPNum) -> Self {
   329         Self {
   325         Self {
   330             x_is_negative: x.is_negative,
   326             x_is_negative: x.is_negative,
   331             y_is_negative: y.is_negative,
   327             y_is_negative: y.is_negative,
   332             x_value: x.value,
   328             x_value: x.value,
   333             y_value: y.value,
   329             y_value: y.value,
   348     pub fn unit_y() -> FPPoint {
   344     pub fn unit_y() -> FPPoint {
   349         FPPoint::new(fp!(0), fp!(1))
   345         FPPoint::new(fp!(0), fp!(1))
   350     }
   346     }
   351 
   347 
   352     #[inline]
   348     #[inline]
   353     pub fn x(&self) -> FPNum {
   349     pub const fn x(&self) -> FPNum {
   354         FPNum {
   350         FPNum {
   355             is_negative: self.x_is_negative,
   351             is_negative: self.x_is_negative,
   356             value: self.x_value,
   352             value: self.x_value,
   357         }
   353         }
   358     }
   354     }
   359 
   355 
   360     #[inline]
   356     #[inline]
   361     pub fn y(&self) -> FPNum {
   357     pub const fn y(&self) -> FPNum {
   362         FPNum {
   358         FPNum {
   363             is_negative: self.y_is_negative,
   359             is_negative: self.y_is_negative,
   364             value: self.y_value,
   360             value: self.y_value,
   365         }
   361         }
   366     }
   362     }