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