main.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* This file is part of the Vc library.
  2. Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
  3. Permission to use, copy, modify, and distribute this software
  4. and its documentation for any purpose and without fee is hereby
  5. granted, provided that the above copyright notice appear in all
  6. copies and that both that the copyright notice and this
  7. permission notice and warranty disclaimer appear in supporting
  8. documentation, and that the name of the author not be used in
  9. advertising or publicity pertaining to distribution of the
  10. software without specific, written prior permission.
  11. The author disclaim all warranties with regard to this
  12. software, including all implied warranties of merchantability
  13. and fitness. In no event shall the author be liable for any
  14. special, indirect or consequential damages or any damages
  15. whatsoever resulting from loss of use, data or profits, whether
  16. in an action of contract, negligence or other tortious action,
  17. arising out of or in connection with the use or performance of
  18. this software.
  19. */
  20. //! [includes]
  21. #include <Vc/Vc>
  22. #include <iostream>
  23. #include <iomanip>
  24. using Vc::float_v;
  25. //! [includes]
  26. //! [memory allocation]
  27. int Vc_CDECL main()
  28. {
  29. // allocate memory for our initial x and y coordinates. Note that you can also put it into a
  30. // normal float C-array but that you then must ensure alignment to Vc::VectorAlignment!
  31. Vc::Memory<float_v, 1000> x_mem;
  32. Vc::Memory<float_v, 1000> y_mem;
  33. Vc::Memory<float_v, 1000> r_mem;
  34. Vc::Memory<float_v, 1000> phi_mem;
  35. //! [memory allocation]
  36. //! [random init]
  37. // fill the memory with values from -1.f to 1.f
  38. for (size_t i = 0; i < x_mem.vectorsCount(); ++i) {
  39. x_mem.vector(i) = float_v::Random() * 2.f - 1.f;
  40. y_mem.vector(i) = float_v::Random() * 2.f - 1.f;
  41. }
  42. //! [random init]
  43. //! [conversion]
  44. // calculate the polar coordinates for all coordinates and overwrite the euclidian coordinates
  45. // with the result
  46. for (size_t i = 0; i < x_mem.vectorsCount(); ++i) {
  47. const float_v x = x_mem.vector(i);
  48. const float_v y = y_mem.vector(i);
  49. r_mem.vector(i) = Vc::sqrt(x * x + y * y);
  50. float_v phi = Vc::atan2(y, x) * 57.295780181884765625f; // 180/pi
  51. phi(phi < 0.f) += 360.f;
  52. phi_mem.vector(i) = phi;
  53. }
  54. //! [conversion]
  55. //! [output]
  56. // print the results
  57. for (size_t i = 0; i < x_mem.entriesCount(); ++i) {
  58. std::cout << std::setw(3) << i << ": ";
  59. std::cout << std::setw(10) << x_mem[i] << ", " << std::setw(10) << y_mem[i] << " -> ";
  60. std::cout << std::setw(10) << r_mem[i] << ", " << std::setw(10) << phi_mem[i] << '\n';
  61. }
  62. return 0;
  63. }
  64. //! [output]