Skip to content

Commit

Permalink
refactor: use int_type_var(usize) to simplify int_ops and conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Nov 13, 2023
1 parent 3fb2707 commit 0063b46
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 153 deletions.
35 changes: 14 additions & 21 deletions src/std_extensions/arithmetic/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@ use crate::{
PRELUDE,
},
type_row,
types::{FunctionType, PolyFuncType, Type},
types::{FunctionType, PolyFuncType},
Extension,
};

use super::int_types::{int_type_var, INT_TYPE_ID};
use super::int_types::int_type_var;
use super::{float_types::FLOAT64_TYPE, int_types::LOG_WIDTH_TYPE_PARAM};

/// The extension identifier.
pub const EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("arithmetic.conversions");

fn ftoi_sig(
int_type_var: Type,
temp_reg: &ExtensionRegistry,
) -> Result<PolyFuncType, SignatureError> {
let body = FunctionType::new(type_row![FLOAT64_TYPE], vec![sum_with_error(int_type_var)]);
fn ftoi_sig(temp_reg: &ExtensionRegistry) -> Result<PolyFuncType, SignatureError> {
let body = FunctionType::new(
type_row![FLOAT64_TYPE],
vec![sum_with_error(int_type_var(0))],
);

PolyFuncType::new_validated(vec![LOG_WIDTH_TYPE_PARAM], body, temp_reg)
}

fn itof_sig(
int_type_var: Type,
temp_reg: &ExtensionRegistry,
) -> Result<PolyFuncType, SignatureError> {
let body = FunctionType::new(vec![int_type_var], type_row![FLOAT64_TYPE]);
fn itof_sig(temp_reg: &ExtensionRegistry) -> Result<PolyFuncType, SignatureError> {
let body = FunctionType::new(vec![int_type_var(0)], type_row![FLOAT64_TYPE]);

PolyFuncType::new_validated(vec![LOG_WIDTH_TYPE_PARAM], body, temp_reg)
}
Expand All @@ -43,12 +40,8 @@ pub fn extension() -> Extension {
super::float_types::EXTENSION_ID,
]),
);
let int_types_extension = super::int_types::extension();
let int_type_def = int_types_extension.get_type(&INT_TYPE_ID).unwrap();
let int_type_var = int_type_var(0, int_type_def).unwrap();
let temp_reg: ExtensionRegistry = [
extension.clone(),
int_types_extension,
super::int_types::EXTENSION.to_owned(),
super::float_types::extension(),
PRELUDE.to_owned(),
]
Expand All @@ -57,28 +50,28 @@ pub fn extension() -> Extension {
.add_op_type_scheme_simple(
"trunc_u".into(),
"float to unsigned int".to_owned(),
ftoi_sig(int_type_var.clone(), &temp_reg).unwrap(),
ftoi_sig(&temp_reg).unwrap(),
)
.unwrap();
extension
.add_op_type_scheme_simple(
"trunc_s".into(),
"float to signed int".to_owned(),
ftoi_sig(int_type_var.clone(), &temp_reg).unwrap(),
ftoi_sig(&temp_reg).unwrap(),
)
.unwrap();
extension
.add_op_type_scheme_simple(
"convert_u".into(),
"unsigned int to float".to_owned(),
itof_sig(int_type_var.clone(), &temp_reg).unwrap(),
itof_sig(&temp_reg).unwrap(),
)
.unwrap();
extension
.add_op_type_scheme_simple(
"convert_s".into(),
"signed int to float".to_owned(),
itof_sig(int_type_var, &temp_reg).unwrap(),
itof_sig(&temp_reg).unwrap(),
)
.unwrap();

Expand Down
Loading

0 comments on commit 0063b46

Please sign in to comment.