//======================================================================================
//======================================================================================
// Filename: SIMKIT_macrodefs.include
//======================================================================================
//======================================================================================
//
//  (c) Copyright 2007, All Rights Reserved, NXP Semiconductors
//
//
//  Version: 102.1, April 2007 (Simkit 2.5)
//
//======================================================================================
//======================================================================================
//
// Further information can be found in the file readme.txt
//

//////////////////////////////////////////////////////////////
//
//  General macros and constants for compact va-models 
//
//////////////////////////////////////////////////////////////

`define VERS  "0.0"
`define VREV  "0.0"
`define VERSreal  0.0
`define VREVreal  0.0

`define CLIP_LOW(val,min)      ((val)>(min)?(val):(min))
`define CLIP_HIGH(val,max)     ((val)<(max)?(val):(max))
`define CLIP_BOTH(val,min,max) ((val)>(min)?((val)<(max)?(val):(max)):(min))

    // Note 1: In this va-code, the `P-macro is defined such that its argument
    // is ignored during compilation; in this source code it acts as
    // a comment
    // Note 2: In this va-code, the "from" keyword in the parameter
    // list is not used. Silent clipping is used instead. One could enable
    // the Verilog-A range checking by redefining the `from-macro below.
    `define P(txt)
    `define AT_MODEL
    `define AT_INSTANCE
    `define AT_NOISE
    `define from(lower,upper)
//    `define from(lower,upper) from[lower:upper]

// Some  functions
`define MAX(x,y)              ((x)>(y)?(x):(y))
`define MIN(x,y)              ((x)<(y)?(x):(y))

// Mathematical constants
`define PI                    3.1415926535897931
`define SQRTPI                1.77245385090551603

// Physical constants
`define KELVINCONVERSION      273.15                            
`define KBOL                  1.3806505E-23                     
`define QELE                  1.6021918E-19                     
`define HBAR                  1.05457168E-34                       
`define MELE                  9.1093826E-31
`define EPSSI                 1.045E-10

// Other constants 
`define oneThird              3.3333333333333333e-01
`define twoThirds             6.6666666666666667e-01

// Constants needed in safe exponential function (called "expl")
`define se                    4.6051701859880916e+02
`define se05                  2.3025850929940458e+02
`define ke                    1.0e-200
`define ke05                  1.0e-100
`define keinv                 1.0e200
`define ke05inv               1.0e100

/////////////////////////////////////////////////////////////////////////////
//
//  Macro definitions.
//
//  Note that because variables in macros are not locally scoped,
//  the intermediate variables used in the macros below must be
//  explicitly declared in the main code.
//
/////////////////////////////////////////////////////////////////////////////


//  P3       3rd order polynomial expansion of exp()
`define P3(u) (1.0 + (u) * (1.0 + 0.5 * ((u) * (1.0 + (u) * `oneThird))))


//  expl     exp() with 3rd order polynomial extrapolation
//           for very low values (exp_low), very high
//           values (exp_high), or both (expl), to avoid overflows
//           and underflows and retain C-3 continuity
`define expl(x, res) \
if (abs(x) < `se05) begin\
    res       = exp(x); \
end else begin \
    if ((x) < -`se05) begin\
        res       = `ke05 / `P3(-`se05 - (x)); \
    end else begin\
        res       =  `ke05inv * `P3((x) - `se05); \
    end \
end

`define expl_low(x, res) \
if ((x) > -`se05) begin\
    res       =  exp(x); \
end else begin\
    res       = `ke05 / `P3(-`se05 - (x)); \
end

`define expl_high(x, res) \
if ((x) < `se05) begin\
    res       = exp(x); \
end else begin \
    res       =  `ke05inv * `P3((x) - `se05); \
end

`define swap(a, b) \
temp = a; \
a    = b; \
b    = temp;
