Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
enricozb committed Apr 11, 2024
1 parent 83bf034 commit 2144bfd
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 165 deletions.
2 changes: 1 addition & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl<'i> Parser<'i> {

/// See `ops.rs` for the available operators.
fn parse_op(&mut self) -> Result<Op, String> {
let op = self.take_while(|c| "fui0123456789.+-=*/%<>|&^!?$".contains(c));
let op = self.take_while(|c| c.is_alphanumeric() || ".+-=*/%<>|&^!?$".contains(c));
op.parse().map_err(|_| format!("Unknown operator: {op:?}"))
}

Expand Down
16 changes: 6 additions & 10 deletions src/host/encode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::*;
use crate::{
ops::{Num, TypedOp as Op},
run::Lab,
util::maybe_grow,
};
use crate::{ops::TypedOp as Op, run::Lab, util::maybe_grow};

impl Host {
/// Converts an ast net to a list of instructions to create the net.
Expand Down Expand Up @@ -105,11 +101,11 @@ impl<'a, E: Encoder> State<'a, E> {
}
Tree::Op { op, rhs: lft, out: rgt } => match &**lft {
Tree::Int { val } => {
let o = self.encoder.op_num(*op, trg, Num::Int(*val));
let o = self.encoder.op_num(*op, trg, Port::new_int(*val));
self.visit_tree(rgt, o);
}
Tree::F32 { val } => {
let o = self.encoder.op_num(*op, trg, Num::Float(val.0));
let o = self.encoder.op_num(*op, trg, Port::new_float(val.0));
self.visit_tree(rgt, o);
}
_ => {
Expand Down Expand Up @@ -142,7 +138,7 @@ trait Encoder {
fn make_const(&mut self, port: Port) -> Self::Trg;
fn ctr(&mut self, lab: Lab, trg: Self::Trg) -> (Self::Trg, Self::Trg);
fn op(&mut self, op: Op, trg: Self::Trg) -> (Self::Trg, Self::Trg);
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Num) -> Self::Trg;
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Port) -> Self::Trg;
fn mat(&mut self, trg: Self::Trg) -> (Self::Trg, Self::Trg);
fn wires(&mut self) -> (Self::Trg, Self::Trg, Self::Trg, Self::Trg);
}
Expand Down Expand Up @@ -180,7 +176,7 @@ impl Encoder for InterpretedDef {
self.instr.push(Instruction::Op { op, trg, rhs, out });
(rhs, out)
}
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Num) -> Self::Trg {
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Port) -> Self::Trg {
let out = self.new_trg_id();
self.instr.push(Instruction::OpNum { op, trg, rhs, out });
out
Expand Down Expand Up @@ -218,7 +214,7 @@ impl<'a, M: Mode> Encoder for run::Net<'a, M> {
fn op(&mut self, op: Op, trg: Self::Trg) -> (Self::Trg, Self::Trg) {
self.do_op(op, trg)
}
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Num) -> Self::Trg {
fn op_num(&mut self, op: Op, trg: Self::Trg, rhs: Port) -> Self::Trg {
self.do_op_num(op, trg, rhs)
}
fn mat(&mut self, trg: Self::Trg) -> (Self::Trg, Self::Trg) {
Expand Down
3 changes: 1 addition & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
mod num;
pub mod word;
mod word;

use crate::util::bi_enum;

pub use self::num::Num;
use self::{
num::Numeric,
word::{FromWord, ToWord},
Expand Down
87 changes: 26 additions & 61 deletions src/ops/num.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
use super::word::ToWord;

#[derive(Clone, Copy, Debug)]
pub enum Num {
Int(i64),
Float(f32),
}

impl ToWord for Num {
fn to_word(self) -> u64 {
match self {
Self::Int(int) => int as u64,
Self::Float(float) => unsafe { std::mem::transmute::<_, u32>(float) as u64 },
}
}
}

#[rustfmt::skip]
pub trait Numeric: Sized {
const ZERO: Self;
const ONE: Self;

fn add(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn sub(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn mul(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn div(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn rem(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn and(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn or(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn xor(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn shl(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn shr(_: Self, _: Self) -> Self {
return Self::ZERO;
}
fn add(_: Self, _: Self) -> Self { Self::ZERO }
fn sub(_: Self, _: Self) -> Self { Self::ZERO }
fn mul(_: Self, _: Self) -> Self { Self::ZERO }
fn div(_: Self, _: Self) -> Self { Self::ZERO }
fn rem(_: Self, _: Self) -> Self { Self::ZERO }
fn and(_: Self, _: Self) -> Self { Self::ZERO }
fn or(_: Self, _: Self) -> Self { Self::ZERO }
fn xor(_: Self, _: Self) -> Self { Self::ZERO }
fn shl(_: Self, _: Self) -> Self { Self::ZERO }
fn shr(_: Self, _: Self) -> Self { Self::ZERO }

fn from_bool(b: bool) -> Self {
if b { Self::ONE } else { Self::ZERO }
}
Expand All @@ -57,21 +22,21 @@ pub trait Numeric: Sized {
macro_rules! impl_numeric {
( $($ty:ty),+ ) => {
$(
impl Numeric for $ty {
const ZERO: Self = 0;
const ONE: Self = 1;
impl Numeric for $ty {
const ZERO: Self = 0;
const ONE: Self = 1;

fn add(a: Self, b: Self) -> Self { a.wrapping_add(b) }
fn sub(a: Self, b: Self) -> Self { a.wrapping_sub(b) }
fn mul(a: Self, b: Self) -> Self { a.wrapping_mul(b) }
fn div(a: Self, b: Self) -> Self { a.checked_div(b).unwrap_or(0) }
fn rem(a: Self, b: Self) -> Self { a.checked_rem(b).unwrap_or(0) }
fn and(a: Self, b: Self) -> Self { a & b }
fn or(a: Self, b: Self) -> Self { a | b }
fn xor(a: Self, b: Self) -> Self { a ^ b }
fn shl(a: Self, b: Self) -> Self { a.wrapping_shl(b as u32) }
fn shr(a: Self, b: Self) -> Self { a.wrapping_shr(b as u32) }
}
fn add(a: Self, b: Self) -> Self { a.wrapping_add(b) }
fn sub(a: Self, b: Self) -> Self { a.wrapping_sub(b) }
fn mul(a: Self, b: Self) -> Self { a.wrapping_mul(b) }
fn div(a: Self, b: Self) -> Self { a.checked_div(b).unwrap_or(0) }
fn rem(a: Self, b: Self) -> Self { a.checked_rem(b).unwrap_or(0) }
fn and(a: Self, b: Self) -> Self { a & b }
fn or(a: Self, b: Self) -> Self { a | b }
fn xor(a: Self, b: Self) -> Self { a ^ b }
fn shl(a: Self, b: Self) -> Self { a.wrapping_shl(b as u32) }
fn shr(a: Self, b: Self) -> Self { a.wrapping_shr(b as u32) }
}
)*
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ops/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl_unsigned! { u8, u16, u32, u64 }
impl FromWord for f32 {
#[inline(always)]
fn from_word(bits: u64) -> Self {
unsafe { std::mem::transmute(bits as u32) }
f32::from_bits(bits as u32)
}
}

impl ToWord for f32 {
#[inline(always)]
fn to_word(self) -> u64 {
unsafe { std::mem::transmute::<_, u32>(self) as u64 }
self.to_bits() as u64
}
}
4 changes: 2 additions & 2 deletions src/run/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl AsHostedDef for InterpretedDef {
trgs.set_trg(rhs, r);
trgs.set_trg(out, o);
}
Instruction::OpNum { op, trg, rhs: lhs, out } => {
let o = net.do_op_num(op, trgs.get_trg(trg), lhs);
Instruction::OpNum { op, trg, rhs: ref lhs, out } => {
let o = net.do_op_num(op, trgs.get_trg(trg), lhs.clone());
trgs.set_trg(out, o);
}
Instruction::Mat { trg, lft, rgt } => {
Expand Down
14 changes: 6 additions & 8 deletions src/run/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::ops::{word::ToWord, Num};

use super::*;

/// Each instruction corresponds to a fragment of a net that has a native
Expand Down Expand Up @@ -31,7 +29,7 @@ use super::*;
/// of each `TrgId`.
///
/// Some instructions take a [`Port`]; these must always be statically-valid
/// ports -- that is, [`Ref`] or [`Int`] ports.
/// ports -- that is, [`Ref`], [`Int`], or [`F32`] ports.
#[derive(Debug, Clone)]
pub enum Instruction {
/// ```rust,ignore
Expand Down Expand Up @@ -60,7 +58,7 @@ pub enum Instruction {
/// ```rust,ignore
/// let out = net.do_op_num(lab, trg, rhs);
/// ```
OpNum { op: Op, trg: TrgId, rhs: Num, out: TrgId },
OpNum { op: Op, trg: TrgId, rhs: Port, out: TrgId },
/// See [`Net::do_mat`].
/// ```rust,ignore
/// let (lft, rgt) = net.do_mat(trg);
Expand Down Expand Up @@ -154,13 +152,13 @@ impl<'a, M: Mode> Net<'a, M> {

/// `trg ~ <op #b x>`
#[inline(always)]
pub(crate) fn do_op_num(&mut self, op: Op, trg: Trg, rhs: Num) -> Trg {
pub(crate) fn do_op_num(&mut self, op: Op, trg: Trg, rhs: Port) -> Trg {
let port = trg.target();
if !M::LAZY && (port.tag() == Int || port.tag() == F32) {
if !M::LAZY && port.is_num() {
self.rwts.oper += 1;
self.free_trg(trg);

let res = op.op(port.num(), rhs.to_word());
let res = op.op(port.num(), rhs.num());

if op.is_int() { Trg::port(Port::new_num(Tag::Int, res)) } else { Trg::port(Port::new_num(Tag::F32, res)) }
} else if !M::LAZY && port == Port::ERA {
Expand All @@ -169,7 +167,7 @@ impl<'a, M: Mode> Net<'a, M> {
} else {
let n = self.create_node(Op, op.into());
self.link_trg_port(trg, n.p0);
n.p1.wire().set_target(Port::from(rhs));
n.p1.wire().set_target(rhs);
Trg::port(n.p2)
}
}
Expand Down
Loading

0 comments on commit 2144bfd

Please sign in to comment.