Skip to content

Commit

Permalink
Avoid unnecessary copies
Browse files Browse the repository at this point in the history
  • Loading branch information
marcromani committed Apr 6, 2024
1 parent 08e3e32 commit 2f00d85
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 79 deletions.
46 changes: 23 additions & 23 deletions src/functions/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace cascade
{
Var pow(Var x, Var y)
Var pow(const Var& x, const Var& y)
{
Var result = std::pow(x.value(), y.value());

Expand All @@ -40,7 +40,7 @@ Var pow(Var x, Var y)
return result;
}

Var sqrt(Var x)
Var sqrt(const Var& x)
{
Var result = std::sqrt(x.value());

Expand All @@ -51,7 +51,7 @@ Var sqrt(Var x)
return result;
}

Var abs(Var x)
Var abs(const Var& x)
{
Var result = std::abs(x.value());

Expand All @@ -62,7 +62,7 @@ Var abs(Var x)
return result;
}

Var exp(Var x)
Var exp(const Var& x)
{
Var result = std::exp(x.value());

Expand All @@ -73,7 +73,7 @@ Var exp(Var x)
return result;
}

Var exp2(Var x)
Var exp2(const Var& x)
{
Var result = std::exp2(x.value());

Expand All @@ -84,7 +84,7 @@ Var exp2(Var x)
return result;
}

Var exp10(Var x)
Var exp10(const Var& x)
{
Var result = std::pow(10.0, x.value());

Expand All @@ -95,7 +95,7 @@ Var exp10(Var x)
return result;
}

Var log(Var x)
Var log(const Var& x)
{
Var result = std::log(x.value());

Expand All @@ -106,7 +106,7 @@ Var log(Var x)
return result;
}

Var log2(Var x)
Var log2(const Var& x)
{
Var result = std::log2(x.value());

Expand All @@ -117,7 +117,7 @@ Var log2(Var x)
return result;
}

Var log10(Var x)
Var log10(const Var& x)
{
Var result = std::log10(x.value());

Expand All @@ -128,7 +128,7 @@ Var log10(Var x)
return result;
}

Var sin(Var x)
Var sin(const Var& x)
{
Var result = std::sin(x.value());

Expand All @@ -139,7 +139,7 @@ Var sin(Var x)
return result;
}

Var cos(Var x)
Var cos(const Var& x)
{
Var result = std::cos(x.value());

Expand All @@ -150,7 +150,7 @@ Var cos(Var x)
return result;
}

Var tan(Var x)
Var tan(const Var& x)
{
Var result = std::tan(x.value());

Expand All @@ -161,7 +161,7 @@ Var tan(Var x)
return result;
}

Var asin(Var x)
Var asin(const Var& x)
{
Var result = std::asin(x.value());

Expand All @@ -172,7 +172,7 @@ Var asin(Var x)
return result;
}

Var acos(Var x)
Var acos(const Var& x)
{
Var result = std::acos(x.value());

Expand All @@ -183,7 +183,7 @@ Var acos(Var x)
return result;
}

Var atan(Var x)
Var atan(const Var& x)
{
Var result = std::atan(x.value());

Expand All @@ -194,7 +194,7 @@ Var atan(Var x)
return result;
}

Var sinh(Var x)
Var sinh(const Var& x)
{
Var result = std::sinh(x.value());

Expand All @@ -205,7 +205,7 @@ Var sinh(Var x)
return result;
}

Var cosh(Var x)
Var cosh(const Var& x)
{
Var result = std::cosh(x.value());

Expand All @@ -216,7 +216,7 @@ Var cosh(Var x)
return result;
}

Var tanh(Var x)
Var tanh(const Var& x)
{
Var result = std::tanh(x.value());

Expand All @@ -227,7 +227,7 @@ Var tanh(Var x)
return result;
}

Var asinh(Var x)
Var asinh(const Var& x)
{
Var result = std::asinh(x.value());

Expand All @@ -238,7 +238,7 @@ Var asinh(Var x)
return result;
}

Var acosh(Var x)
Var acosh(const Var& x)
{
Var result = std::acosh(x.value());

Expand All @@ -249,7 +249,7 @@ Var acosh(Var x)
return result;
}

Var atanh(Var x)
Var atanh(const Var& x)
{
Var result = std::atanh(x.value());

Expand All @@ -260,7 +260,7 @@ Var atanh(Var x)
return result;
}

Var min(Var x, Var y)
Var min(const Var& x, const Var& y)
{
Var result = std::min(x.value(), y.value());

Expand All @@ -271,7 +271,7 @@ Var min(Var x, Var y)
return result;
}

Var max(Var x, Var y)
Var max(const Var& x, const Var& y)
{
Var result = std::max(x.value(), y.value());

Expand Down
46 changes: 23 additions & 23 deletions src/functions/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@

namespace cascade
{
Var pow(Var, Var);
Var pow(const Var&, const Var&);

Var sqrt(Var);
Var sqrt(const Var&);

Var abs(Var);
Var abs(const Var&);

Var exp(Var);
Var exp2(Var);
Var exp10(Var);
Var exp(const Var&);
Var exp2(const Var&);
Var exp10(const Var&);

Var log(Var);
Var log2(Var);
Var log10(Var);
Var log(const Var&);
Var log2(const Var&);
Var log10(const Var&);

Var sin(Var);
Var cos(Var);
Var tan(Var);
Var sin(const Var&);
Var cos(const Var&);
Var tan(const Var&);

Var asin(Var);
Var acos(Var);
Var atan(Var);
Var asin(const Var&);
Var acos(const Var&);
Var atan(const Var&);

Var sinh(Var);
Var cosh(Var);
Var tanh(Var);
Var sinh(const Var&);
Var cosh(const Var&);
Var tanh(const Var&);

Var asinh(Var);
Var acosh(Var);
Var atanh(Var);
Var asinh(const Var&);
Var acosh(const Var&);
Var atanh(const Var&);

Var min(Var, Var);
Var max(Var, Var);
Var min(const Var&, const Var&);
Var max(const Var&, const Var&);
} // namespace cascade

#endif
10 changes: 5 additions & 5 deletions src/var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void Var::backprop() const
}
}

