@@ -16,8 +16,7 @@ use std::fs::File;
16
16
use std:: io:: Write ;
17
17
use std:: path:: Path ;
18
18
19
- use common_expression:: types:: DataType ;
20
- use common_expression:: types:: NumberTypeInfo ;
19
+ use common_expression:: types:: NumberDataType ;
21
20
22
21
pub enum OP {
23
22
Plus ,
@@ -35,6 +34,7 @@ pub fn codegen_arithmetic_type_v2() {
35
34
let path = dest. join ( "arithmetics_type.rs" ) ;
36
35
37
36
let mut file = File :: create ( & path) . expect ( "open" ) ;
37
+ let codegen_src_path = file ! ( ) ;
38
38
// Write the head.
39
39
writeln ! (
40
40
file,
@@ -52,7 +52,10 @@ pub fn codegen_arithmetic_type_v2() {
52
52
// See the License for the specific language governing permissions and
53
53
// limitations under the License.
54
54
55
- // This code is generated by common/codegen. DO NOT EDIT.
55
+ // This code is generated by {codegen_src_path}. DO NOT EDIT.
56
+
57
+ use ordered_float::OrderedFloat;
58
+
56
59
use super::number::Number;
57
60
58
61
pub trait ResultTypeOfBinary: Sized {{
@@ -79,30 +82,26 @@ pub trait ResultTypeOfUnary: Sized {{
79
82
)
80
83
. unwrap ( ) ;
81
84
82
- let lhs = vec ! [
83
- DataType :: UInt8 ,
84
- DataType :: UInt16 ,
85
- DataType :: UInt32 ,
86
- DataType :: UInt64 ,
87
- DataType :: Int8 ,
88
- DataType :: Int16 ,
89
- DataType :: Int32 ,
90
- DataType :: Int64 ,
91
- DataType :: Float32 ,
92
- DataType :: Float64 ,
85
+ let number_types = vec ! [
86
+ NumberDataType :: UInt8 ,
87
+ NumberDataType :: UInt16 ,
88
+ NumberDataType :: UInt32 ,
89
+ NumberDataType :: UInt64 ,
90
+ NumberDataType :: Int8 ,
91
+ NumberDataType :: Int16 ,
92
+ NumberDataType :: Int32 ,
93
+ NumberDataType :: Int64 ,
94
+ NumberDataType :: Float32 ,
95
+ NumberDataType :: Float64 ,
93
96
] ;
94
- let rhs = lhs. clone ( ) ;
95
97
96
- for a in & lhs {
97
- for b in & rhs {
98
- let left = a. number_type_info ( ) . unwrap ( ) ;
99
- let right = b. number_type_info ( ) . unwrap ( ) ;
100
-
101
- let add_mul = arithmetic_coercion ( left, right, OP :: Plus ) ;
102
- let minus = arithmetic_coercion ( left, right, OP :: Minus ) ;
103
- let intdiv = arithmetic_coercion ( left, right, OP :: IntDiv ) ;
104
- let modulo = arithmetic_coercion ( left, right, OP :: Modulo ) ;
105
- let least_super = arithmetic_coercion ( left, right, OP :: Super ) ;
98
+ for lhs in & number_types {
99
+ for rhs in & number_types {
100
+ let add_mul = arithmetic_coercion ( * lhs, * rhs, OP :: Plus ) ;
101
+ let minus = arithmetic_coercion ( * lhs, * rhs, OP :: Minus ) ;
102
+ let intdiv = arithmetic_coercion ( * lhs, * rhs, OP :: IntDiv ) ;
103
+ let modulo = arithmetic_coercion ( * lhs, * rhs, OP :: Modulo ) ;
104
+ let least_super = arithmetic_coercion ( * lhs, * rhs, OP :: Super ) ;
106
105
107
106
writeln ! (
108
107
file,
@@ -114,8 +113,8 @@ impl ResultTypeOfBinary for ({}, {}) {{
114
113
type Modulo = {};
115
114
type LeastSuper = {};
116
115
}}" ,
117
- to_primitive_str( a . clone( ) ) ,
118
- to_primitive_str( b . clone( ) ) ,
116
+ to_primitive_str( lhs . clone( ) ) ,
117
+ to_primitive_str( rhs . clone( ) ) ,
119
118
to_primitive_str( add_mul) ,
120
119
to_primitive_str( minus) ,
121
120
to_primitive_str( intdiv) ,
@@ -126,11 +125,11 @@ impl ResultTypeOfBinary for ({}, {}) {{
126
125
}
127
126
}
128
127
129
- for arg in & lhs {
130
- let negate = neg_coercion ( arg. number_type_info ( ) . unwrap ( ) ) ;
128
+ for arg in & number_types {
129
+ let negate = neg_coercion ( * arg) ;
131
130
132
131
match negate {
133
- DataType :: Float32 | DataType :: Float64 => {
132
+ NumberDataType :: Float32 | NumberDataType :: Float64 => {
134
133
writeln ! (
135
134
file,
136
135
"
@@ -200,97 +199,57 @@ impl ResultTypeOfUnary for {} {{
200
199
file. flush ( ) . unwrap ( ) ;
201
200
}
202
201
203
- fn to_primitive_str ( dt : DataType ) -> & ' static str {
202
+ fn to_primitive_str ( dt : NumberDataType ) -> & ' static str {
204
203
match dt {
205
- DataType :: UInt8 => "u8" ,
206
- DataType :: UInt16 => "u16" ,
207
- DataType :: UInt32 => "u32" ,
208
- DataType :: UInt64 => "u64" ,
209
- DataType :: Int8 => "i8" ,
210
- DataType :: Int16 => "i16" ,
211
- DataType :: Int32 => "i32" ,
212
- DataType :: Int64 => "i64" ,
213
- DataType :: Float32 => "f32" ,
214
- DataType :: Float64 => "f64" ,
215
- _ => panic ! ( "unsupported data type" ) ,
204
+ NumberDataType :: UInt8 => "u8" ,
205
+ NumberDataType :: UInt16 => "u16" ,
206
+ NumberDataType :: UInt32 => "u32" ,
207
+ NumberDataType :: UInt64 => "u64" ,
208
+ NumberDataType :: Int8 => "i8" ,
209
+ NumberDataType :: Int16 => "i16" ,
210
+ NumberDataType :: Int32 => "i32" ,
211
+ NumberDataType :: Int64 => "i64" ,
212
+ NumberDataType :: Float32 => "OrderedFloat<f32>" ,
213
+ NumberDataType :: Float64 => "OrderedFloat<f64>" ,
216
214
}
217
215
}
218
216
219
- fn arithmetic_coercion ( a : NumberTypeInfo , b : NumberTypeInfo , op : OP ) -> DataType {
220
- let is_signed = a. is_signed || b. is_signed ;
221
- let is_float = a. is_float || b. is_float ;
222
- let bit_width = a. bit_width . max ( b. bit_width ) ;
217
+ fn arithmetic_coercion ( a : NumberDataType , b : NumberDataType , op : OP ) -> NumberDataType {
218
+ let is_signed = a. is_signed ( ) || b. is_signed ( ) ;
219
+ let is_float = a. is_float ( ) || b. is_float ( ) ;
220
+ let bit_width = a. bit_width ( ) . max ( b. bit_width ( ) ) ;
223
221
224
222
match op {
225
- OP :: Plus | OP :: Mul => {
226
- let info = NumberTypeInfo {
227
- is_signed,
228
- is_float,
229
- bit_width : next_bit_width ( bit_width) ,
230
- } ;
231
-
232
- DataType :: new_number ( info)
233
- }
223
+ OP :: Plus | OP :: Mul => NumberDataType :: new ( next_bit_width ( bit_width) , is_signed, is_float) ,
234
224
OP :: Modulo => {
235
225
if is_float {
236
- return DataType :: Float64 ;
226
+ return NumberDataType :: Float64 ;
237
227
}
238
- let result_is_signed = a. is_signed ;
239
- let right_size = b. bit_width ;
228
+ let result_is_signed = a. is_signed ( ) ;
229
+ let right_size = b. bit_width ( ) ;
240
230
let size_of_result = if result_is_signed {
241
231
next_bit_width ( right_size)
242
232
} else {
243
233
right_size
244
234
} ;
245
235
246
- let info = NumberTypeInfo {
247
- is_signed : result_is_signed,
248
- is_float : false ,
249
- bit_width : size_of_result,
250
- } ;
251
- DataType :: new_number ( info)
252
- }
253
- OP :: Minus => {
254
- let info = NumberTypeInfo {
255
- is_signed : true ,
256
- is_float,
257
- bit_width : next_bit_width ( bit_width) ,
258
- } ;
259
- DataType :: new_number ( info)
260
- }
261
- OP :: Div => DataType :: Float64 ,
262
- OP :: IntDiv => {
263
- let info = NumberTypeInfo {
264
- is_signed,
265
- is_float : false ,
266
- bit_width,
267
- } ;
268
- DataType :: new_number ( info)
269
- }
270
- OP :: Super => {
271
- let info = NumberTypeInfo {
272
- is_signed,
273
- is_float,
274
- bit_width,
275
- } ;
276
- DataType :: new_number ( info)
236
+ NumberDataType :: new ( size_of_result, result_is_signed, false )
277
237
}
238
+ OP :: Minus => NumberDataType :: new ( next_bit_width ( bit_width) , true , is_float) ,
239
+ OP :: Div => NumberDataType :: Float64 ,
240
+ OP :: IntDiv => NumberDataType :: new ( bit_width, is_signed, false ) ,
241
+ OP :: Super => NumberDataType :: new ( bit_width, is_signed, is_float) ,
278
242
}
279
243
}
280
244
281
- fn neg_coercion ( a : NumberTypeInfo ) -> DataType {
282
- let bit_width = if a. is_signed {
283
- a. bit_width
245
+ fn neg_coercion ( a : NumberDataType ) -> NumberDataType {
246
+ let bit_width = if a. is_signed ( ) {
247
+ a. bit_width ( )
284
248
} else {
285
- next_bit_width ( a. bit_width )
249
+ next_bit_width ( a. bit_width ( ) )
286
250
} ;
287
251
288
- let info = NumberTypeInfo {
289
- is_float : a. is_float ,
290
- is_signed : true ,
291
- bit_width,
292
- } ;
293
- DataType :: new_number ( info)
252
+ NumberDataType :: new ( bit_width, true , a. is_float ( ) )
294
253
}
295
254
296
255
const fn next_bit_width ( width : u8 ) -> u8 {
0 commit comments