xref: /llvm-project/flang/unittests/Optimizer/Builder/Runtime/NumericTest.cpp (revision 4cdc19b84cf48189e40915178d60991f6fc469b2)
1 //===- NumericTest.cpp -- Numeric intrinsic runtime builder unit tests ----===//
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 #include "flang/Optimizer/Builder/Runtime/Numeric.h"
10 #include "RuntimeCallTestBase.h"
11 #include "gtest/gtest.h"
12 
13 using namespace mlir;
14 
15 void testGenExponent(fir::FirOpBuilder &builder, mlir::Type resultType,
16     mlir::Type xType, llvm::StringRef fctName) {
17   auto loc = builder.getUnknownLoc();
18   mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
19   mlir::Value exp = fir::runtime::genExponent(builder, loc, resultType, x);
20   checkCallOp(exp.getDefiningOp(), fctName, 1, /*addLocArg=*/false);
21 }
22 
23 TEST_F(RuntimeCallTest, genExponentTest) {
24   testGenExponent(*firBuilder, i32Ty, f32Ty, "_FortranAExponent4_4");
25   testGenExponent(*firBuilder, i64Ty, f32Ty, "_FortranAExponent4_8");
26   testGenExponent(*firBuilder, i32Ty, f64Ty, "_FortranAExponent8_4");
27   testGenExponent(*firBuilder, i64Ty, f64Ty, "_FortranAExponent8_8");
28   testGenExponent(*firBuilder, i32Ty, f80Ty, "_FortranAExponent10_4");
29   testGenExponent(*firBuilder, i64Ty, f80Ty, "_FortranAExponent10_8");
30   testGenExponent(*firBuilder, i32Ty, f128Ty, "_FortranAExponent16_4");
31   testGenExponent(*firBuilder, i64Ty, f128Ty, "_FortranAExponent16_8");
32 }
33 
34 void testGenX(fir::FirOpBuilder &builder, mlir::Type xType,
35     mlir::Value (*genFct)(fir::FirOpBuilder &, Location, mlir::Value),
36     llvm::StringRef fctName) {
37   auto loc = builder.getUnknownLoc();
38   mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
39   mlir::Value val = genFct(builder, loc, x);
40   checkCallOp(val.getDefiningOp(), fctName, 1, /*addLocArg=*/false);
41 }
42 
43 TEST_F(RuntimeCallTest, genFractionTest) {
44   testGenX(*firBuilder, f32Ty, fir::runtime::genFraction, "_FortranAFraction4");
45   testGenX(*firBuilder, f64Ty, fir::runtime::genFraction, "_FortranAFraction8");
46   testGenX(
47       *firBuilder, f80Ty, fir::runtime::genFraction, "_FortranAFraction10");
48   testGenX(
49       *firBuilder, f128Ty, fir::runtime::genFraction, "_FortranAFraction16");
50 }
51 
52 void testGenNearest(fir::FirOpBuilder &builder, mlir::Type xType,
53     mlir::Type sType, llvm::StringRef fctName) {
54   auto loc = builder.getUnknownLoc();
55   mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
56   mlir::Value s = builder.create<fir::UndefOp>(loc, sType);
57   mlir::Value nearest = fir::runtime::genNearest(builder, loc, x, s);
58   checkCallOp(nearest.getDefiningOp(), fctName, 2, /*addLocArg=*/false);
59 }
60 
61 TEST_F(RuntimeCallTest, genNearestTest) {
62   testGenNearest(*firBuilder, f32Ty, f32Ty, "_FortranANearest4");
63   testGenNearest(*firBuilder, f64Ty, f32Ty, "_FortranANearest8");
64   testGenNearest(*firBuilder, f80Ty, f32Ty, "_FortranANearest10");
65   testGenNearest(*firBuilder, f128Ty, f32Ty, "_FortranANearest16");
66 }
67 
68 TEST_F(RuntimeCallTest, genRRSpacingTest) {
69   testGenX(
70       *firBuilder, f32Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing4");
71   testGenX(
72       *firBuilder, f64Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing8");
73   testGenX(
74       *firBuilder, f80Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing10");
75   testGenX(
76       *firBuilder, f128Ty, fir::runtime::genRRSpacing, "_FortranARRSpacing16");
77 }
78 
79 void testGenXI(fir::FirOpBuilder &builder, mlir::Type xType, mlir::Type iType,
80     mlir::Value (*genFct)(
81         fir::FirOpBuilder &, Location, mlir::Value, mlir::Value),
82     llvm::StringRef fctName) {
83   auto loc = builder.getUnknownLoc();
84   mlir::Value x = builder.create<fir::UndefOp>(loc, xType);
85   mlir::Value i = builder.create<fir::UndefOp>(loc, iType);
86   mlir::Value val = genFct(builder, loc, x, i);
87   checkCallOp(val.getDefiningOp(), fctName, 2, /*addLocArg=*/false);
88 }
89 
90 TEST_F(RuntimeCallTest, genScaleTest) {
91   testGenXI(
92       *firBuilder, f32Ty, f32Ty, fir::runtime::genScale, "_FortranAScale4");
93   testGenXI(
94       *firBuilder, f64Ty, f32Ty, fir::runtime::genScale, "_FortranAScale8");
95   testGenXI(
96       *firBuilder, f80Ty, f32Ty, fir::runtime::genScale, "_FortranAScale10");
97   testGenXI(
98       *firBuilder, f128Ty, f32Ty, fir::runtime::genScale, "_FortranAScale16");
99 }
100 
101 TEST_F(RuntimeCallTest, genSetExponentTest) {
102   testGenXI(*firBuilder, f32Ty, f32Ty, fir::runtime::genSetExponent,
103       "_FortranASetExponent4");
104   testGenXI(*firBuilder, f64Ty, f32Ty, fir::runtime::genSetExponent,
105       "_FortranASetExponent8");
106   testGenXI(*firBuilder, f80Ty, f32Ty, fir::runtime::genSetExponent,
107       "_FortranASetExponent10");
108   testGenXI(*firBuilder, f128Ty, f32Ty, fir::runtime::genSetExponent,
109       "_FortranASetExponent16");
110 }
111 
112 TEST_F(RuntimeCallTest, genSpacingTest) {
113   testGenX(*firBuilder, f32Ty, fir::runtime::genSpacing, "_FortranASpacing4");
114   testGenX(*firBuilder, f64Ty, fir::runtime::genSpacing, "_FortranASpacing8");
115   testGenX(*firBuilder, f80Ty, fir::runtime::genSpacing, "_FortranASpacing10");
116   testGenX(*firBuilder, f128Ty, fir::runtime::genSpacing, "_FortranASpacing16");
117 }
118