2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "FloatingPointConverter.hpp"
7 #include <armnnUtils/Half.hpp>
13 #include <boost/test/unit_test.hpp>
15 BOOST_AUTO_TEST_SUITE(TestFPConversion)
17 BOOST_AUTO_TEST_CASE(TestConvertFp32ToFp16)
19 using namespace half_float::literal;
21 float floatArray[] = { 1.0f, 2.0f, 0.5f, 3.1f, 2.4f,
22 5.666f, 6.444f, 7.1f, 432.121f, 12.22f };
23 size_t numFloats = sizeof(floatArray) / sizeof(floatArray[0]);
24 std::vector<armnn::Half> convertedBuffer(numFloats, 0.0_h);
26 armnnUtils::FloatingPointConverter::ConvertFloat32To16(floatArray, numFloats, convertedBuffer.data());
28 for (size_t i = 0; i < numFloats; i++)
30 armnn::Half expected(floatArray[i]);
31 armnn::Half actual = convertedBuffer[i];
32 BOOST_CHECK_EQUAL(expected, actual);
34 float convertedHalf = actual;
35 BOOST_CHECK_CLOSE(floatArray[i], convertedHalf, 0.07);
39 BOOST_AUTO_TEST_CASE(TestConvertFp16ToFp32)
41 using namespace half_float::literal;
43 armnn::Half halfArray[] = { 1.0_h, 2.0_h, 0.5_h, 3.1_h, 2.4_h,
44 5.666_h, 6.444_h, 7.1_h, 432.121_h, 12.22_h };
45 size_t numFloats = sizeof(halfArray) / sizeof(halfArray[0]);
46 std::vector<float> convertedBuffer(numFloats, 0.0f);
48 armnnUtils::FloatingPointConverter::ConvertFloat16To32(halfArray, numFloats, convertedBuffer.data());
50 for (size_t i = 0; i < numFloats; i++)
52 float expected(halfArray[i]);
53 float actual = convertedBuffer[i];
54 BOOST_CHECK_EQUAL(expected, actual);
58 BOOST_AUTO_TEST_SUITE_END()