Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 1 | //===-- FPEnv.cpp ---- FP Environment -------------------------------------===// |
| 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 | /// @file |
| 10 | /// This file contains the implementations of entities that describe floating |
| 11 | /// point environment. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/IR/FPEnv.h" |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 20 | Optional<RoundingMode> StrToRoundingMode(StringRef RoundingArg) { |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 21 | // For dynamic rounding mode, we use round to nearest but we will set the |
| 22 | // 'exact' SDNodeFlag so that the value will not be rounded. |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 23 | return StringSwitch<Optional<RoundingMode>>(RoundingArg) |
| 24 | .Case("round.dynamic", RoundingMode::Dynamic) |
| 25 | .Case("round.tonearest", RoundingMode::NearestTiesToEven) |
| 26 | .Case("round.tonearestaway", RoundingMode::NearestTiesToAway) |
| 27 | .Case("round.downward", RoundingMode::TowardNegative) |
| 28 | .Case("round.upward", RoundingMode::TowardPositive) |
| 29 | .Case("round.towardzero", RoundingMode::TowardZero) |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 30 | .Default(None); |
| 31 | } |
| 32 | |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 33 | Optional<StringRef> RoundingModeToStr(RoundingMode UseRounding) { |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 34 | Optional<StringRef> RoundingStr = None; |
| 35 | switch (UseRounding) { |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 36 | case RoundingMode::Dynamic: |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 37 | RoundingStr = "round.dynamic"; |
| 38 | break; |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 39 | case RoundingMode::NearestTiesToEven: |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 40 | RoundingStr = "round.tonearest"; |
| 41 | break; |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 42 | case RoundingMode::NearestTiesToAway: |
| 43 | RoundingStr = "round.tonearestaway"; |
| 44 | break; |
| 45 | case RoundingMode::TowardNegative: |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 46 | RoundingStr = "round.downward"; |
| 47 | break; |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 48 | case RoundingMode::TowardPositive: |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 49 | RoundingStr = "round.upward"; |
| 50 | break; |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 51 | case RoundingMode::TowardZero: |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 52 | RoundingStr = "round.towardzero"; |
| 53 | break; |
Serge Pavlov | c7ff5b3 | 2020-03-26 14:51:09 +0700 | [diff] [blame^] | 54 | default: |
| 55 | break; |
Serge Pavlov | ea8678d | 2019-08-29 19:29:11 +0700 | [diff] [blame] | 56 | } |
| 57 | return RoundingStr; |
| 58 | } |
| 59 | |
| 60 | Optional<fp::ExceptionBehavior> StrToExceptionBehavior(StringRef ExceptionArg) { |
| 61 | return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg) |
| 62 | .Case("fpexcept.ignore", fp::ebIgnore) |
| 63 | .Case("fpexcept.maytrap", fp::ebMayTrap) |
| 64 | .Case("fpexcept.strict", fp::ebStrict) |
| 65 | .Default(None); |
| 66 | } |
| 67 | |
| 68 | Optional<StringRef> ExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) { |
| 69 | Optional<StringRef> ExceptStr = None; |
| 70 | switch (UseExcept) { |
| 71 | case fp::ebStrict: |
| 72 | ExceptStr = "fpexcept.strict"; |
| 73 | break; |
| 74 | case fp::ebIgnore: |
| 75 | ExceptStr = "fpexcept.ignore"; |
| 76 | break; |
| 77 | case fp::ebMayTrap: |
| 78 | ExceptStr = "fpexcept.maytrap"; |
| 79 | break; |
| 80 | } |
| 81 | return ExceptStr; |
| 82 | } |
Sven van Haastregt | 665dcda | 2020-02-12 15:57:25 +0000 | [diff] [blame] | 83 | } |