1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// |T|DDDDDDD|RRRRRRR|SMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM|
// T: Type Extension Flag (0 For Typed Math numbers)
// D: Domain [0, 127]
// R: Range [-64, 63]
// S: mantissa Sign bit
// M: Mantissa [-2^48, 2^48 - 1]
// Credit: Josh Cole, who implemented this for Eve v0.4
// Adapted and Extended for Mech by Corey Montella

extern crate num;
use self::num::Float;

const EXTENSION_MASK:u64 = 1 << 63;
const MANTISSA_MASK:u64 = (((1 as u64) << 49) as u64 - 1); // 49 bits at the end
const META_MASK:u64 = ((1 << 15) as u64 - 1) << 49; // 15 1s at the front
const OVERFLOW_MASK:u64 = ((1 << 16) as u64 - 1) << 48; // 15 1s at the front
const RANGE_MASK:u64 = ((1 << 7) as u64 - 1) << 49;
const SHIFTED_RANGE_DOMAIN_MASK:u64 = ((1 << 7) as u64 - 1);
const SHIFTED_FILL:u64 = ((((1 as u64) << 57) as u64 - 1) << 7);
const SIGN_MASK:u64 = 1 << 48;

pub type Quantity = u64;

pub trait ToQuantity {
    fn to_quantity(&self) -> u64;
}

pub trait FromQuantity<T> {
    fn get_value(self) -> T;
}

impl ToQuantity for u32 {
    #[inline(always)]
    fn to_quantity(&self) -> u64 {
        let result:u64 = (*self).into();
        result | (1 << 63)
    }
}

impl ToQuantity for i32 {
    #[inline(always)]
    fn to_quantity(&self) -> u64 {
        let me = *self;
        if me.is_negative() {
            me as u64 & MANTISSA_MASK | EXTENSION_MASK
        } else {
            me as u64 | EXTENSION_MASK
        }
    }
}

impl ToQuantity for u64 {
    #[inline(always)]
    fn to_quantity(&self) -> u64 {
        let me = *self;
        if me & META_MASK != 0 {
            let (mantissa, range) = overflow_handler(me);
            (mantissa as u64) & MANTISSA_MASK | shifted_range(range) |  EXTENSION_MASK
        } else {
            me & MANTISSA_MASK | EXTENSION_MASK
        }
    }
}

impl ToQuantity for i64 {
    #[inline(always)]
    fn to_quantity(&self) -> u64 {
        let me = *self;
        if me.is_negative() {
            if (me as u64) & META_MASK != META_MASK {
                let (mantissa, range) = overflow_handler(me.abs() as u64);
                !(mantissa - 1) & MANTISSA_MASK | shifted_range(range) |  EXTENSION_MASK
            } else {
                (me as u64) & MANTISSA_MASK | EXTENSION_MASK
            }
        } else if (me as u64) & OVERFLOW_MASK != 0 {
            let (mantissa, range) = overflow_handler(me as u64);
            (mantissa as u64) & MANTISSA_MASK | shifted_range(range) |  EXTENSION_MASK
        } else {
            (me as u64) & MANTISSA_MASK | EXTENSION_MASK
        }
    }
}

impl ToQuantity for f64 {
  #[inline(always)]
  fn to_quantity(&self) -> u64 {
    let me = *self;
    let (mantissa, exponent, sign) = Float::integer_decode(me);
    if mantissa == 0 {
      let result = make_quantity(0,0,0);
      result
    } else {
      let exp_log = 2f64.powi(exponent as i32).log10();
      let real_exponent = exp_log.floor() as i64 + 1;
      let real_mantissa = (((mantissa as f64) * 10f64.powf(exp_log.fract()))) as i64;
      let mut result = real_mantissa.to_quantity();
      if sign < 0 {
          result = result.negate();
      }
      let cur = result.range();
      result.set_range(cur + real_exponent);
      result
    }
  }
}


#[inline(always)]
pub fn overflow_handler(me:u64) -> (u64, u64) {
    let hi = 64 - me.leading_zeros() - 48;
    let r = (2u64.pow(hi) as f64).log10().ceil() as u32;
    let result = me / 10u64.pow(r) as u64;
    (result, r as u64)
}

pub fn decrease_range(mantissa:i64, range_delta:u64) -> (i64, u64) {
    let remaining_space = mantissa.leading_zeros();
    let thing:u64 = (1 as u64) << remaining_space;
    let remaining_10 = (thing as f64).log10().floor() as u64;
    if range_delta <= remaining_10 {
        let new_mantissa = mantissa * 10u64.pow(range_delta as u32) as i64;
        (new_mantissa, range_delta)
    } else {
        let new_mantissa = mantissa * 10u64.pow(remaining_10 as u32) as i64;
        (new_mantissa, remaining_10)
    }
}

pub fn increase_range(mantissa:i64, range_delta:u64) -> (i64, bool) {
    let range = 10u64.pow(range_delta as u32) as i64;
    (mantissa / range, mantissa % range != 0)
}

#[inline(always)]
pub fn shifted_range(range:u64) -> u64 {
    range << 49
}

pub fn make_quantity(mantissa:i64, range:i64, domain:u64) -> Quantity {
    let value = mantissa.to_quantity();
    let cur_range = (value.range() + range) as u64;
    value & !RANGE_MASK | ((cur_range << 49) & RANGE_MASK) | (domain << 56)
}

