1356843Sdim//===-- FPEnv.cpp ---- FP Environment -------------------------------------===//
2356843Sdim//
3356843Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4356843Sdim// See https://llvm.org/LICENSE.txt for license information.
5356843Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6356843Sdim//
7356843Sdim//===----------------------------------------------------------------------===//
8356843Sdim//
9356843Sdim/// @file
10356843Sdim/// This file contains the implementations of entities that describe floating
11356843Sdim/// point environment.
12356843Sdim//
13356843Sdim//===----------------------------------------------------------------------===//
14356843Sdim
15356843Sdim#include "llvm/ADT/StringSwitch.h"
16356843Sdim#include "llvm/IR/FPEnv.h"
17356843Sdim
18356843Sdimnamespace llvm {
19356843Sdim
20356843SdimOptional<fp::RoundingMode> StrToRoundingMode(StringRef RoundingArg) {
21356843Sdim  // For dynamic rounding mode, we use round to nearest but we will set the
22356843Sdim  // 'exact' SDNodeFlag so that the value will not be rounded.
23356843Sdim  return StringSwitch<Optional<fp::RoundingMode>>(RoundingArg)
24356843Sdim      .Case("round.dynamic", fp::rmDynamic)
25356843Sdim      .Case("round.tonearest", fp::rmToNearest)
26356843Sdim      .Case("round.downward", fp::rmDownward)
27356843Sdim      .Case("round.upward", fp::rmUpward)
28356843Sdim      .Case("round.towardzero", fp::rmTowardZero)
29356843Sdim      .Default(None);
30356843Sdim}
31356843Sdim
32356843SdimOptional<StringRef> RoundingModeToStr(fp::RoundingMode UseRounding) {
33356843Sdim  Optional<StringRef> RoundingStr = None;
34356843Sdim  switch (UseRounding) {
35356843Sdim  case fp::rmDynamic:
36356843Sdim    RoundingStr = "round.dynamic";
37356843Sdim    break;
38356843Sdim  case fp::rmToNearest:
39356843Sdim    RoundingStr = "round.tonearest";
40356843Sdim    break;
41356843Sdim  case fp::rmDownward:
42356843Sdim    RoundingStr = "round.downward";
43356843Sdim    break;
44356843Sdim  case fp::rmUpward:
45356843Sdim    RoundingStr = "round.upward";
46356843Sdim    break;
47356843Sdim  case fp::rmTowardZero:
48356843Sdim    RoundingStr = "round.towardzero";
49356843Sdim    break;
50356843Sdim  }
51356843Sdim  return RoundingStr;
52356843Sdim}
53356843Sdim
54356843SdimOptional<fp::ExceptionBehavior> StrToExceptionBehavior(StringRef ExceptionArg) {
55356843Sdim  return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg)
56356843Sdim      .Case("fpexcept.ignore", fp::ebIgnore)
57356843Sdim      .Case("fpexcept.maytrap", fp::ebMayTrap)
58356843Sdim      .Case("fpexcept.strict", fp::ebStrict)
59356843Sdim      .Default(None);
60356843Sdim}
61356843Sdim
62356843SdimOptional<StringRef> ExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
63356843Sdim  Optional<StringRef> ExceptStr = None;
64356843Sdim  switch (UseExcept) {
65356843Sdim  case fp::ebStrict:
66356843Sdim    ExceptStr = "fpexcept.strict";
67356843Sdim    break;
68356843Sdim  case fp::ebIgnore:
69356843Sdim    ExceptStr = "fpexcept.ignore";
70356843Sdim    break;
71356843Sdim  case fp::ebMayTrap:
72356843Sdim    ExceptStr = "fpexcept.maytrap";
73356843Sdim    break;
74356843Sdim  }
75356843Sdim  return ExceptStr;
76356843Sdim}
77356843Sdim
78356843Sdim}