Skip to content

Commit b7e05c8

Browse files
author
Siva Chandra Reddy
committed
[libc] Add implementations of the remaining fenv functions.
Namely, implementations of fegetexceptfflag, fesetexceptflag, fegetenv, fesetenv, feholdexcept and feupdateenv have been added. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D96935
1 parent 53e83af commit b7e05c8

25 files changed

+707
-19
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ set(TARGET_LIBC_ENTRYPOINTS
2222

2323
# fenv.h entrypoints
2424
libc.src.fenv.feclearexcept
25+
libc.src.fenv.fegetenv
26+
libc.src.fenv.fegetexceptflag
2527
libc.src.fenv.fegetround
28+
libc.src.fenv.feholdexcept
29+
libc.src.fenv.fesetenv
30+
libc.src.fenv.fesetexceptflag
2631
libc.src.fenv.fesetround
2732
libc.src.fenv.feraiseexcept
2833
libc.src.fenv.fetestexcept
34+
libc.src.fenv.feupdateenv
2935

3036
# stdlib.h entrypoints
3137
libc.src.stdlib.abs

libc/config/linux/api.td

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ def MathAPI : PublicAPI<"math.h"> {
179179
];
180180
}
181181

182+
def FEnvT : TypeDecl<"fenv_t"> {
183+
let Decl = [{
184+
#ifdef __aarch64__
185+
typedef struct {
186+
unsigned char __control_word[4];
187+
unsigned char __status_word[4];
188+
} fenv_t;
189+
#endif
190+
#ifdef __x86_64__
191+
typedef struct {
192+
unsigned char __x86_status[28];
193+
unsigned char __mxcsr[4];
194+
} fenv_t;
195+
#endif
196+
}];
197+
}
198+
199+
def FExceptT : TypeDecl<"fexcept_t"> {
200+
let Decl = [{
201+
typedef int fexcept_t;
202+
}];
203+
}
204+
182205
def FenvAPI: PublicAPI<"fenv.h"> {
183206
let Macros = [
184207
SimpleMacroDef<"FE_DIVBYZERO", "1">,
@@ -193,6 +216,10 @@ def FenvAPI: PublicAPI<"fenv.h"> {
193216
SimpleMacroDef<"FE_TOWARDZERO", "4">,
194217
SimpleMacroDef<"FE_UPWARD", "8">,
195218
];
219+
let TypeDeclarations = [
220+
FEnvT,
221+
FExceptT,
222+
];
196223
}
197224

198225
def StringAPI : PublicAPI<"string.h"> {

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ set(TARGET_LIBC_ENTRYPOINTS
2525

2626
# fenv.h entrypoints
2727
libc.src.fenv.feclearexcept
28+
libc.src.fenv.fegetenv
29+
libc.src.fenv.fegetexceptflag
2830
libc.src.fenv.fegetround
31+
libc.src.fenv.feholdexcept
32+
libc.src.fenv.fesetenv
33+
libc.src.fenv.fesetexceptflag
2934
libc.src.fenv.fesetround
3035
libc.src.fenv.feraiseexcept
3136
libc.src.fenv.fetestexcept
37+
libc.src.fenv.feupdateenv
3238

3339
# signal.h entrypoints
3440
libc.src.signal.raise

libc/spec/stdc.td

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ def StdC : StandardSpec<"stdc"> {
9696
]
9797
>;
9898

99+
NamedType FEnvT = NamedType<"fenv_t">;
100+
PtrType FEnvTPtr = PtrType<FEnvT>;
101+
ConstType ConstFEnvTPtr = ConstType<FEnvTPtr>;
102+
NamedType FExceptT = NamedType<"fexcept_t">;
103+
PtrType FExceptTPtr = PtrType<FExceptT>;
104+
ConstType ConstFExceptTPtr = ConstType<FExceptTPtr>;
99105
HeaderSpec Fenv = HeaderSpec<
100106
"fenv.h",
101107
[
@@ -111,7 +117,10 @@ def StdC : StandardSpec<"stdc"> {
111117
Macro<"FE_TOWARDZERO">,
112118
Macro<"FE_UPWARD">
113119
],
114-
[], // Types
120+
[
121+
NamedType<"fenv_t">,
122+
NamedType<"fexcept_t">,
123+
], // Types
115124
[], // Enumerations
116125
[
117126
FunctionSpec<
@@ -139,6 +148,36 @@ def StdC : StandardSpec<"stdc"> {
139148
RetValSpec<IntType>,
140149
[]
141150
>,
151+
FunctionSpec<
152+
"fegetenv",
153+
RetValSpec<IntType>,
154+
[ArgSpec<FEnvTPtr>]
155+
>,
156+
FunctionSpec<
157+
"fesetenv",
158+
RetValSpec<IntType>,
159+
[ArgSpec<ConstFEnvTPtr>]
160+
>,
161+
FunctionSpec<
162+
"fegetexceptflag",
163+
RetValSpec<IntType>,
164+
[ArgSpec<FExceptTPtr>, ArgSpec<IntType>]
165+
>,
166+
FunctionSpec<
167+
"fesetexceptflag",
168+
RetValSpec<IntType>,
169+
[ArgSpec<ConstFExceptTPtr>, ArgSpec<IntType>]
170+
>,
171+
FunctionSpec<
172+
"feholdexcept",
173+
RetValSpec<IntType>,
174+
[ArgSpec<FEnvTPtr>]
175+
>,
176+
FunctionSpec<
177+
"feupdateenv",
178+
RetValSpec<IntType>,
179+
[ArgSpec<ConstFEnvTPtr>]
180+
>,
142181
]
143182
>;
144183

libc/src/fenv/CMakeLists.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,81 @@ add_entrypoint_object(
6262
COMPILE_OPTIONS
6363
-O2
6464
)
65+
66+
add_entrypoint_object(
67+
fegetenv
68+
SRCS
69+
fegetenv.cpp
70+
HDRS
71+
fegetenv.h
72+
DEPENDS
73+
libc.include.fenv
74+
libc.utils.FPUtil.fputil
75+
COMPILE_OPTIONS
76+
-O2
77+
)
78+
79+
add_entrypoint_object(
80+
fesetenv
81+
SRCS
82+
fesetenv.cpp
83+
HDRS
84+
fesetenv.h
85+
DEPENDS
86+
libc.include.fenv
87+
libc.utils.FPUtil.fputil
88+
COMPILE_OPTIONS
89+
-O2
90+
)
91+
92+
add_entrypoint_object(
93+
fegetexceptflag
94+
SRCS
95+
fegetexceptflag.cpp
96+
HDRS
97+
fegetexceptflag.h
98+
DEPENDS
99+
libc.include.fenv
100+
libc.utils.FPUtil.fputil
101+
COMPILE_OPTIONS
102+
-O2
103+
)
104+
105+
add_entrypoint_object(
106+
fesetexceptflag
107+
SRCS
108+
fesetexceptflag.cpp
109+
HDRS
110+
fesetexceptflag.h
111+
DEPENDS
112+
libc.include.fenv
113+
libc.utils.FPUtil.fputil
114+
COMPILE_OPTIONS
115+
-O2
116+
)
117+
118+
add_entrypoint_object(
119+
feholdexcept
120+
SRCS
121+
feholdexcept.cpp
122+
HDRS
123+
feholdexcept.h
124+
DEPENDS
125+
libc.include.fenv
126+
libc.utils.FPUtil.fputil
127+
COMPILE_OPTIONS
128+
-O2
129+
)
130+
131+
add_entrypoint_object(
132+
feupdateenv
133+
SRCS
134+
feupdateenv.cpp
135+
HDRS
136+
feupdateenv.h
137+
DEPENDS
138+
libc.include.fenv
139+
libc.utils.FPUtil.fputil
140+
COMPILE_OPTIONS
141+
-O2
142+
)

libc/src/fenv/fegetenv.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of fegetenv function -----------------------------===//
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+
#include "src/fenv/fegetenv.h"
10+
#include "src/__support/common.h"
11+
#include "utils/FPUtil/FEnv.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(int, fegetenv, (fenv_t * envp)) {
16+
return fputil::getEnv(envp);
17+
}
18+
19+
} // namespace __llvm_libc

libc/src/fenv/fegetenv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for fegetenv ----------------------*- 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_FENV_FEGETENV_H
10+
#define LLVM_LIBC_SRC_FENV_FEGETENV_H
11+
12+
#include <fenv.h>
13+
14+
namespace __llvm_libc {
15+
16+
int fegetenv(fenv_t *);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_FENV_FEGETENV_H

libc/src/fenv/fegetexceptflag.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation of fegetexceptflag function ------------------------===//
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+
#include "src/fenv/fegetexceptflag.h"
10+
#include "src/__support/common.h"
11+
#include "utils/FPUtil/FEnv.h"
12+
13+
#include <fenv.h>
14+
15+
namespace __llvm_libc {
16+
17+
LLVM_LIBC_FUNCTION(int, fegetexceptflag, (fexcept_t * flagp, int excepts)) {
18+
// Since the return type of fetestexcept is int, we ensure that fexcept_t
19+
// matches in size.
20+
static_assert(sizeof(int) == sizeof(fexcept_t),
21+
"sizeof(fexcept_t) != sizeof(int)");
22+
*reinterpret_cast<int *>(flagp) = fputil::testExcept(FE_ALL_EXCEPT) & excepts;
23+
return 0;
24+
}
25+
26+
} // namespace __llvm_libc

libc/src/fenv/fegetexceptflag.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for fegetexceptflag ---------------*- 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_FENV_FEGETEXCEPTFLAG_H
10+
#define LLVM_LIBC_SRC_FENV_FEGETEXCEPTFLAG_H
11+
12+
#include <fenv.h>
13+
14+
namespace __llvm_libc {
15+
16+
int fegetexceptflag(fexcept_t *, int excepts);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_FENV_FEGETEXCEPTFLAG_H

libc/src/fenv/feholdexcept.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of feholdexcept function ---------------------------===//
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+
#include "src/fenv/feholdexcept.h"
10+
#include "src/__support/common.h"
11+
#include "utils/FPUtil/FEnv.h"
12+
13+
#include <fenv.h>
14+
15+
namespace __llvm_libc {
16+
17+
LLVM_LIBC_FUNCTION(int, feholdexcept, (fenv_t * envp)) {
18+
if (fputil::getEnv(envp) != 0)
19+
return -1;
20+
fputil::clearExcept(FE_ALL_EXCEPT);
21+
fputil::disableExcept(FE_ALL_EXCEPT);
22+
return 0;
23+
}
24+
25+
} // namespace __llvm_libc

libc/src/fenv/feholdexcept.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for feholdexcept ------------------*- 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_FENV_FEHOLDEXCEPT_H
10+
#define LLVM_LIBC_SRC_FENV_FEHOLDEXCEPT_H
11+
12+
#include <fenv.h>
13+
14+
namespace __llvm_libc {
15+
16+
int feholdexcept(fenv_t *);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_FENV_FEHOLDEXCEPT_H

libc/src/fenv/fesetenv.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of fesetenv function -----------------------------===//
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+
#include "src/fenv/fesetenv.h"
10+
#include "src/__support/common.h"
11+
#include "utils/FPUtil/FEnv.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(int, fesetenv, (const fenv_t *envp)) {
16+
return fputil::setEnv(envp);
17+
}
18+
19+
} // namespace __llvm_libc

libc/src/fenv/fesetenv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for fesetenv ----------------------*- 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_FENV_FESETENV_H
10+
#define LLVM_LIBC_SRC_FENV_FESETENV_H
11+
12+
#include <fenv.h>
13+
14+
namespace __llvm_libc {
15+
16+
int fesetenv(const fenv_t *);
17+
18+
} // namespace __llvm_libc
19+
20+
#endif // LLVM_LIBC_SRC_FENV_FESETENV_H

0 commit comments

Comments
 (0)