HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
float16.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include <emmintrin.h>
8#include <immintrin.h>
9#include <type_traits>
10
11namespace tt {
12
13class float16 {
14 uint16_t v;
15
16public:
17 float16() noexcept : v() {}
18
19 template<typename T, std::enable_if_t<std::is_arithmetic_v<T>,int> = 0>
20 float16(T const &rhs) noexcept {
21 ttlet tmp1 = narrow_cast<float>(rhs);
22 ttlet tmp2 = _mm_set_ss(tmp1);
23 ttlet tmp3 = _mm_cvtps_ph(tmp2, _MM_FROUND_CUR_DIRECTION);
24 _mm_storeu_si16(&v, tmp3);
25 }
26
27 template<typename T, std::enable_if_t<std::is_arithmetic_v<T>,int> = 0>
28 float16 &operator=(T const &rhs) noexcept {
29 ttlet tmp1 = narrow_cast<float>(rhs);
30 ttlet tmp2 = _mm_set_ss(tmp1);
31 ttlet tmp3 = _mm_cvtps_ph(tmp2, _MM_FROUND_CUR_DIRECTION);
32 _mm_storeu_si16(&v, tmp3);
33 return *this;
34 }
35
36 operator float () const noexcept {
37 ttlet tmp1 = _mm_loadu_si16(&v);
38 ttlet tmp2 = _mm_cvtph_ps(tmp1);
39 return _mm_cvtss_f32(tmp2);
40 }
41
42 float16(uint16_t const &rhs, bool) noexcept : v(rhs) {}
43
44 [[nodiscard]] constexpr uint16_t get() const noexcept {
45 return v;
46 }
47
48 constexpr float16 &set(uint16_t rhs) noexcept {
49 v = rhs;
50 return *this;
51 }
52
53 [[nodiscard]] friend bool operator==(float16 const &lhs, float16 const &rhs) noexcept {
54 return lhs.v == rhs.v;
55 }
56
57 [[nodiscard]] friend bool operator!=(float16 const &lhs, float16 const &rhs) noexcept {
58 return lhs.v != rhs.v;
59 }
60};
61
62}
Definition float16.hpp:13