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
use std::{cmp, mem, raw};
use {Vector, Bitcast};

pub unsafe trait Convert<Out> {
    fn convert(self) -> Out;
}

unsafe impl<'a, In, Out> Convert<&'a [Out]> for &'a [In]
    where In: Vector, Out: Vector,
          <In as Vector>::Item: Bitcast<<Out as Vector>::Item> {
        #[inline]
        fn convert(self) -> &'a [Out] {
            // we need to ensure that the pointer has valid alignment
            // and that the slice has correct length to fit `Out`s
            // exactly; the first checks in each `assert` are
            // statically checkable, and so hopefully results in the
            // asserts being removed for "downcasts" (e.g. [u32x4] to
            // [u32]).
            assert!(mem::align_of::<In>() >= mem::align_of::<Out>() ||
                    self.as_ptr() as usize % mem::align_of::<Out>() == 0);
            assert!(mem::size_of::<In>() % mem::size_of::<Out>() == 0 ||
                    (self.len() * Vector::count(None::<In>)) % Vector::count(None::<Out>) == 0);

            unsafe {
                mem::transmute(raw::Slice {
                    data: self.as_ptr() as *mut Out,
                    len: self.len() * Vector::count(None::<In>) / Vector::count(None::<Out>)
                })
            }
        }
}
unsafe impl<'a, In, Out> Convert<&'a mut [Out]> for &'a mut [In]
    where In: Vector, Out: Vector,
          <In as Vector>::Item: Bitcast<<Out as Vector>::Item> {
        #[inline]
        fn convert(self) -> &'a mut [Out] {
            // we need to ensure that the pointer has valid alignment
            // and that the slice has correct length to fit `Out`s
            // exactly; the first checks in each `assert` are
            // statically checkable, and so hopefully results in the
            // asserts being removed for "downcasts" (e.g. [u32x4] to
            // [u32]).
            assert!(mem::align_of::<In>() >= mem::align_of::<Out>() ||
                    self.as_ptr() as usize % mem::align_of::<Out>() == 0);
            assert!(mem::size_of::<In>() % mem::size_of::<Out>() == 0 ||
                    (self.len() * Vector::count(None::<In>)) % Vector::count(None::<Out>) == 0);

            unsafe {
                mem::transmute(raw::Slice {
                    data: self.as_ptr() as *mut Out,
                    len: self.len() * Vector::count(None::<In>) / Vector::count(None::<Out>)
                })
            }
        }
}

/// A type that repesents a `&[U]` converted from a `&[T]`, where the
/// conversion may be forced to leave leading/trailing elements (due
/// to alignment/size mismatches).
pub struct Tails<'a,T:'a,U:'a> {
    pub start: &'a [T],
    pub middle: &'a [U],
    pub end: &'a [T],
}
impl<'a, T, U> Copy for Tails<'a, T, U> {}
impl<'a, T, U> Clone for Tails<'a, T, U> {
    fn clone(&self) -> Tails<'a, T, U> { *self }
}

/// A type that repesents a `&mut [U]` converted from a `&mut [T]`,
/// where the conversion may be forced to leave leading/trailing
/// elements (due to alignment/size mismatches).
pub struct TailsMut<'a,T:'a,U:'a> {
    pub start: &'a mut [T],
    pub middle: &'a mut [U],
    pub end: &'a mut [T],
}

unsafe impl<'a, In, Out> Convert<Tails<'a, In, Out>> for &'a [In]
    where In: Vector, Out: Vector,
          <In as Vector>::Item: Bitcast<<Out as Vector>::Item>
{
    #[inline]
    fn convert(self) -> Tails<'a,In,Out> {
        let isize = mem::size_of::<In>();
        let osize = mem::size_of::<Out>();
        let oalign = mem::align_of::<Out>();

        let start = self.as_ptr() as usize;
        let len = self.len();

        let offset = start % oalign;
        let slen = if offset == 0 {
            0
        } else {
            cmp::min(len, (oalign - offset) / isize)
        };
        let remaining = len - slen;
        let mlen_in_outs = remaining * isize / osize;
        let mlen_in_ins = mlen_in_outs * osize / isize;

        let (initial, tail) = self.split_at(slen);
        Tails {
            start: initial,
            middle: unsafe {mem::transmute(raw::Slice {
                data: tail.as_ptr() as *mut Out,
                len: mlen_in_outs,
            })},
            end: &tail[mlen_in_ins..]
        }
    }
}
unsafe impl<'a, In, Out> Convert<TailsMut<'a, In, Out>> for &'a mut [In]
    where In: Vector, Out: Vector,
          <In as Vector>::Item: Bitcast<<Out as Vector>::Item>
{
    #[inline]
    fn convert(self) -> TailsMut<'a,In,Out> {
        let isize = mem::size_of::<In>();
        let osize = mem::size_of::<Out>();
        let oalign = mem::align_of::<Out>();

        let start = self.as_ptr() as usize;
        let len = self.len();

        let offset = start % oalign;
        let slen = if offset == 0 {
            0
        } else {
            cmp::min(len, (oalign - offset) / isize)
        };
        let remaining = len - slen;

        let mlen_in_outs = remaining * isize / osize;
        let mlen_in_ins = mlen_in_outs * osize / isize;

        let (start, tail) = self.split_at_mut(slen);
        TailsMut {
            start: start,
            middle: unsafe {mem::transmute(raw::Slice {
                data: tail.as_ptr() as *mut Out,
                len: mlen_in_outs,
            })},
            end: &mut tail[mlen_in_ins..]
        }
    }
}