Var operator+(Var x, Var y)
Var operator+(const Var &x, const Var &y)
{
Var result = x.value() + y.value();

Expand All @@ -106,7 +106,7 @@ Var operator+(Var x, Var y)
return result;
}

Var operator-(Var x, Var y)
Var operator-(const Var &x, const Var &y)
{
Var result = x.value() - y.value();

Expand All @@ -117,7 +117,7 @@ Var operator-(Var x, Var y)
return result;
}

Var operator-(Var x)
Var operator-(const Var &x)
{
Var result = -x.value();

Expand All @@ -128,7 +128,7 @@ Var operator-(Var x)
return result;
}

Var operator*(Var x, Var y)
Var operator*(const Var &x, const Var &y)
{
Var result = x.value() * y.value();

Expand All @@ -139,7 +139,7 @@ Var operator*(Var x, Var y)
return result;
}

Var operator/(Var x, Var y)
Var operator/(const Var &x, const Var &y)
{
Var result = x.value() / y.value();

Expand Down
56 changes: 28 additions & 28 deletions src/var.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,46 @@ class Var

/* Math operators */

friend Var operator+(Var, Var);
friend Var operator-(Var, Var);
friend Var operator-(Var);
friend Var operator*(Var, Var);
friend Var operator/(Var, Var);
friend Var operator+(const Var &, const Var &);
friend Var operator-(const Var &, const Var &);
friend Var operator-(const Var &);
friend Var operator*(const Var &, const Var &);
friend Var operator/(const Var &, const Var &);

/* Math functions */

friend Var pow(Var, Var);
friend Var pow(const Var &, const Var &);

friend Var sqrt(Var);
friend Var sqrt(const Var &);

friend Var abs(Var);
friend Var abs(const Var &);

friend Var exp(Var);
friend Var exp2(Var);
friend Var exp10(Var);
friend Var exp(const Var &);
friend Var exp2(const Var &);
friend Var exp10(const Var &);

friend Var log(Var);
friend Var log2(Var);
friend Var log10(Var);
friend Var log(const Var &);
friend Var log2(const Var &);
friend Var log10(const Var &);

friend Var sin(Var);
friend Var cos(Var);
friend Var tan(Var);
friend Var sin(const Var &);
friend Var cos(const Var &);
friend Var tan(const Var &);

friend Var asin(Var);
friend Var acos(Var);
friend Var atan(Var);
friend Var asin(const Var &);
friend Var acos(const Var &);
friend Var atan(const Var &);

friend Var sinh(Var);
friend Var cosh(Var);
friend Var tanh(Var);
friend Var sinh(const Var &);
friend Var cosh(const Var &);
friend Var tanh(const Var &);

friend Var asinh(Var);
friend Var acosh(Var);
friend Var atanh(Var);
friend Var asinh(const Var &);
friend Var acosh(const Var &);
friend Var atanh(const Var &);

friend Var min(Var, Var);
friend Var max(Var, Var);
friend Var min(const Var &, const Var &);
friend Var max(const Var &, const Var &);

private:
static void createEdges_(const std::initializer_list<Var> &inputNodes, Var &outputNode);
Expand Down

0 comments on commit 2f00d85

Please sign in to comment.