1 | /* Copyright (C) 2013-2017 Povilas Kanapickas <povilas@radix.lt> |
2 | |
3 | Distributed under the Boost Software License, Version 1.0. |
4 | (See accompanying file LICENSE_1_0.txt or copy at |
5 | http://www.boost.org/LICENSE_1_0.txt) |
6 | */ |
7 | |
8 | #ifndef LIBSIMDPP_SIMDPP_CORE_TO_INT8_H |
9 | #define LIBSIMDPP_SIMDPP_CORE_TO_INT8_H |
10 | |
11 | #ifndef LIBSIMDPP_SIMD_H |
12 | #error "This file must be included through simd.h" |
13 | #endif |
14 | |
15 | #include <simdpp/types.h> |
16 | #include <simdpp/detail/insn/conv_shrink_to_int8.h> |
17 | #include <simdpp/detail/insn/conv_float_to_int8.h> |
18 | |
19 | namespace simdpp { |
20 | namespace SIMDPP_ARCH_NAMESPACE { |
21 | |
22 | /** Converts elements within a vector to 8-bit signed values. |
23 | |
24 | The conversion rules are as follows: |
25 | 16-bit and wider integers are truncated. |
26 | floating-point numbers are converted to integer values and truncated. |
27 | If floating-point value can not be represented in 8-bit signed integer, |
28 | the behavior is different for different instruction sets. |
29 | |
30 | @code |
31 | r0 = (int8_t) a0 |
32 | ... |
33 | rN = (int8_t) aN |
34 | @endcode |
35 | */ |
36 | template<unsigned N, class E> SIMDPP_INL |
37 | int8<N,expr_empty> to_int8(const int8<N,E>& a) |
38 | { |
39 | return a.eval(); |
40 | } |
41 | template<unsigned N, class E> SIMDPP_INL |
42 | int8<N,expr_empty> to_int8(const uint8<N,E>& a) |
43 | { |
44 | return int8<N>(a.eval()); |
45 | } |
46 | template<unsigned N, class E> SIMDPP_INL |
47 | int8<N,expr_empty> to_int8(const int16<N,E>& a) |
48 | { |
49 | return detail::insn::i_to_uint8(uint16<N>(a.eval())); |
50 | } |
51 | template<unsigned N, class E> SIMDPP_INL |
52 | int8<N,expr_empty> to_int8(const uint16<N,E>& a) |
53 | { |
54 | return detail::insn::i_to_uint8(a.eval()); |
55 | } |
56 | template<unsigned N, class E> SIMDPP_INL |
57 | int8<N,expr_empty> to_int8(const int32<N,E>& a) |
58 | { |
59 | return detail::insn::i_to_uint8(uint32<N>(a.eval())); |
60 | } |
61 | template<unsigned N, class E> SIMDPP_INL |
62 | int8<N,expr_empty> to_int8(const uint32<N,E>& a) |
63 | { |
64 | return detail::insn::i_to_uint8(a.eval()); |
65 | } |
66 | template<unsigned N, class E> SIMDPP_INL |
67 | int8<N,expr_empty> to_int8(const int64<N,E>& a) |
68 | { |
69 | return detail::insn::i_to_uint8(uint64<N>(a.eval())); |
70 | } |
71 | template<unsigned N, class E> SIMDPP_INL |
72 | int8<N,expr_empty> to_int8(const uint64<N,E>& a) |
73 | { |
74 | return detail::insn::i_to_uint8(a.eval()); |
75 | } |
76 | template<unsigned N, class E> SIMDPP_INL |
77 | int8<N,expr_empty> to_int8(const float32<N,E>& a) |
78 | { |
79 | return detail::insn::i_to_int8(a.eval()); |
80 | } |
81 | template<unsigned N, class E> SIMDPP_INL |
82 | int8<N,expr_empty> to_int8(const float64<N,E>& a) |
83 | { |
84 | return detail::insn::i_to_int8(a.eval()); |
85 | } |
86 | |
87 | /** Converts elements within a vector to 8-bit unsigned values. |
88 | |
89 | The conversion rules are as follows: |
90 | 16-bit and wider integers are truncated. |
91 | If floating-point value can not be represented in 8-bit unsigned integer, |
92 | the behavior is different for different instruction sets. |
93 | |
94 | @code |
95 | r0 = (uint8_t) a0 |
96 | ... |
97 | rN = (uint8_t) aN |
98 | @endcode |
99 | */ |
100 | template<unsigned N, class E> SIMDPP_INL |
101 | uint8<N,expr_empty> to_uint8(const int8<N,E>& a) |
102 | { |
103 | return uint8<N>(a.eval()); |
104 | } |
105 | template<unsigned N, class E> SIMDPP_INL |
106 | uint8<N,expr_empty> to_uint8(const uint8<N,E>& a) |
107 | { |
108 | return a; |
109 | } |
110 | template<unsigned N, class E> SIMDPP_INL |
111 | uint8<N,expr_empty> to_uint8(const int16<N,E>& a) |
112 | { |
113 | return detail::insn::i_to_uint8(uint16<N>(a.eval())); |
114 | } |
115 | template<unsigned N, class E> SIMDPP_INL |
116 | uint8<N,expr_empty> to_uint8(const uint16<N,E>& a) |
117 | { |
118 | return detail::insn::i_to_uint8(a.eval()); |
119 | } |
120 | template<unsigned N, class E> SIMDPP_INL |
121 | uint8<N,expr_empty> to_uint8(const int32<N,E>& a) |
122 | { |
123 | return detail::insn::i_to_uint8(uint32<N>(a.eval())); |
124 | } |
125 | template<unsigned N, class E> SIMDPP_INL |
126 | uint8<N,expr_empty> to_uint8(const uint32<N,E>& a) |
127 | { |
128 | return detail::insn::i_to_uint8(a.eval()); |
129 | } |
130 | template<unsigned N, class E> SIMDPP_INL |
131 | uint8<N,expr_empty> to_uint8(const int64<N,E>& a) |
132 | { |
133 | return detail::insn::i_to_uint8(uint64<N>(a.eval())); |
134 | } |
135 | template<unsigned N, class E> SIMDPP_INL |
136 | uint8<N,expr_empty> to_uint8(const uint64<N,E>& a) |
137 | { |
138 | return detail::insn::i_to_uint8(a.eval()); |
139 | } |
140 | template<unsigned N, class E> SIMDPP_INL |
141 | uint8<N,expr_empty> to_uint8(const float32<N,E>& a) |
142 | { |
143 | return detail::insn::i_to_uint8(a.eval()); |
144 | } |
145 | template<unsigned N, class E> SIMDPP_INL |
146 | uint8<N,expr_empty> to_uint8(const float64<N,E>& a) |
147 | { |
148 | return detail::insn::i_to_uint8(a.eval()); |
149 | } |
150 | |
151 | } // namespace SIMDPP_ARCH_NAMESPACE |
152 | } // namespace simdpp |
153 | |
154 | #endif |
155 | |
156 | |