pub trait QuantityMath {
    fn is_number(self) -> bool;
    fn is_other(self) -> bool;
    fn domain(self) -> u64;
    fn range(self) -> i64;
    fn set_range(&mut self, range:i64);
    fn mantissa(self) -> i64;
    fn is_negative(self) -> bool;
    fn negate(self) -> Quantity;
    fn add(self, Quantity) -> Quantity;
    fn sub(self, Quantity) -> Quantity;
    fn multiply(self, Quantity) -> Quantity;
    fn divide(self, Quantity) -> Quantity;
    fn less_than(self, Quantity) -> bool;
    fn greater_than(self, Quantity) -> bool;
    fn to_string(self) -> String;
    fn to_float(self) -> f64;
    fn to_u64(self) -> u64;
}

impl QuantityMath for Quantity {
    #[inline(always)]
    fn is_number(self) -> bool {
        self & EXTENSION_MASK == EXTENSION_MASK
    }

    #[inline(always)]
    fn is_other(self) -> bool {
        self & EXTENSION_MASK == 0
    }

    #[inline(always)]
    fn domain(self) -> u64 {
        (self >> 56) & SHIFTED_RANGE_DOMAIN_MASK
    }

    #[inline(always)]
    fn range(self) -> i64 {
        let range = (self >> 49) & SHIFTED_RANGE_DOMAIN_MASK;
        if range & (1 << 6) == 0 {
            range as i64
        } else {
            (range | SHIFTED_FILL) as i64
        }
    }

    fn set_range(&mut self, range:i64) {
        let range_fill = ((range << 49) as u64) & RANGE_MASK;
        *self &= !RANGE_MASK;
        *self |= range_fill;
    }

    #[inline(always)]
    fn mantissa(self) -> i64 {
        if self & SIGN_MASK == SIGN_MASK {
            let a = self & MANTISSA_MASK;
            (a as i64) | (META_MASK as i64)
        } else {
            (self & MANTISSA_MASK) as i64
        }
    }

    fn negate(self) -> Quantity {
        let value = ((self.mantissa() * -1) as u64 & MANTISSA_MASK) as u64;
        self & META_MASK | value
    }

    #[inline(always)]
    fn is_negative(self) -> bool {
        (self & SIGN_MASK) == SIGN_MASK
    }

    fn to_string(self) -> String {
        format!("{}e{}", self.mantissa(), self.range())
    }

    fn to_float(self) -> f64 {
        (self.mantissa() as f64) * 10f64.powi(self.range() as i32)
    }

    fn to_u64(self) -> u64 {
        self.to_float() as u64
    }

    #[inline(always)]
    fn add(self, other:Quantity) -> Quantity {
        let my_range = self.range();
        let other_range = other.range();
        if self.mantissa() == 0 {
            return other
        }
        if my_range == other_range {
            let add = self.mantissa() + other.mantissa();
            let mut add_quantity = add.to_quantity();
            add_quantity.set_range(add_quantity.range() + my_range);
            add_quantity
        } else {
            let my_mant = self.mantissa();
            let other_mant = other.mantissa();
            let (a_range, b_range, a_mant, b_mant) = if my_range > other_range {
                (my_range, other_range, my_mant, other_mant)
            } else {
                (other_range, my_range, other_mant, my_mant)
            };
            // A is so much bigger than b, we just take a
            if a_range - b_range > 15 {
                return make_quantity(a_mant,a_range,0)
            }
            let range_delta = (a_range - b_range) as u64;
            let sign = if a_mant < 0 {
                -1
            } else {
                1
            };
            let (new_mantissa, actual_delta) = decrease_range(a_mant * sign, range_delta);
            if actual_delta == range_delta {
                let added = sign * new_mantissa + b_mant;
                let mut added_quantity = added.to_quantity();
                added_quantity.set_range(b_range + added_quantity.range());
                added_quantity
            } else {
                let (b_neue, _) = increase_range(b_mant, actual_delta);
                let mut added = (new_mantissa + b_neue).to_quantity();
                added.set_range(a_range - actual_delta as i64);
                added
            }
        }
    }

    fn sub(self, other:Quantity) -> Quantity {
        self.add(other.negate())
    }

    fn multiply(self, other:Quantity) -> Quantity {
        let result = match self.mantissa().checked_mul(other.mantissa()) {
           Some(result) => { result },
           None => { panic!("QuantityMultiply overflow") }
        };
        let mut quantity = result.to_quantity();
        quantity.set_range(quantity.range() + self.range() + other.range());
        quantity
    }

    fn divide(self, other:Quantity) -> Quantity {
        let result = self.mantissa() * 10000 / other.mantissa();
        make_quantity(result, -4, 0)
    }

    fn less_than(self, other: Quantity) -> bool {
        if self.is_negative() && !other.is_negative() {
            true
        } else if !self.is_negative() && other.is_negative() {
            false
        } else {
            self.to_float() < other.to_float()
        }
    }

    fn greater_than(self, other: Quantity) -> bool {
        if self.is_negative() && !other.is_negative() {
            false
        } else if !self.is_negative() && other.is_negative() {
            true
        } else {
            self.to_float() > other.to_float()
        }
    }
}