1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 #ifndef UMATRIX_H_740EBFEF554E833645E0FD72419A8185
10 #define UMATRIX_H_740EBFEF554E833645E0FD72419A8185
16 /// \class matrix umatrix.h ustl.h
17 /// \ingroup Sequences
19 /// \brief A two-dimensional array of NX*NY elements of type T.
21 template <size_t NX
, size_t NY
, typename T
>
22 class matrix
: public tuple
<NX
*NY
,T
> {
24 typedef tuple
<NX
,T
> row_type
;
25 typedef tuple
<NY
,T
> column_type
;
26 typedef tuple
<NX
*NY
,T
> tuple_type
;
27 typedef typename
tuple_type::value_type value_type
;
28 typedef typename
tuple_type::size_type size_type
;
29 typedef typename
tuple_type::pointer pointer
;
30 typedef typename
tuple_type::const_pointer const_pointer
;
31 typedef typename
tuple_type::reference reference
;
32 typedef typename
tuple_type::const_reference const_reference
;
33 typedef typename
tuple_type::iterator iterator
;
34 typedef typename
tuple_type::const_iterator const_iterator
;
35 typedef typename
tuple_type::range_t range_t
;
36 typedef typename
tuple_type::const_range_t const_range_t
;
37 typedef typename
tuple_type::reverse_iterator reverse_iterator
;
38 typedef typename
tuple_type::const_reverse_iterator const_reverse_iterator
;
40 inline matrix (void) { }
41 inline size_type
columns (void) const { return (NX
); }
42 inline size_type
rows (void) const { return (NY
); }
43 inline const_iterator
at (size_type i
) const { return (matrix::begin() + i
* NX
); }
44 inline iterator
at (size_type i
) { return (matrix::begin() + i
* NX
); }
45 inline const_iterator
operator[] (size_type i
) const { return (at (i
)); }
46 inline iterator
operator[] (size_type i
) { return (at (i
)); }
47 inline row_type
row (size_type r
) const { return (row_type (at (r
))); }
48 inline column_type
column (size_type c
) const;
49 template <typename T2
>
50 inline const matrix
& operator= (const matrix
<NX
,NY
,T2
>& src
) { tuple_type::operator= (src
); return (*this); }
51 inline const matrix
& operator= (const matrix
<NX
,NY
,T
>& src
) { tuple_type::operator= (src
); return (*this); }
52 inline const matrix
& operator+= (const_reference v
) { tuple_type::operator+= (v
); return (*this); }
53 inline const matrix
& operator-= (const_reference v
) { tuple_type::operator-= (v
); return (*this); }
54 inline const matrix
& operator*= (const_reference v
) { tuple_type::operator*= (v
); return (*this); }
55 inline const matrix
& operator/= (const_reference v
) { tuple_type::operator/= (v
); return (*this); }
56 inline const matrix
operator+ (const_reference v
) const
57 { matrix
result (*this); result
+= v
; return (result
); }
58 inline const matrix
operator- (const_reference v
) const
59 { matrix
result (*this); result
-= v
; return (result
); }
60 inline const matrix
operator* (const_reference v
) const
61 { matrix
result (*this); result
*= v
; return (result
); }
62 inline const matrix
operator/ (const_reference v
) const
63 { matrix
result (*this); result
/= v
; return (result
); }
66 template <size_t NX
, size_t NY
, typename T
>
67 inline typename matrix
<NX
,NY
,T
>::column_type matrix
<NX
,NY
,T
>::column (size_type c
) const
70 const_iterator
src (matrix::begin() + c
);
71 iterator
dest (result
.begin());
72 for (uoff_t i
= 0; i
< NY
; ++ i
, ++ dest
, src
+= NX
)
77 //----------------------------------------------------------------------
78 // Define SIMD specializations for member functions.
81 #define MATRIX_R(v) "m"(v[0]),"m"(v[4]),"m"(v[8]),"m"(v[12])
82 #define MATRIX_W(v) "=m"(v[0]),"=m"(v[4]),"=m"(v[8]),"=m"(v[12])
83 #define SSE_TUPLE_SPECS(n,type) \
84 template <> inline tuple<n,type>::tuple (void) \
85 { asm volatile ("xorps %%xmm0, %%xmm0\n\t" \
86 "movups %%xmm0, %0\n\t" \
87 "movups %%xmm0, %1\n\t" \
88 "movups %%xmm0, %2\n\t" \
90 : "=m"(m_v[0]),"=m"(m_v[4]),"=m"(m_v[8]),"=m"(m_v[12]) \
94 SIMD_PASSIGN_SPEC(n,type) \
96 asm volatile ("movups %2, %%xmm0\n\t" \
97 "movups %3, %%xmm1\n\t" \
98 "movups %%xmm0, %0\n\t" \
100 : "=m"(oout[0]),"=m"(oout[4]) \
101 : "m"(oin[0]),"m"(oin[4]) \
102 : "xmm0", "xmm1", "memory"); \
103 asm volatile ("movups %2, %%xmm0\n\t" \
104 "movups %3, %%xmm1\n\t" \
105 "movups %%xmm0, %0\n\t" \
106 "movups %%xmm1, %1" \
107 : "=m"(oout[8]),"=m"(oout[12]) \
108 : "m"(oin[8]),"m"(oin[12]) \
109 : "xmm0", "xmm1", "memory"); \
112 SSE_TUPLE_SPECS(16,float)
113 SSE_TUPLE_SPECS(16,int32_t)
114 SSE_TUPLE_SPECS(16,uint32_t)
115 #undef SSE_TUPLE_SPECS
116 #undef TOUCH_MATRIX_R
117 #undef TOUCH_MATRIX_W