1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef BERBERIS_INTRINSICS_GUEST_FPSTATE_H_ 18 #define BERBERIS_INTRINSICS_GUEST_FPSTATE_H_ 19 20 // This interface allows to work with guest's fp-environment 21 // portion that is reflected in hosts' fp-environment. 22 // TODO(levarum): Rename file to reflect this. 23 24 #include <cfenv> // FE_TONEAREST and friends. 25 #include <cstdint> 26 27 namespace berberis { 28 29 // Special rounding mode value to tell intrinsics 30 // and interpreter to use rounding mode stored on host. 31 const uint32_t FE_HOSTROUND = static_cast<uint32_t>(-1); 32 const uint32_t FE_TIESAWAY = static_cast<uint32_t>(-2); 33 static_assert(FE_HOSTROUND != FE_TONEAREST); 34 static_assert(FE_HOSTROUND != FE_UPWARD); 35 static_assert(FE_HOSTROUND != FE_DOWNWARD); 36 static_assert(FE_HOSTROUND != FE_TOWARDZERO); 37 static_assert(FE_TIESAWAY != FE_TONEAREST); 38 static_assert(FE_TIESAWAY != FE_UPWARD); 39 static_assert(FE_TIESAWAY != FE_DOWNWARD); 40 static_assert(FE_TIESAWAY != FE_TOWARDZERO); 41 42 class ScopedRoundingMode { 43 public: ScopedRoundingMode()44 ScopedRoundingMode() : saved_round_mode(std::fegetround()) {} ScopedRoundingMode(int rm)45 ScopedRoundingMode(int rm) : saved_round_mode(std::fegetround()) { std::fesetround(rm); } ~ScopedRoundingMode()46 ~ScopedRoundingMode() { std::fesetround(saved_round_mode); } 47 48 private: 49 int saved_round_mode; 50 }; 51 52 } // namespace berberis 53 54 #endif // BERBERIS_INTRINSICS_GUEST_FPSTATE_H_ 55