Skip to content

Commit

Permalink
fixes ERA and f32 compilations (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricozb authored Apr 23, 2024
1 parent 90e3b44 commit 8a9a009
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn refs<'a>(host: &'a Host, instructions: &'a [Instruction]) -> BTreeSet<&'a str

for instr in instructions {
if let Instruction::Const { port, .. } | Instruction::LinkConst { port, .. } = instr {
if port.tag() == Tag::Ref {
if port.tag() == Tag::Ref && !port.is_era() {
refs.insert(host.back[&port.addr()].as_str());
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ fn compile_struct(
writeln!(code, "let ({rhs}, {out}) = net.do_op({op:?}, {trg});")
}
Instruction::OpNum { op, trg, rhs, out } => {
writeln!(code, "let {out} = net.do_op_num({op:?}, {trg}, {rhs:?});")
writeln!(code, "let {out} = net.do_op_num({op:?}, {trg}, {});", compile_port(host, rhs))
}
Instruction::Mat { trg, lft, rgt } => {
writeln!(code, "let ({lft}, {rgt}) = net.do_mat({trg});")
Expand All @@ -168,7 +168,17 @@ fn compile_port(host: &Host, port: &Port) -> String {
} else if port.tag() == Tag::Int {
format!("Port::new_int({})", port.int())
} else if port.tag() == Tag::F32 {
format!("Port::new_float({:?})", port.float())
let float = port.float();

if float.is_nan() {
"Port::new_float(f32::NAN)".to_string()
} else if float.is_infinite() && float > 0.0 {
"Port::new_float(f32::INFINITY)".to_string()
} else if float.is_infinite() {
"Port::new_float(f32::NEG_INFINITY)".to_string()
} else {
format!("Port::new_float({float:?})")
}
} else {
unreachable!()
}
Expand Down
6 changes: 6 additions & 0 deletions src/run/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl Port {
self.tag() == Tag::Int || self.tag() == Tag::F32
}

/// Whether this port is an [`ERA`] port.
#[inline(always)]
pub fn is_era(&self) -> bool {
matches!(*self, Port::ERA)
}

/// Accesses the label of this port; this is valid for all non-numeric ports.
#[inline(always)]
pub const fn lab(&self) -> Lab {
Expand Down

0 comments on commit 8a9a009

Please sign in to comment.