1 //===-- RoundingModeUtils.cpp ---------------------------------------------===// 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 "RoundingModeUtils.h" 10 11 #include <fenv.h> 12 13 namespace __llvm_libc { 14 namespace testutils { 15 get_fe_rounding(RoundingMode mode)16int get_fe_rounding(RoundingMode mode) { 17 switch (mode) { 18 case RoundingMode::Upward: 19 return FE_UPWARD; 20 break; 21 case RoundingMode::Downward: 22 return FE_DOWNWARD; 23 break; 24 case RoundingMode::TowardZero: 25 return FE_TOWARDZERO; 26 break; 27 case RoundingMode::Nearest: 28 return FE_TONEAREST; 29 break; 30 } 31 } 32 ForceRoundingMode(RoundingMode mode)33ForceRoundingMode::ForceRoundingMode(RoundingMode mode) { 34 old_rounding_mode = fegetround(); 35 rounding_mode = get_fe_rounding(mode); 36 if (old_rounding_mode != rounding_mode) 37 fesetround(rounding_mode); 38 } 39 ~ForceRoundingMode()40ForceRoundingMode::~ForceRoundingMode() { 41 if (old_rounding_mode != rounding_mode) 42 fesetround(old_rounding_mode); 43 } 44 45 } // namespace testutils 46 } // namespace __llvm_libc 47