xref: /freebsd/contrib/llvm-project/llvm/lib/IR/FPEnv.cpp (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
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/IR/FPEnv.h"
16 #include "llvm/ADT/StringSwitch.h"
17 
18 namespace llvm {
19 
20 Optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
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.
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)
30       .Default(None);
31 }
32 
33 Optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
34   Optional<StringRef> RoundingStr = None;
35   switch (UseRounding) {
36   case RoundingMode::Dynamic:
37     RoundingStr = "round.dynamic";
38     break;
39   case RoundingMode::NearestTiesToEven:
40     RoundingStr = "round.tonearest";
41     break;
42   case RoundingMode::NearestTiesToAway:
43     RoundingStr = "round.tonearestaway";
44     break;
45   case RoundingMode::TowardNegative:
46     RoundingStr = "round.downward";
47     break;
48   case RoundingMode::TowardPositive:
49     RoundingStr = "round.upward";
50     break;
51   case RoundingMode::TowardZero:
52     RoundingStr = "round.towardzero";
53     break;
54   default:
55     break;
56   }
57   return RoundingStr;
58 }
59 
60 Optional<fp::ExceptionBehavior>
61 convertStrToExceptionBehavior(StringRef ExceptionArg) {
62   return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg)
63       .Case("fpexcept.ignore", fp::ebIgnore)
64       .Case("fpexcept.maytrap", fp::ebMayTrap)
65       .Case("fpexcept.strict", fp::ebStrict)
66       .Default(None);
67 }
68 
69 Optional<StringRef>
70 convertExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
71   Optional<StringRef> ExceptStr = None;
72   switch (UseExcept) {
73   case fp::ebStrict:
74     ExceptStr = "fpexcept.strict";
75     break;
76   case fp::ebIgnore:
77     ExceptStr = "fpexcept.ignore";
78     break;
79   case fp::ebMayTrap:
80     ExceptStr = "fpexcept.maytrap";
81     break;
82   }
83   return ExceptStr;
84 }
85 } // namespace llvm
86