telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 6 | #include <armnnUtils/FloatingPointConverter.hpp> |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 7 | |
Narumol Prangnawarat | 8832522 | 2020-03-06 14:45:57 +0000 | [diff] [blame] | 8 | #include "BFloat16.hpp" |
arovir01 | 616e775 | 2018-10-01 17:08:59 +0100 | [diff] [blame] | 9 | #include "Half.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | |
Jim Flynn | 39faea8 | 2023-09-17 09:02:23 +0100 | [diff] [blame] | 11 | #include <armnn/Exceptions.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnnUtils |
| 15 | { |
| 16 | |
| 17 | void FloatingPointConverter::ConvertFloat32To16(const float* srcFloat32Buffer, |
| 18 | size_t numElements, |
| 19 | void* dstFloat16Buffer) |
| 20 | { |
Jim Flynn | 39faea8 | 2023-09-17 09:02:23 +0100 | [diff] [blame] | 21 | if (srcFloat32Buffer == nullptr) |
| 22 | { |
| 23 | throw armnn::InvalidArgumentException("ConvertFloat32To16: source float32 buffer pointer is null"); |
| 24 | } |
| 25 | if (dstFloat16Buffer == nullptr) |
| 26 | { |
| 27 | throw armnn::InvalidArgumentException("ConvertFloat32To16: destination float16 buffer pointer is null"); |
| 28 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | |
Ryan OShea | f4bfa6a | 2020-06-10 11:33:37 +0100 | [diff] [blame] | 30 | armnn::Half* pHalf = static_cast<armnn::Half*>(dstFloat16Buffer); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 31 | |
| 32 | for (size_t i = 0; i < numElements; i++) |
| 33 | { |
| 34 | pHalf[i] = armnn::Half(srcFloat32Buffer[i]); |
Narumol Prangnawarat | 9f62d33 | 2023-07-11 16:49:00 +0100 | [diff] [blame] | 35 | if (isinf(pHalf[i])) |
| 36 | { |
| 37 | // If the value of converted Fp16 is infinity, round to the closest finite Fp16 value. |
| 38 | pHalf[i] = copysign(std::numeric_limits<armnn::Half>::max(), pHalf[i]); |
| 39 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | void FloatingPointConverter::ConvertFloat16To32(const void* srcFloat16Buffer, |
| 44 | size_t numElements, |
| 45 | float* dstFloat32Buffer) |
| 46 | { |
Jim Flynn | 39faea8 | 2023-09-17 09:02:23 +0100 | [diff] [blame] | 47 | if (srcFloat16Buffer == nullptr) |
| 48 | { |
| 49 | throw armnn::InvalidArgumentException("ConvertFloat16To32: source float16 buffer pointer is null"); |
| 50 | } |
| 51 | if (dstFloat32Buffer == nullptr) |
| 52 | { |
| 53 | throw armnn::InvalidArgumentException("ConvertFloat16To32: destination float32 buffer pointer is null"); |
| 54 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | |
Ryan OShea | f4bfa6a | 2020-06-10 11:33:37 +0100 | [diff] [blame] | 56 | const armnn::Half* pHalf = static_cast<const armnn::Half*>(srcFloat16Buffer); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 57 | |
| 58 | for (size_t i = 0; i < numElements; i++) |
| 59 | { |
| 60 | dstFloat32Buffer[i] = pHalf[i]; |
| 61 | } |
| 62 | } |
| 63 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 64 | } //namespace armnnUtils |