1b7e05c87SSiva Chandra Reddy //===-- Implementation of fesetexceptflag function ------------------------===//
2b7e05c87SSiva Chandra Reddy //
3b7e05c87SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b7e05c87SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
5b7e05c87SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b7e05c87SSiva Chandra Reddy //
7b7e05c87SSiva Chandra Reddy //===----------------------------------------------------------------------===//
8b7e05c87SSiva Chandra Reddy 
9b7e05c87SSiva Chandra Reddy #include "src/fenv/fesetexceptflag.h"
1076ec69a9STue Ly #include "src/__support/FPUtil/FEnvImpl.h"
11b7e05c87SSiva Chandra Reddy #include "src/__support/common.h"
12b7e05c87SSiva Chandra Reddy 
13b7e05c87SSiva Chandra Reddy #include <fenv.h>
14b7e05c87SSiva Chandra Reddy 
15b7e05c87SSiva Chandra Reddy namespace __llvm_libc {
16b7e05c87SSiva Chandra Reddy 
17b7e05c87SSiva Chandra Reddy LLVM_LIBC_FUNCTION(int, fesetexceptflag,
18b7e05c87SSiva Chandra Reddy                    (const fexcept_t *flagp, int excepts)) {
19b7e05c87SSiva Chandra Reddy   // Since the return type of fetestexcept is int, we ensure that fexcept_t
209550f8baSSiva Chandra Reddy   // can fit in int type.
219550f8baSSiva Chandra Reddy   static_assert(sizeof(int) >= sizeof(fexcept_t),
229550f8baSSiva Chandra Reddy                 "fexcept_t value cannot fit in an int value.");
23*40a55fffSAlex Brachet   int excepts_to_set = static_cast<int>(*flagp) & excepts;
241c92911eSMichael Jones   fputil::clear_except(FE_ALL_EXCEPT);
251c92911eSMichael Jones   return fputil::set_except(excepts_to_set);
26b7e05c87SSiva Chandra Reddy }
27b7e05c87SSiva Chandra Reddy 
28b7e05c87SSiva Chandra Reddy } // namespace __llvm_libc
29