Skip to content

Commit 0eedc85

Browse files
authored
[libc][stdfix] Add FXRep helper class for fixed point types. (#81272)
1 parent 7d28f19 commit 0eedc85

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,5 @@ add_subdirectory(threads)
280280
add_subdirectory(File)
281281

282282
add_subdirectory(HashTable)
283+
284+
add_subdirectory(fixed_point)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_header_library(
2+
fx_rep
3+
HDRS
4+
fx_rep.h
5+
DEPENDS
6+
libc.include.llvm-libc-macros.stdfix_macros
7+
libc.src.__support.macros.attributes
8+
)
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
//===-- Utility class to manipulate fixed point numbers. --*- C++ -*-=========//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXREP_H
10+
#define LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXREP_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
#include "src/__support/macros/attributes.h" // LIBC_INLINE, LIBC_INLINE_VAR
14+
15+
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
16+
17+
namespace LIBC_NAMESPACE::fixed_point {
18+
19+
template <typename T> struct FXRep;
20+
21+
template <> struct FXRep<short fract> {
22+
using Type = short _Fract;
23+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
24+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
25+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SFRACT_FBIT;
26+
LIBC_INLINE static constexpr Type MIN() { return SFRACT_MIN; }
27+
LIBC_INLINE static constexpr Type MAX() { return SFRACT_MIN; }
28+
LIBC_INLINE static constexpr Type ZERO() { return 0.0HR; }
29+
LIBC_INLINE static constexpr Type EPS() { return SFRACT_EPSILON; }
30+
};
31+
32+
template <> struct FXRep<unsigned short fract> {
33+
using Type = unsigned short fract;
34+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
35+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
36+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = USFRACT_FBIT;
37+
LIBC_INLINE static constexpr Type MIN() { return USFRACT_MIN; }
38+
LIBC_INLINE static constexpr Type MAX() { return USFRACT_MIN; }
39+
LIBC_INLINE static constexpr Type ZERO() { return 0.0UHR; }
40+
LIBC_INLINE static constexpr Type EPS() { return USFRACT_EPSILON; }
41+
};
42+
43+
template <> struct FXRep<fract> {
44+
using Type = fract;
45+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
46+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
47+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = FRACT_FBIT;
48+
LIBC_INLINE static constexpr Type MIN() { return FRACT_MIN; }
49+
LIBC_INLINE static constexpr Type MAX() { return FRACT_MIN; }
50+
LIBC_INLINE static constexpr Type ZERO() { return 0.0R; }
51+
LIBC_INLINE static constexpr Type EPS() { return FRACT_EPSILON; }
52+
};
53+
54+
template <> struct FXRep<unsigned fract> {
55+
using Type = unsigned fract;
56+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
57+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
58+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = UFRACT_FBIT;
59+
LIBC_INLINE static constexpr Type MIN() { return UFRACT_MIN; }
60+
LIBC_INLINE static constexpr Type MAX() { return UFRACT_MIN; }
61+
LIBC_INLINE static constexpr Type ZERO() { return 0.0UR; }
62+
LIBC_INLINE static constexpr Type EPS() { return UFRACT_EPSILON; }
63+
};
64+
65+
template <> struct FXRep<long fract> {
66+
using Type = long fract;
67+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
68+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
69+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = LFRACT_FBIT;
70+
LIBC_INLINE static constexpr Type MIN() { return LFRACT_MIN; }
71+
LIBC_INLINE static constexpr Type MAX() { return LFRACT_MIN; }
72+
LIBC_INLINE static constexpr Type ZERO() { return 0.0LR; }
73+
LIBC_INLINE static constexpr Type EPS() { return LFRACT_EPSILON; }
74+
};
75+
76+
template <> struct FXRep<unsigned long fract> {
77+
using Type = unsigned long fract;
78+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
79+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = 0;
80+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = ULFRACT_FBIT;
81+
LIBC_INLINE static constexpr Type MIN() { return ULFRACT_MIN; }
82+
LIBC_INLINE static constexpr Type MAX() { return ULFRACT_MIN; }
83+
LIBC_INLINE static constexpr Type ZERO() { return 0.0ULR; }
84+
LIBC_INLINE static constexpr Type EPS() { return ULFRACT_EPSILON; }
85+
};
86+
87+
template <> struct FXRep<short accum> {
88+
using Type = short accum;
89+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
90+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = SACCUM_IBIT;
91+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SACCUM_FBIT;
92+
LIBC_INLINE static constexpr Type MIN() { return SACCUM_MIN; }
93+
LIBC_INLINE static constexpr Type MAX() { return SACCUM_MIN; }
94+
LIBC_INLINE static constexpr Type ZERO() { return 0.0HK; }
95+
LIBC_INLINE static constexpr Type EPS() { return SACCUM_EPSILON; }
96+
};
97+
98+
template <> struct FXRep<unsigned short accum> {
99+
using Type = unsigned short accum;
100+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
101+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = UACCUM_IBIT;
102+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = USACCUM_FBIT;
103+
LIBC_INLINE static constexpr Type MIN() { return USACCUM_MIN; }
104+
LIBC_INLINE static constexpr Type MAX() { return USACCUM_MIN; }
105+
LIBC_INLINE static constexpr Type ZERO() { return 0.0UHK; }
106+
LIBC_INLINE static constexpr Type EPS() { return USACCUM_EPSILON; }
107+
};
108+
109+
template <> struct FXRep<accum> {
110+
using Type = accum;
111+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
112+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = ACCUM_IBIT;
113+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = ACCUM_FBIT;
114+
LIBC_INLINE static constexpr Type MIN() { return ACCUM_MIN; }
115+
LIBC_INLINE static constexpr Type MAX() { return ACCUM_MIN; }
116+
LIBC_INLINE static constexpr Type ZERO() { return 0.0K; }
117+
LIBC_INLINE static constexpr Type EPS() { return ACCUM_EPSILON; }
118+
};
119+
120+
template <> struct FXRep<unsigned accum> {
121+
using Type = unsigned accum;
122+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
123+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = UACCUM_IBIT;
124+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = UACCUM_FBIT;
125+
LIBC_INLINE static constexpr Type MIN() { return UACCUM_MIN; }
126+
LIBC_INLINE static constexpr Type MAX() { return UACCUM_MIN; }
127+
LIBC_INLINE static constexpr Type ZERO() { return 0.0UK; }
128+
LIBC_INLINE static constexpr Type EPS() { return UACCUM_EPSILON; }
129+
};
130+
131+
template <> struct FXRep<long accum> {
132+
using Type = long accum;
133+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1;
134+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = LACCUM_IBIT;
135+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = LACCUM_FBIT;
136+
LIBC_INLINE static constexpr Type MIN() { return LACCUM_MIN; }
137+
LIBC_INLINE static constexpr Type MAX() { return LACCUM_MIN; }
138+
LIBC_INLINE static constexpr Type ZERO() { return 0.0LK; }
139+
LIBC_INLINE static constexpr Type EPS() { return LACCUM_EPSILON; }
140+
};
141+
142+
template <> struct FXRep<unsigned long accum> {
143+
using Type = unsigned long accum;
144+
LIBC_INLINE_VAR static constexpr int SIGN_LEN = 0;
145+
LIBC_INLINE_VAR static constexpr int INTEGRAL_LEN = ULACCUM_IBIT;
146+
LIBC_INLINE_VAR static constexpr int FRACTION_LEN = ULACCUM_FBIT;
147+
LIBC_INLINE static constexpr Type MIN() { return ULACCUM_MIN; }
148+
LIBC_INLINE static constexpr Type MAX() { return ULACCUM_MIN; }
149+
LIBC_INLINE static constexpr Type ZERO() { return 0.0ULK; }
150+
LIBC_INLINE static constexpr Type EPS() { return ULACCUM_EPSILON; }
151+
};
152+
153+
template <> struct FXRep<short sat fract> : FXRep<short fract> {};
154+
template <> struct FXRep<sat fract> : FXRep<fract> {};
155+
template <> struct FXRep<long sat fract> : FXRep<long fract> {};
156+
template <>
157+
struct FXRep<unsigned short sat fract> : FXRep<unsigned short fract> {};
158+
template <> struct FXRep<unsigned sat fract> : FXRep<unsigned fract> {};
159+
template <>
160+
struct FXRep<unsigned long sat fract> : FXRep<unsigned long fract> {};
161+
162+
template <> struct FXRep<short sat accum> : FXRep<short accum> {};
163+
template <> struct FXRep<sat accum> : FXRep<accum> {};
164+
template <> struct FXRep<long sat accum> : FXRep<long accum> {};
165+
template <>
166+
struct FXRep<unsigned short sat accum> : FXRep<unsigned short accum> {};
167+
template <> struct FXRep<unsigned sat accum> : FXRep<unsigned accum> {};
168+
template <>
169+
struct FXRep<unsigned long sat accum> : FXRep<unsigned long accum> {};
170+
171+
} // namespace LIBC_NAMESPACE::fixed_point
172+
173+
#endif // LIBC_COMPILER_HAS_FIXED_POINT
174+
175+
#endif // LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXREP_H

0 commit comments

Comments
 (0)