Open Broadcaster Software
Free, open source software for live streaming and recording
half.h
Go to the documentation of this file.
1 /******************************************************************************
2  Copyright (C) 2020 by Hugh Bailey <obs.jim@gmail.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 /******************************************************************************
19  The MIT License (MIT)
20 
21 Copyright (c) 2011-2019 Microsoft Corp
22 
23 Permission is hereby granted, free of charge, to any person obtaining a copy of this
24 software and associated documentation files (the "Software"), to deal in the Software
25 without restriction, including without limitation the rights to use, copy, modify,
26 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
27 permit persons to whom the Software is furnished to do so, subject to the following
28 conditions:
29 
30 The above copyright notice and this permission notice shall be included in all copies
31 or substantial portions of the Software.
32 
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
34 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
35 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
36 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
37 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
38 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 ******************************************************************************/
40 
41 #pragma once
42 
43 #include "math-defs.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 struct half {
50  uint16_t u;
51 };
52 
53 /* adapted from DirectXMath XMConvertFloatToHalf */
54 static struct half half_from_float(float f)
55 {
56  uint32_t Result;
57 
58  uint32_t IValue;
59  memcpy(&IValue, &f, sizeof(IValue));
60  uint32_t Sign = (IValue & 0x80000000U) >> 16U;
61  IValue = IValue & 0x7FFFFFFFU; // Hack off the sign
62 
63  if (IValue > 0x477FE000U) {
64  // The number is too large to be represented as a half. Saturate to infinity.
65  if (((IValue & 0x7F800000) == 0x7F800000) &&
66  ((IValue & 0x7FFFFF) != 0)) {
67  Result = 0x7FFF; // NAN
68  } else {
69  Result = 0x7C00U; // INF
70  }
71  } else if (!IValue) {
72  Result = 0;
73  } else {
74  if (IValue < 0x38800000U) {
75  // The number is too small to be represented as a normalized half.
76  // Convert it to a denormalized value.
77  uint32_t Shift = 113U - (IValue >> 23U);
78  IValue = (0x800000U | (IValue & 0x7FFFFFU)) >> Shift;
79  } else {
80  // Rebias the exponent to represent the value as a normalized half.
81  IValue += 0xC8000000U;
82  }
83 
84  Result = ((IValue + 0x0FFFU + ((IValue >> 13U) & 1U)) >> 13U) &
85  0x7FFFU;
86  }
87 
88  struct half h;
89  h.u = (uint16_t)(Result | Sign);
90  return h;
91 }
92 
93 static struct half half_from_bits(uint16_t u)
94 {
95  struct half h;
96  h.u = u;
97  return h;
98 }
99 
100 #ifdef __cplusplus
101 }
102 #endif
math-defs.h
half
Definition: half.h:49
half::u
uint16_t u
Definition: half.h:86