• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #define BOOST_TEST_MODULE TestFunctionalUnpack
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/copy_n.hpp>
16 #include <boost/compute/algorithm/transform.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/iterator/zip_iterator.hpp>
19 #include <boost/compute/functional/detail/unpack.hpp>
20 
21 #include "check_macros.hpp"
22 #include "context_setup.hpp"
23 
24 namespace compute = boost::compute;
25 
BOOST_AUTO_TEST_CASE(plus_int)26 BOOST_AUTO_TEST_CASE(plus_int)
27 {
28     int data1[] = { 1, 3, 5, 7 };
29     int data2[] = { 2, 4, 6, 8 };
30 
31     compute::vector<int> input1(4, context);
32     compute::vector<int> input2(4, context);
33 
34     compute::copy_n(data1, 4, input1.begin(), queue);
35     compute::copy_n(data2, 4, input2.begin(), queue);
36 
37     compute::vector<int> output(4, context);
38     compute::transform(
39         compute::make_zip_iterator(
40             boost::make_tuple(input1.begin(), input2.begin())
41         ),
42         compute::make_zip_iterator(
43             boost::make_tuple(input1.end(), input2.end())
44         ),
45         output.begin(),
46         compute::detail::unpack(compute::plus<int>()),
47         queue
48     );
49     CHECK_RANGE_EQUAL(int, 4, output, (3, 7, 11, 15));
50 }
51 
BOOST_AUTO_TEST_CASE(fma_float)52 BOOST_AUTO_TEST_CASE(fma_float)
53 {
54     float data1[] = { 1, 3, 5, 7 };
55     float data2[] = { 2, 4, 6, 8 };
56     float data3[] = { 0, 9, 1, 2 };
57 
58     compute::vector<float> input1(4, context);
59     compute::vector<float> input2(4, context);
60     compute::vector<float> input3(4, context);
61 
62     compute::copy_n(data1, 4, input1.begin(), queue);
63     compute::copy_n(data2, 4, input2.begin(), queue);
64     compute::copy_n(data3, 4, input3.begin(), queue);
65 
66     compute::vector<int> output(4, context);
67     compute::transform(
68         compute::make_zip_iterator(
69             boost::make_tuple(input1.begin(), input2.begin(), input3.begin())
70         ),
71         compute::make_zip_iterator(
72             boost::make_tuple(input1.end(), input2.end(), input3.begin())
73         ),
74         output.begin(),
75         compute::detail::unpack(compute::fma<float>()),
76         queue
77     );
78 }
79 
BOOST_AUTO_TEST_CASE(subtract_int2)80 BOOST_AUTO_TEST_CASE(subtract_int2)
81 {
82     using compute::int2_;
83 
84     int data[] = { 4, 2, 5, 1, 6, 3, 7, 0 };
85     compute::vector<int2_> input(4, context);
86     compute::copy_n(reinterpret_cast<int2_ *>(data), 4, input.begin(), queue);
87 
88     compute::vector<int> output(4, context);
89     compute::transform(
90         input.begin(),
91         input.end(),
92         output.begin(),
93         compute::detail::unpack(compute::minus<int>()),
94         queue
95     );
96     CHECK_RANGE_EQUAL(int, 4, output, (2, 4, 3, 7));
97 }
98 
99 BOOST_AUTO_TEST_SUITE_END()
100