Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions integration_tests/VAMS2023_MATH/vams2023_math.va
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
`include "constants.vams"
`include "disciplines.vams"

// Exercises the VAMS-2023 expm1()/ln1p() builtins through the whole pipeline
// (HIR -> MIR -> autodiff -> LLVM/OSDI), including their derivatives.
//
// expm1 is the natural spelling for a diode current: the usual
// is*(exp(vd/(n*vt)) - 1) cancels to zero once |vd| << n*vt, which is exactly
// what expm1 avoids.
module vams2023_math(A, C);
inout A, C;
electrical A, C;

branch (A, C) br_ac;

(*desc = "Saturation current", units = "A"*) parameter real is = 1e-14 from [0:inf];
(*desc = "Emission coefficient"*) parameter real n = 1.0 from (0:inf);
(*desc = "Leakage conductance", units = "S"*) parameter real gleak = 1e-12 from [0:inf];

real vt, vd, id, vleak;

(*desc = "diode admittance", units = "S"*) real gd;

analog begin
vt = `P_K * $temperature / `P_Q;
vd = V(br_ac);

id = is * expm1(vd / (n * vt));

// Independent argument so this is not simplified into the expm1 above,
// which keeps ln1p and its derivative in the generated code.
vleak = vt * ln1p(vd / vt);

I(br_ac) <+ id + gleak * vleak;

