small.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // Small arrays
  3. // Adapted from blitz++/examples/tiny.cpp
  4. // Daniel Llorens - 2015
  5. // The point of this example is to show the assembly; I will when it's not awful :-/
  6. // $CXX -o small.s -S -O3 -DRA_DO_CHECK=0 -std=c++14 -Wall -Werror -Wno-unknown-pragmas -Wno-parentheses -Wno-error=strict-overflow -march=native -I.. small.cc -funroll-loops -ffast-math -fno-exceptions
  7. #include "ra/ra.hh"
  8. #include <iostream>
  9. using std::cout, std::endl, std::flush;
  10. void
  11. add_vectors(ra::Small<double, 4> & c, ra::Small<double, 4> const & a, ra::Small<double, 4> const & b)
  12. {
  13. c = a+b;
  14. }
  15. void
  16. reflect(ra::Small<double, 3> & reflection, ra::Small<double, 3> const & ray, ra::Small<double, 3> const & surfaceNormal)
  17. {
  18. // The surface normal must be unit length to use this equation.
  19. reflection = ray - 2.7 * dot(ray, surfaceNormal) * surfaceNormal;
  20. }
  21. int main()
  22. {
  23. {
  24. ra::Small<double, 3> x, y, z;
  25. // y will be the incident ray
  26. y = { 1, 0, -1 };
  27. // z is the surface normal
  28. z = { 0, 0, 1 };
  29. reflect(x, y, z);
  30. cout << "Reflected ray is: [ " << x[0] << " " << x[1] << " " << x[2]
  31. << " ]" << endl;
  32. }
  33. {
  34. ra::Small<double, 4> x, y { 1, 2, 3, 4 }, z { 10, 11, 12, 13 };
  35. add_vectors(x, y, z);
  36. cout << "x(" << x << ") = y (" << y << ") + z (" << z << ")" << endl;
  37. }
  38. return 0;
  39. }