Skip to content

Commit 8c390a5

Browse files
committed
Added inverse square root optimisation
1 parent 9ed8595 commit 8c390a5

File tree

8 files changed

+41
-27
lines changed

8 files changed

+41
-27
lines changed

external/Actor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ Actor::Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType
1111
color = pColor;
1212
}
1313

14-
SYCL_EXTERNAL vecType Actor::getPos() {
14+
SYCL_EXTERNAL vecType Actor::getPos() const {
1515
return pos;
1616
}
1717

18-
SYCL_EXTERNAL vecType Actor::getVelocity() {
18+
SYCL_EXTERNAL vecType Actor::getVelocity() const {
1919
return velocity;
2020
}
2121

22-
SYCL_EXTERNAL vecType Actor::getDesiredVelocity() {
22+
SYCL_EXTERNAL vecType Actor::getDesiredVelocity() const {
2323
return desiredVelocity;
2424
}
2525

26-
SYCL_EXTERNAL vecType Actor::getDestination() {
26+
SYCL_EXTERNAL vecType Actor::getDestination() const {
2727
return destination;
2828
}
2929

30-
SYCL_EXTERNAL float Actor::getMass() {
30+
SYCL_EXTERNAL float Actor::getMass() const {
3131
return mass;
3232
}
3333

34-
SYCL_EXTERNAL float Actor::getRadius() {
34+
SYCL_EXTERNAL float Actor::getRadius() const {
3535
return radius;
3636
}
3737

38-
SYCL_EXTERNAL bool Actor::getAtDestination() {
38+
SYCL_EXTERNAL bool Actor::getAtDestination() const {
3939
return atDestination;
4040
}
4141

42-
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() {
42+
SYCL_EXTERNAL std::array<int, 3> Actor::getColor() const {
4343
return color;
4444
}
4545

external/Actor.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class Actor {
2222
public:
2323
Actor(vecType pPos, vecType pVelocity, vecType pDesiredVelocity, vecType pDestination, float pMass, float pRadius, bool pAtDestination, std::array<int, 3> pColor);
2424

25-
SYCL_EXTERNAL vecType getPos();
26-
SYCL_EXTERNAL vecType getVelocity();
27-
SYCL_EXTERNAL vecType getDesiredVelocity();
28-
SYCL_EXTERNAL vecType getDestination();
29-
SYCL_EXTERNAL float getMass();
30-
SYCL_EXTERNAL float getRadius();
31-
SYCL_EXTERNAL bool getAtDestination();
32-
SYCL_EXTERNAL std::array<int, 3> getColor();
25+
SYCL_EXTERNAL vecType getPos() const;
26+
SYCL_EXTERNAL vecType getVelocity() const;
27+
SYCL_EXTERNAL vecType getDesiredVelocity() const;
28+
SYCL_EXTERNAL vecType getDestination() const;
29+
SYCL_EXTERNAL float getMass() const;
30+
SYCL_EXTERNAL float getRadius() const;
31+
SYCL_EXTERNAL bool getAtDestination() const;
32+
SYCL_EXTERNAL std::array<int, 3> getColor() const;
3333

3434
SYCL_EXTERNAL void setPos(vecType newPos);
3535
SYCL_EXTERNAL void setVelocity(vecType newVelocity);

external/DifferentialEq.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ SYCL_EXTERNAL void differentialEq(int z, sycl::accessor<Actor, 1, sycl::access::
5454
vecType newPos = i->getPos();
5555
vecType destination = i->getDestination();
5656

57-
if (newPos[0] <= destination[0] + 0.01 && newPos[0] >= destination[0] - 0.01
58-
&& pos[1] <= destination[1] + 0.01 && pos[1] >= destination[1] - 0.01) {
57+
std::array<float, 4> destinationBoundingBox = {destination[0] + 0.01f, destination[0] - 0.01f, destination[1] + 0.01f, destination[1] - 0.01f};
58+
if (newPos[0] <= destinationBoundingBox[0] && newPos[0] >= destinationBoundingBox[1]
59+
&& pos[1] <= destinationBoundingBox[2] && pos[1] >= destinationBoundingBox[3]) {
5960
i->setAtDestination(true);
6061
}
6162
}

external/MathHelper.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ SYCL_EXTERNAL vecType getDirectionVector(vecType from, vecType to) {
44
return to - from;
55
}
66

7-
SYCL_EXTERNAL vecType velFromSpeedAndDir(float s, vecType d) {
8-
return d * (s / magnitude(d));
7+
SYCL_EXTERNAL vecType velFromSpeedAndDir(float speed, vecType direction) {
8+
return direction * (speed * inverseMagnitude(direction));
99
}
1010

11-
SYCL_EXTERNAL vecType velToPoint(float s, vecType pos, vecType destination) {
12-
return velFromSpeedAndDir(s, getDirectionVector(pos, destination));
11+
SYCL_EXTERNAL vecType velToPoint(float speed, vecType pos, vecType destination) {
12+
return velFromSpeedAndDir(speed, getDirectionVector(pos, destination));
1313
}
1414

1515
SYCL_EXTERNAL float magnitude(vecType inp) {
1616
return std::sqrt((inp[0] * inp[0]) + (inp[1] * inp[1]));
1717
}
1818

19+
SYCL_EXTERNAL float inverseMagnitude(vecType inp) {
20+
float x = (inp[0] * inp[0]) + (inp[1] * inp[1]);
21+
float y = x;
22+
uint32_t *i = reinterpret_cast<uint32_t *>(&y);
23+
const uint32_t expMask = 0x7F800000;
24+
const uint32_t magicNumber = 0x5F000000;
25+
*i = magicNumber - ((*i >> 1) & expMask);
26+
return (x * y * y + 1) / (2 * x * y);
27+
}
28+
1929
SYCL_EXTERNAL float dotProduct(vecType a, vecType b) {
2030
return (a[0] * b[0]) + (a[1] * b[1]);
2131
}
@@ -25,7 +35,7 @@ SYCL_EXTERNAL float distance(vecType from, vecType to) {
2535
}
2636

2737
SYCL_EXTERNAL vecType normalize(vecType inp) {
28-
return inp / magnitude(inp);
38+
return inp * inverseMagnitude(inp);
2939
}
3040

3141
SYCL_EXTERNAL std::pair<float, vecType> getDistanceAndNiw(vecType point, std::array<vecType, 2> wall) {

external/MathHelper.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ using vecType = std::array<float, 2>;
1010

1111
SYCL_EXTERNAL vecType getDirectionVector(vecType a, vecType b);
1212

13-
SYCL_EXTERNAL vecType velFromSpeedAndDir(float s, vecType d);
13+
SYCL_EXTERNAL vecType velFromSpeedAndDir(float speed, vecType direction);
1414

15-
SYCL_EXTERNAL vecType velToPoint(float s, vecType pos, vecType destination);
15+
SYCL_EXTERNAL vecType velToPoint(float speed, vecType pos, vecType destination);
1616

1717
SYCL_EXTERNAL float magnitude(vecType inp);
1818

19+
SYCL_EXTERNAL float inverseMagnitude(vecType inp);
20+
1921
SYCL_EXTERNAL float dotProduct(vecType a, vecType b);
2022

2123
SYCL_EXTERNAL float distance(vecType a, vecType b);

external/Room.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ Room::Room(std::vector<std::array<vecType, 2>> pWalls) {
44
walls = pWalls;
55
}
66

7-
std::vector<std::array<vecType, 2>> Room::getWalls() {
7+
std::vector<std::array<vecType, 2>> Room::getWalls() const {
88
return walls;
99
}

external/Room.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Room {
1414
public:
1515
Room(std::vector<std::array<vecType, 2>> pWalls);
1616

17-
std::vector<std::array<vecType, 2>> getWalls();
17+
std::vector<std::array<vecType, 2>> getWalls() const;
1818
};
1919

2020
#endif

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <sycl/sycl.hpp>
22
#include <SDL2/SDL.h>
33
#include <iostream>
4+
#include <chrono>
45

56
#include "Actor.hpp"
67
#include "Room.hpp"

0 commit comments

Comments
 (0)