      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

    // Copyright 2015 The Ramp Developers
    //
    //    Licensed under the Apache License, Version 2.0 (the "License");
    //    you may not use this file except in compliance with the License.
    //    You may obtain a copy of the License at
    //
    //        http://www.apache.org/licenses/LICENSE-2.0
    //
    //    Unless required by applicable law or agreed to in writing, software
    //    distributed under the License is distributed on an "AS IS" BASIS,
    //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    //    See the License for the specific language governing permissions and
    //    limitations under the License.

    #![allow(improper_ctypes)]

    use ll::limb::Limb;
    use ll::limb_ptr::{Limbs, LimbsMut};
    use super::{copy_rest, same_or_separate};

    #[allow(dead_code)]
    unsafe fn add_n_generic(mut wp: LimbsMut, mut xp: Limbs, mut yp: Limbs,
                            mut n: i32) -> Limb {

        let mut carry = Limb(0);

        loop {
            let xl = *xp;
            let yl = *yp;

            let (sl, c1) = xl.add_overflow(yl);
            let (rl, c2) = sl.add_overflow(carry);

            carry = if c1 || c2 { Limb(1) } else { Limb(0) };
            *wp = rl;

            n -= 1;
            if n == 0 { break; }

            wp = wp.offset(1);
            xp = xp.offset(1);
            yp = yp.offset(1);

        }

        carry
    }

    /**
     * Adds the `n` least signficant limbs of `xp` and `yp`, storing the result in {wp, n}.
     * If there was a carry, it is returned.
     */
    #[inline]
    #[cfg(asm)]
    pub unsafe fn add_n(mut wp: LimbsMut, xp: Limbs, yp: Limbs,
                        n: i32) -> Limb {
        #[cfg(target_arch="x86_64")]
        extern "C" { fn ramp_add_n(wp: *mut Limb, xp: *const Limb, yp: *const Limb,
                                   n: i32) -> Limb; }

        debug_assert!(n >= 1);
        debug_assert!(same_or_separate(wp, n, xp, n));
        debug_assert!(same_or_separate(wp, n, yp, n));

        return ramp_add_n(&mut *wp, &*xp, &*yp, n);
    }

    /**
     * Adds the `n` least signficant limbs of `xp` and `yp`, storing the result in {wp, n}.
     * If there was a carry, it is returned.
     */
    #[cfg(not(asm))]
    #[inline]
    pub unsafe fn add_n(wp: LimbsMut, xp: Limbs, yp: Limbs,
                        n: i32) -> Limb {
        debug_assert!(n >= 1);
        debug_assert!(same_or_separate(wp, n, xp, n));
        debug_assert!(same_or_separate(wp, n, yp, n));

        add_n_generic(wp, xp, yp, n)
    }

    #[allow(dead_code)]
    unsafe fn sub_n_generic(mut wp: LimbsMut, mut xp: Limbs, mut yp: Limbs,
                            mut n: i32) -> Limb {
        let mut carry = Limb(0);

        debug_assert!(n >= 1);
        debug_assert!(same_or_separate(wp, n, xp, n));
        debug_assert!(same_or_separate(wp, n, yp, n));

        loop {
            let xl = *xp;
            let yl = *yp;

            let (sl, c1) = xl.sub_overflow(yl);
            let (rl, c2) = sl.sub_overflow(carry);

            carry = if c1 || c2 { Limb(1) } else { Limb(0) };
            *wp = rl;

            n -= 1;
            if n == 0 { break; }

            wp = wp.offset(1);
            xp = xp.offset(1);
            yp = yp.offset(1);

        }

        carry
    }

    /**
     * Subtracts the `n` least signficant limbs of `yp` from `xp`, storing the result in {wp, n}.
     * If there was a borrow from a higher-limb (i.e., the result would be negative), it is returned.
     */
    #[cfg(asm)]
    #[inline]
    pub unsafe fn sub_n(mut wp: LimbsMut, xp: Limbs, yp: Limbs,
                        n: i32) -> Limb {
        extern "C" {
            fn ramp_sub_n(wp: *mut Limb, xp: *const Limb, yp: *const Limb,
                          n: i32) -> Limb;
        }

        ramp_sub_n(&mut *wp, &*xp, &*yp, n)
    }

    /**
     * Subtracts the `n` least signficant limbs of `yp` from `xp`, storing the result in {wp, n}.
     * If there was a borrow from a higher-limb (i.e., the result would be negative), it is returned.
     */
    #[cfg(not(asm))]
    #[inline]
    pub unsafe fn sub_n(wp: LimbsMut, xp: Limbs, yp: Limbs,
                        n: i32) -> Limb {
        sub_n_generic(wp, xp, yp, n)
    }

    macro_rules! aors {
        ($op:ident, $lop:ident, $f:ident) => {
            #[inline]
            pub unsafe fn $op(wp: LimbsMut,
                              xp: Limbs, xs: i32,
                              yp: Limbs, ys: i32) -> Limb {

                debug_assert!(xs >= ys);
                debug_assert!(ys >= 0);

                let mut i = ys;
                let carry = $f(wp, xp, yp, ys);
                if carry == 1 {
                    loop {
                        if i >= xs { return Limb(1); }

                        let (x, carry) = Limb::$lop(*xp.offset(i as isize), Limb(1));
                        *wp.offset(i as isize) = x;
                        i += 1;
                        if !carry {
                            break;
                        }
                    }
                }

                if wp.as_const() != xp && i < xs {
                    copy_rest(xp, wp, xs, i);
                }

                return Limb(0);
            }
        }
    }

    aors!(add, add_overflow, add_n);
    aors!(sub, sub_overflow, sub_n);

    macro_rules! aors_1 {
        ($op:ident, $f:ident) => {
            #[inline]
            pub unsafe fn $op(mut wp: LimbsMut,
                              xp: Limbs, xs: i32,
                              y: Limb) -> Limb {

                if xs > 0 {
                    let (v, mut carry) = Limb::$f(*xp, y);
                    *wp = v;
                    let mut i = 1;
                    while carry {
                        if i >= xs { return Limb(1); }

                        let (v, c) = Limb::$f(*xp.offset(i as isize), Limb(1));
                        carry = c;
                        *wp.offset(i as isize) = v;
                        i += 1;
                    }
                }
                return Limb(0);
            }
        }
    }

    aors_1!(add_1, add_overflow);
    aors_1!(sub_1, sub_overflow);

    #[inline(always)]
    pub unsafe fn incr(mut ptr: LimbsMut, incr: Limb) {
        let (x, mut carry) = (*ptr).add_overflow(incr);
        *ptr = x;

        while carry {
            ptr = ptr.offset(1);
            let (x, c) = (*ptr).add_overflow(Limb(1));
            *ptr = x;
            carry = c;
        }
    }

    #[inline(always)]
    pub unsafe fn decr(mut ptr: LimbsMut, decr: Limb) {
        let x = *ptr;
        *ptr = x - decr;

        if x < decr {
            loop {
                ptr = ptr.offset(1);
                let x = *ptr;
                *ptr = x - 1;
                if x != 0 { break; }
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)