1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package oracle.code.triton;
 25 
 26 import oracle.code.triton.TritonTestExtension.TritonTestData;
 27 import org.junit.jupiter.api.Test;
 28 import org.junit.jupiter.api.extension.ExtendWith;
 29 
 30 import java.lang.reflect.code.TypeElement;
 31 import java.lang.reflect.code.type.JavaType;
 32 import java.lang.runtime.CodeReflection;
 33 import java.util.List;
 34 
 35 import static oracle.code.triton.Triton.*;
 36 import static oracle.code.triton.TritonTest.consume;
 37 
 38 @ExtendWith(TritonTestExtension.class)
 39 public class TestCdiv {
 40 
 41     @TritonCodeModel("""
 42             module ()void -> {
 43                 tt.func @"cdiv_int_int_int" (%0 : int, %1 : int)int -> {
 44                     %2 : int = arith.addi %0 %1;
 45                     %3 : int = arith.constant @"1";
 46                     %4 : int = arith.subi %2 %3;
 47                     %5 : int = arith.divsi %4 %1;
 48                     tt.return %5;
 49                 };
 50                 tt.func @"testScalar_int_int_void" (%6 : int, %7 : int)void -> {
 51                     %8 : int = tt.call %6 %7 @"cdiv_int_int_int";
 52                     tt.consume %8;
 53                     tt.return;
 54                 };
 55                 unreachable;
 56             };
 57             """)
 58     @CodeReflection
 59     static void testScalar(int a, int b) {
 60         var r1 = cdiv(a, b);
 61         consume(r1);
 62     }
 63 
 64     @Test
 65     public void testScalar(TritonTestData t) {
 66         List<TypeElement> argTypes = List.of(
 67                 JavaType.INT,
 68                 JavaType.INT);
 69 
 70         t.test(argTypes);
 71     }
 72 
 73 
 74     @TritonCodeModel("""
 75             module ()void -> {
 76                 tt.func @"cdiv_int_10_int" (%0 : int)int -> {
 77                     %1 : int = arith.constant @"10";
 78                     %2 : int = arith.addi %0 %1;
 79                     %3 : int = arith.constant @"1";
 80                     %4 : int = arith.subi %2 %3;
 81                     %5 : int = arith.divsi %4 %1;
 82                     tt.return %5;
 83                 };
 84                 tt.func @"testConstant_int_10_void" (%6 : int)void -> {
 85                     %7 : int = tt.call %6 @"cdiv_int_10_int";
 86                     tt.consume %7;
 87                     tt.return;
 88                 };
 89                 unreachable;
 90             };
 91             """)
 92     @CodeReflection
 93     static void testConstant(int a, int b) {
 94         var r1 = cdiv(a, b);
 95         consume(r1);
 96     }
 97 
 98     @Test
 99     public void testConstant(TritonTestData t) {
100         List<TypeElement> argTypes = List.of(
101                 JavaType.INT,
102                 new ConstantType(JavaType.INT, 10));
103 
104         t.test(argTypes);
105     }
106 
107 
108     @TritonCodeModel("""
109             module ()void -> {
110                 tt.func @"cdiv_int_int_int" (%0 : int, %1 : int)int -> {
111                     %2 : int = arith.addi %0 %1;
112                     %3 : int = arith.constant @"1";
113                     %4 : int = arith.subi %2 %3;
114                     %5 : int = arith.divsi %4 %1;
115                     tt.return %5;
116                 };
117                 tt.func @"cdiv_int_10_int" (%6 : int)int -> {
118                     %7 : int = arith.constant @"10";
119                     %8 : int = arith.addi %6 %7;
120                     %9 : int = arith.constant @"1";
121                     %10 : int = arith.subi %8 %9;
122                     %11 : int = arith.divsi %10 %7;
123                     tt.return %11;
124                 };
125                 tt.func @"cdiv_10_int_int" (%12 : int)int -> {
126                     %13 : int = arith.constant @"10";
127                     %14 : int = arith.addi %13 %12;
128                     %15 : int = arith.constant @"1";
129                     %16 : int = arith.subi %14 %15;
130                     %17 : int = arith.divsi %16 %12;
131                     tt.return %17;
132                 };
133                 tt.func @"testCalls_int_int_10_void" (%18 : int, %19 : int)void -> {
134                     %20 : int = tt.call %18 %19 @"cdiv_int_int_int";
135                     tt.consume %20;
136                     %21 : int = tt.call %19 %18 @"cdiv_int_int_int";
137                     tt.consume %21;
138                     %22 : int = tt.call %18 @"cdiv_int_10_int";
139                     tt.consume %22;
140                     %23 : int = tt.call %18 @"cdiv_10_int_int";
141                     tt.consume %23;
142                     tt.return;
143                 };
144                 unreachable;
145             };
146             """)
147     @CodeReflection
148     static void testCalls(int a, int b, int c) {
149         consume(cdiv(a, b));
150         consume(cdiv(b, a));
151         consume(cdiv(a, c));
152         consume(cdiv(c, a));
153     }
154 
155     @Test
156     public void testCalls(TritonTestData t) {
157         List<TypeElement> argTypes = List.of(
158                 JavaType.INT,
159                 JavaType.INT,
160                 new ConstantType(JavaType.INT, 10));
161 
162         t.test(argTypes);
163     }
164 }