FPEnv.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "llvm/IR/FPEnv.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. namespace llvm {
  17. Optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
  18. // For dynamic rounding mode, we use round to nearest but we will set the
  19. // 'exact' SDNodeFlag so that the value will not be rounded.
  20. return StringSwitch<Optional<RoundingMode>>(RoundingArg)
  21. .Case("round.dynamic", RoundingMode::Dynamic)
  22. .Case("round.tonearest", RoundingMode::NearestTiesToEven)
  23. .Case("round.tonearestaway", RoundingMode::NearestTiesToAway)
  24. .Case("round.downward", RoundingMode::TowardNegative)
  25. .Case("round.upward", RoundingMode::TowardPositive)
  26. .Case("round.towardzero", RoundingMode::TowardZero)
  27. .Default(None);
  28. }
  29. Optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
  30. Optional<StringRef> RoundingStr = None;
  31. switch (UseRounding) {
  32. case RoundingMode::Dynamic:
  33. RoundingStr = "round.dynamic";
  34. break;
  35. case RoundingMode::NearestTiesToEven:
  36. RoundingStr = "round.tonearest";
  37. break;
  38. case RoundingMode::NearestTiesToAway:
  39. RoundingStr = "round.tonearestaway";
  40. break;
  41. case RoundingMode::TowardNegative:
  42. RoundingStr = "round.downward";
  43. break;
  44. case RoundingMode::TowardPositive:
  45. RoundingStr = "round.upward";
  46. break;
  47. case RoundingMode::TowardZero:
  48. RoundingStr = "round.towardzero";
  49. break;
  50. default:
  51. break;
  52. }
  53. return RoundingStr;
  54. }
  55. Optional<fp::ExceptionBehavior>
  56. convertStrToExceptionBehavior(StringRef ExceptionArg) {
  57. return StringSwitch<Optional<fp::ExceptionBehavior>>(ExceptionArg)
  58. .Case("fpexcept.ignore", fp::ebIgnore)
  59. .Case("fpexcept.maytrap", fp::ebMayTrap)
  60. .Case("fpexcept.strict", fp::ebStrict)
  61. .Default(None);
  62. }
  63. Optional<StringRef>
  64. convertExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
  65. Optional<StringRef> ExceptStr = None;
  66. switch (UseExcept) {
  67. case fp::ebStrict:
  68. ExceptStr = "fpexcept.strict";
  69. break;
  70. case fp::ebIgnore:
  71. ExceptStr = "fpexcept.ignore";
  72. break;
  73. case fp::ebMayTrap:
  74. ExceptStr = "fpexcept.maytrap";
  75. break;
  76. }
  77. return ExceptStr;
  78. }
  79. } // namespace llvm