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
19namespace simdpp {
20namespace 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*/
36template<unsigned N, class E> SIMDPP_INL
37int8<N,expr_empty> to_int8(const int8<N,E>& a)
38{
39 return a.eval();
40}
41template<unsigned N, class E> SIMDPP_INL
42int8<N,expr_empty> to_int8(const uint8<N,E>& a)
43{
44 return int8<N>(a.eval());
45}
46template<unsigned N, class E> SIMDPP_INL
47int8<N,expr_empty> to_int8(const int16<N,E>& a)
48{
49 return detail::insn::i_to_uint8(uint16<N>(a.eval()));
50}
51template<unsigned N, class E> SIMDPP_INL
52int8<N,expr_empty> to_int8(const uint16<N,E>& a)
53{
54 return detail::insn::i_to_uint8(a.eval());
55}
56template<unsigned N, class E> SIMDPP_INL
57int8<N,expr_empty> to_int8(const int32<N,E>& a)
58{
59 return detail::insn::i_to_uint8(uint32<N>(a.eval()));
60}
61template<unsigned N, class E> SIMDPP_INL
62int8<N,expr_empty> to_int8(const uint32<N,E>& a)
63{
64 return detail::insn::i_to_uint8(a.eval());
65}
66template<unsigned N, class E> SIMDPP_INL
67int8<N,expr_empty> to_int8(const int64<N,E>& a)
68{
69 return detail::insn::i_to_uint8(uint64<N>(a.eval()));
70}
71template<unsigned N, class E> SIMDPP_INL
72int8<N,expr_empty> to_int8(const uint64<N,E>& a)
73{
74 return detail::insn::i_to_uint8(a.eval());
75}
76template<unsigned N, class E> SIMDPP_INL
77int8<N,expr_empty> to_int8(const float32<N,E>& a)
78{
79 return detail::insn::i_to_int8(a.eval());
80}
81template<unsigned N, class E> SIMDPP_INL
82int8<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*/
100template<unsigned N, class E> SIMDPP_INL
101uint8<N,expr_empty> to_uint8(const int8<N,E>& a)
102{
103 return uint8<N>(a.eval());
104}
105template<unsigned N, class E> SIMDPP_INL
106uint8<N,expr_empty> to_uint8(const uint8<N,E>& a)
107{
108 return a;
109}
110template<unsigned N, class E> SIMDPP_INL
111uint8<N,expr_empty> to_uint8(const int16<N,E>& a)
112{
113 return detail::insn::i_to_uint8(uint16<N>(a.eval()));
114}
115template<unsigned N, class E> SIMDPP_INL
116uint8<N,expr_empty> to_uint8(const uint16<N,E>& a)
117{
118 return detail::insn::i_to_uint8(a.eval());
119}
120template<unsigned N, class E> SIMDPP_INL
121uint8<N,expr_empty> to_uint8(const int32<N,E>& a)
122{
123 return detail::insn::i_to_uint8(uint32<N>(a.eval()));
124}
125template<unsigned N, class E> SIMDPP_INL
126uint8<N,expr_empty> to_uint8(const uint32<N,E>& a)
127{
128 return detail::insn::i_to_uint8(a.eval());
129}
130template<unsigned N, class E> SIMDPP_INL
131uint8<N,expr_empty> to_uint8(const int64<N,E>& a)
132{
133 return detail::insn::i_to_uint8(uint64<N>(a.eval()));
134}
135template<unsigned N, class E> SIMDPP_INL
136uint8<N,expr_empty> to_uint8(const uint64<N,E>& a)
137{
138 return detail::insn::i_to_uint8(a.eval());
139}
140template<unsigned N, class E> SIMDPP_INL
141uint8<N,expr_empty> to_uint8(const float32<N,E>& a)
142{
143 return detail::insn::i_to_uint8(a.eval());
144}
145template<unsigned N, class E> SIMDPP_INL
146uint8<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