gd = ddx(id, V(A));
end
endmodule
204 changes: 105 additions & 99 deletions openvaf/hir_def/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,105 +24,107 @@ pub enum BuiltIn {
cos = 9u8,
cosh = 10u8,
exp = 11u8,
floor = 12u8,
flow = 13u8,
potential = 14u8,
hypot = 15u8,
ln = 16u8,
log = 17u8,
max = 18u8,
min = 19u8,
pow = 20u8,
sin = 21u8,
sinh = 22u8,
sqrt = 23u8,
tan = 24u8,
tanh = 25u8,
display = 26u8,
strobe = 27u8,
write = 28u8,
monitor = 29u8,
debug = 30u8,
fclose = 31u8,
fopen = 32u8,
fdisplay = 33u8,
fwrite = 34u8,
fstrobe = 35u8,
fmonitor = 36u8,
fgets = 37u8,
fscanf = 38u8,
swrite = 39u8,
sformat = 40u8,
sscanf = 41u8,
rewind = 42u8,
fseek = 43u8,
ftell = 44u8,
fflush = 45u8,
ferror = 46u8,
feof = 47u8,
fdebug = 48u8,
finish = 49u8,
stop = 50u8,
fatal = 51u8,
warning = 52u8,
error = 53u8,
info = 54u8,
abstime = 55u8,
dist_chi_square = 56u8,
dist_exponential = 57u8,
dist_poisson = 58u8,
dist_uniform = 59u8,
dist_erlang = 60u8,
dist_normal = 61u8,
dist_t = 62u8,
random = 63u8,
arandom = 64u8,
rdist_chi_square = 65u8,
rdist_exponential = 66u8,
rdist_poisson = 67u8,
rdist_uniform = 68u8,
rdist_erlang = 69u8,
rdist_normal = 70u8,
rdist_t = 71u8,
clog2 = 72u8,
log10 = 73u8,
temperature = 74u8,
vt = 75u8,
simparam = 76u8,
simparam_str = 77u8,
simprobe = 78u8,
discontinuity = 79u8,
param_given = 80u8,
port_connected = 81u8,
analog_node_alias = 82u8,
analog_port_alias = 83u8,
test_plusargs = 84u8,
value_plusargs = 85u8,
bound_step = 86u8,
analysis = 87u8,
ac_stim = 88u8,
noise_table = 89u8,
noise_table_log = 90u8,
white_noise = 91u8,
flicker_noise = 92u8,
limit = 93u8,
absdelay = 94u8,
ddt = 95u8,
idt = 96u8,
idtmod = 97u8,
ddx = 98u8,
zi_nd = 99u8,
zi_np = 100u8,
zi_zd = 101u8,
zi_zp = 102u8,
laplace_nd = 103u8,
laplace_np = 104u8,
laplace_zd = 105u8,
laplace_zp = 106u8,
limexp = 107u8,
last_crossing = 108u8,
slew = 109u8,
transition = 110u8,
expm1 = 12u8,
floor = 13u8,
flow = 14u8,
potential = 15u8,
hypot = 16u8,
ln = 17u8,
ln1p = 18u8,
log = 19u8,
max = 20u8,
min = 21u8,
pow = 22u8,
sin = 23u8,
sinh = 24u8,
sqrt = 25u8,
tan = 26u8,
tanh = 27u8,
display = 28u8,
strobe = 29u8,
write = 30u8,
monitor = 31u8,
debug = 32u8,
fclose = 33u8,
fopen = 34u8,
fdisplay = 35u8,
fwrite = 36u8,
fstrobe = 37u8,
fmonitor = 38u8,
fgets = 39u8,
fscanf = 40u8,
swrite = 41u8,
sformat = 42u8,
sscanf = 43u8,
rewind = 44u8,
fseek = 45u8,
ftell = 46u8,
fflush = 47u8,
ferror = 48u8,
feof = 49u8,
fdebug = 50u8,
finish = 51u8,
stop = 52u8,
fatal = 53u8,
warning = 54u8,
error = 55u8,
info = 56u8,
abstime = 57u8,
dist_chi_square = 58u8,
dist_exponential = 59u8,
dist_poisson = 60u8,
dist_uniform = 61u8,
dist_erlang = 62u8,
dist_normal = 63u8,
dist_t = 64u8,
random = 65u8,
arandom = 66u8,
rdist_chi_square = 67u8,
rdist_exponential = 68u8,
rdist_poisson = 69u8,
rdist_uniform = 70u8,
rdist_erlang = 71u8,
rdist_normal = 72u8,
rdist_t = 73u8,
clog2 = 74u8,
log10 = 75u8,
temperature = 76u8,
vt = 77u8,
simparam = 78u8,
simparam_str = 79u8,
simprobe = 80u8,
discontinuity = 81u8,
param_given = 82u8,
port_connected = 83u8,
analog_node_alias = 84u8,
analog_port_alias = 85u8,
test_plusargs = 86u8,
value_plusargs = 87u8,
bound_step = 88u8,
analysis = 89u8,
ac_stim = 90u8,
noise_table = 91u8,
noise_table_log = 92u8,
white_noise = 93u8,
flicker_noise = 94u8,
limit = 95u8,
absdelay = 96u8,
ddt = 97u8,
idt = 98u8,
idtmod = 99u8,
ddx = 100u8,
zi_nd = 101u8,
zi_np = 102u8,
zi_zd = 103u8,
zi_zp = 104u8,
laplace_nd = 105u8,
laplace_np = 106u8,
laplace_zd = 107u8,
laplace_zp = 108u8,
limexp = 109u8,
last_crossing = 110u8,
slew = 111u8,
transition = 112u8,
}
#[derive(Eq, PartialEq, Copy, Clone, Hash, Debug)]
#[allow(nonstandard_style, unreachable_pub)]
Expand Down Expand Up @@ -251,11 +253,13 @@ pub fn insert_builtin_scope(dst: &mut IndexMap<Name, ScopeDefItem, BuildHasherDe
dst.insert(kw::cos, BuiltIn::cos.into());
dst.insert(kw::cosh, BuiltIn::cosh.into());
dst.insert(kw::exp, BuiltIn::exp.into());
dst.insert(kw::expm1, BuiltIn::expm1.into());
dst.insert(kw::floor, BuiltIn::floor.into());
dst.insert(kw::flow, BuiltIn::flow.into());
dst.insert(kw::potential, BuiltIn::potential.into());
dst.insert(kw::hypot, BuiltIn::hypot.into());
dst.insert(kw::ln, BuiltIn::ln.into());
dst.insert(kw::ln1p, BuiltIn::ln1p.into());
dst.insert(kw::log, BuiltIn::log.into());
dst.insert(kw::max, BuiltIn::max.into());
dst.insert(kw::min, BuiltIn::min.into());
Expand Down Expand Up @@ -313,8 +317,10 @@ pub fn insert_builtin_scope(dst: &mut IndexMap<Name, ScopeDefItem, BuildHasherDe
dst.insert(sysfun::rdist_t, BuiltIn::rdist_t.into());
dst.insert(sysfun::clog2, BuiltIn::clog2.into());
dst.insert(sysfun::ln, BuiltIn::ln.into());
dst.insert(sysfun::ln1p, BuiltIn::ln1p.into());
dst.insert(sysfun::log10, BuiltIn::log10.into());
dst.insert(sysfun::exp, BuiltIn::exp.into());
dst.insert(sysfun::expm1, BuiltIn::expm1.into());
dst.insert(sysfun::sqrt, BuiltIn::sqrt.into());
dst.insert(sysfun::pow, BuiltIn::pow.into());
dst.insert(sysfun::floor, BuiltIn::floor.into());
Expand Down
8 changes: 8 additions & 0 deletions openvaf/hir_lower/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ impl BodyLoweringCtx<'_, '_, '_> {
let arg0 = self.lower_expr(args[0]);
self.ctx.ins().exp(arg0)
}
BuiltIn::expm1 => {
let arg0 = self.lower_expr(args[0]);
self.ctx.ins().expm1(arg0)
}

BuiltIn::limexp => {
let arg0 = self.lower_expr(args[0]);
Expand Down Expand Up @@ -393,6 +397,10 @@ impl BodyLoweringCtx<'_, '_, '_> {
let arg0 = self.lower_expr(args[0]);
self.ctx.ins().ln(arg0)
}
BuiltIn::ln1p => {
let arg0 = self.lower_expr(args[0]);
self.ctx.ins().ln1p(arg0)
}
BuiltIn::sin => {
let arg0 = self.lower_expr(args[0]);
self.ctx.ins().sin(arg0)
Expand Down
2 changes: 2 additions & 0 deletions openvaf/hir_ty/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,10 @@ copied_builtins! {
COS = REAL_MATH_1
COSH = REAL_MATH_1
EXP = REAL_MATH_1
EXPM1 = REAL_MATH_1
FLOOR = REAL_MATH_1
LN = REAL_MATH_1
LN1P = REAL_MATH_1
LOG = REAL_MATH_1
CLOG2 = INT_MATH_2
LOG10 = REAL_MATH_1
Expand Down
4 changes: 3 additions & 1 deletion openvaf/hir_ty/src/builtin/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hir_def::BuiltIn;

use crate::builtin::*;

const BUILTIN_INFO: [BuiltinInfo; 111usize] = [
const BUILTIN_INFO: [BuiltinInfo; 113usize] = [
ABS,
ACOS,
ACOSH,
Expand All @@ -17,11 +17,13 @@ const BUILTIN_INFO: [BuiltinInfo; 111usize] = [
COS,
COSH,
EXP,
EXPM1,
FLOOR,
FLOW,
POTENTIAL,
HYPOT,
LN,
LN1P,
LOG,
MAX,
MIN,
Expand Down
8 changes: 8 additions & 0 deletions openvaf/mir/src/builder/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ pub trait InstBuilder<'f>: InstBuilderBase<'f> {
let (inst, dfg) = self.unary(Opcode::Exp, arg0);
dfg.first_result(inst)
}
fn expm1(self, arg0: Value) -> Value {
let (inst, dfg) = self.unary(Opcode::Expm1, arg0);
dfg.first_result(inst)
}
fn ln(self, arg0: Value) -> Value {
let (inst, dfg) = self.unary(Opcode::Ln, arg0);
dfg.first_result(inst)
}
fn ln1p(self, arg0: Value) -> Value {
let (inst, dfg) = self.unary(Opcode::Ln1p, arg0);
dfg.first_result(inst)
}
fn log(self, arg0: Value) -> Value {
let (inst, dfg) = self.unary(Opcode::Log, arg0);
dfg.first_result(inst)
Expand Down
Loading
Loading