Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move Spline to single precision #7632

Merged
merged 1 commit into from
Feb 11, 2015
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
20 changes: 10 additions & 10 deletions PhysicsTools/MVAComputer/interface/Spline.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ class Spline {
void set(unsigned int n, const double *vals);

/// compute y coordinate at x coordinate \a x
double eval(double x) const;
float eval(float x) const;

/// compute the derivate at x coordinate \a x
double deriv(double x) const;
float deriv(float x) const;

/// compute integral under curve between 0 and \a x
double integral(double x) const;
float integral(float x) const;

/// total area (integral between 0 and 1) under curve
double getArea() const { return area; }
float getArea() const { return area; }

/// return the number of entries
inline unsigned int numberOfEntries() const { return n + 1; }

private:
/// internal class describing a "segment" (between two x points)
struct Segment {
double coeffs[4];
double area;
float coeffs[4];
float area;

double eval(double x) const;
double deriv(double x) const;
double integral(double x) const;
float eval(float x) const;
float deriv(float x) const;
float integral(float x) const;
};

unsigned int n;
Segment *segments;
double area;
float area;
};

} // namespace PhysicsTools
Expand Down
38 changes: 19 additions & 19 deletions PhysicsTools/MVAComputer/src/Spline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@

namespace PhysicsTools {

double Spline::Segment::eval(double x) const
float Spline::Segment::eval(float x) const
{
double tmp;
double y = 0.0;
float tmp;
float y = 0.0;
y += coeffs[0]; tmp = x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Horner, the Unknown...

y += coeffs[1] * tmp; tmp *= x;
y += coeffs[2] * tmp; tmp *= x;
y += coeffs[3] * tmp;
return y;
}

double Spline::Segment::deriv(double x) const
float Spline::Segment::deriv(float x) const
{
double tmp;
double d = 0.0;
float tmp;
float d = 0.0;
d += coeffs[1]; tmp = x;
d += coeffs[2] * tmp * 2.0; tmp *= x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to change all literals to 2.0f, 3.0f etc...

d += coeffs[3] * tmp * 3.0;
return d;
}

double Spline::Segment::integral(double x) const
float Spline::Segment::integral(float x) const
{
double tmp = x;
double area = this->area;
float tmp = x;
float area = this->area;
area += coeffs[0] * tmp; tmp *= x;
area += coeffs[1] * tmp * (1.0 / 2.0); tmp *= x;
area += coeffs[2] * tmp * (1.0 / 3.0); tmp *= x;
Expand Down Expand Up @@ -82,7 +82,7 @@ void Spline::set(unsigned int n_, const double *vals)
return;
}

double m0, m1;
float m0, m1;
Segment *seg = &segments[0];
m0 = 0.0, m1 = 0.5 * (vals[2] - vals[0]);
seg->coeffs[0] = vals[0];
Expand Down Expand Up @@ -128,20 +128,20 @@ Spline &Spline::operator = (const Spline &orig)
return *this;
}

double Spline::eval(double x) const
float Spline::eval(float x) const
{
if (x <= 0.0)
return segments[0].eval(0.0);
if (x >= 1.0)
return segments[n - 1].eval(1.0);

double total;
double rest = std::modf(x * n, &total);
float total;
float rest = std::modf(x * n, &total);

return segments[(unsigned int)total].eval(rest);
}

double Spline::deriv(double x) const
float Spline::deriv(float x) const
{
if (x < 0.0 || x > 1.0)
return 0.0;
Expand All @@ -150,13 +150,13 @@ double Spline::deriv(double x) const
else if (x == 1.0)
return segments[n - 1].deriv(1.0);

double total;
double rest = std::modf(x * n, &total);
float total;
float rest = std::modf(x * n, &total);

return segments[(unsigned int)total].deriv(rest);
}

double Spline::integral(double x) const
float Spline::integral(float x) const
{
if (x <= 0.0)
return 0.0;
Expand All @@ -166,8 +166,8 @@ double Spline::integral(double x) const
if (area < 1.0e-9)
return 0.0;

double total;
double rest = std::modf(x * n, &total);
float total;
float rest = std::modf(x * n, &total);

return segments[(unsigned int)total].integral(rest) / area;
}
Expand Down