diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5b02c5e --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +ColumnLimit: 1000 diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..edfd275 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=c++23] \ No newline at end of file diff --git a/board.hpp b/board.hpp new file mode 100644 index 0000000..33da163 --- /dev/null +++ b/board.hpp @@ -0,0 +1,52 @@ +#pragma once +#include "vector2d.hpp" + +struct Obstacle { + Vector2d start; + Vector2d end; + + Obstacle() : start(0, 0), end(0, 0) {} + Obstacle(Vector2d s, Vector2d e) : start(s), end(e) {} + + // Find the point of the obstacle that is nearest to p + // # 15 flops + Vector2d find_nearest_obstacle_point(Vector2d p) { + + // # 2 ADDs + Vector2d obstacle_v = end - start; + + // # 9 flops = 2 ADDs, 2 MULTs, 1 ADD, 1 DIV, 2 MULTs, 1 ADD + double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v); + + if (projection < 0.0) + projection = 0.0; + else if (projection > 1.0) + projection = 1.0; + + // # 4 flops = 2 ADDs, 2 MULTs + return start + obstacle_v * projection; + + } + +}; + +struct Board { + double length, width; + Obstacle *obstacles; // array of obstacles (represented as lines) that are present on the board + int n_obstacles; // number of obstacles + + Board(double l, double w, int n, Obstacle *o) : length(l), width(w), n_obstacles(n + 4) { + obstacles = new Obstacle[n + 4]; + for (int i = 0; i < n; i++) { + obstacles[i] = o[i]; + } + + // borders of the board: bottom, right, top, left + obstacles[n] = Obstacle(Vector2d(0, 0), Vector2d(l, 0)); + obstacles[n + 1] = Obstacle(Vector2d(l, 0), Vector2d(l, w)); + obstacles[n + 2] = Obstacle(Vector2d(l, w), Vector2d(0, w)); + obstacles[n + 3] = Obstacle(Vector2d(0, w), Vector2d(0, 0)); + } + + ~Board() { delete[] obstacles; } +}; diff --git a/forces.cpp b/forces.cpp index 785f6be..5911909 100644 --- a/forces.cpp +++ b/forces.cpp @@ -1,132 +1,94 @@ -#include +#include "forces.hpp" #include -// return the direction between curr position and destination -// $\vectorarrow{e}_\alpha(t)$ -Vector2d get_desired_direction(Pedestrian &p) -{ - Vector2d diff = p.desired_next_node - p.curr_pos; - double length = diff.norm(); - return (p.desired_next_node - p.curr_pos) / length; -} - -// return the direction between curr position and destination -// $\vectorarrow{e}_\alpha(t)$ -Vector2d get_actual_direction(Pedestrian &p) -{ - Vector2d diff = p.actual_next_node - p.curr_pos; - double length = diff.norm(); - return (p.actual_next_node - p.curr_pos) / length; -} - -// double w(Vector2d e, Vector2d f) { -// -// if (e.dot(f) >= f.norm() * PHI_DEG) return 1.0; -// return C; -// -// } -double w(Vector2d e, Vector2d f) -{ - // The w() function should check if the angle between the desired direction e and the force f is within the field of view. - // Per equation (7), the condition is: - // e · f ≥ ||f|| * cos(phi) - // where phi = 100° (half of the 200° field of view (PHI_DEG)). - const double phi = (PHI_DEG / 2.0) * M_PI / 180.0; // half-angle in radians - if (e.dot(f) >= f.norm() * cos(phi)) +// # 8 flops = 5 MULTs, 2 ADD, 1 SQRT +double w(Vector2d e, Vector2d f) { + if (e.dot(f) >= f.norm() * cos_phi) return 1.0; - return C; + else + return c; } -// F_i is the current position resp. direction vector of pedestrian p in time i? -// -// $\vectorarrow{F}_\alpha(\vectorarrow{v}_\alpha, v^0_\alpha \vectorarrow{e}_\alpha) \vectorarrow{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vectorarrow{e}_\alpha - \vectorarrow{v}_\alpha)$ -// -void compute_acceleration_term(Pedestrian *p) -{ - - Vector2d desired_velocity = get_desired_direction(*p) * p->desired_v0; - - // TODO: How to get this number - // Probably needs to lookup the map and see if the desired position is reachable - Vector2d actual_velocity = get_actual_direction(*p) * p->actual_v0; - - double tau_inv = 1 / TAU; - - p->F_i += (desired_velocity - actual_velocity) * tau_inv; -} - -// $\vectorarrow{f}_{\alpha \beta}(\vectorarrow{r}_{\alpha \beta}) = -\gradient_{\vectorarrow{r}_{\alpha \beta}} V_{\alpha \beta}[b(\vectorarrow{r}_{\alpha \beta})]$ -void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N) -{ - double delta_t = 0.5; // assuming 0.5 seconds steps - for (int i = 0; i < N; i++) - { - // TODO: add check for $\vectorarrow{e} * \vectorarrow{f} >= \Arrowvert \vectorarrow{f} \Arrowvert \cos(\phi) $ - // only consider close pedestrians - if (P + i == p) - continue; - if ((P[i].curr_pos - p->curr_pos).norm() < DISTANCE) - { - // - // $2b = \sqrt{(\Arrowvert \vectorarrow{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vectorarrow{r}_{\alpha \beta} - v_\beta \Delta t \vectorarrow{e}_\beta \Arrowvert)^2 - (v_\beta \Delta t)^2}$ - // - // $\Arrowvert \vectorarrow{r}_{\alpha \beta} \Arrowvert$ - Vector2d r_ab = p->curr_pos - P[i].curr_pos; - double r_ab_norm = (p->curr_pos - P[i].curr_pos).norm(); - - // $\Arrowvert \vectorarrow{r}_{\alpha \beta} - v_\beta \Delta t \vectorarrow{e}_\beta \Arrowvert$ - Vector2d e_beta = get_actual_direction(P[i]); - Vector2d r_ab_something = p->curr_pos - P[i].curr_pos - (e_beta * (P[i].actual_v0 * delta_t)); - double r_ab_something_norm = (p->curr_pos - P[i].curr_pos - (e_beta * (P[i].actual_v0 * delta_t))).norm(); - - // $v_\beta \Delta t$ - double v_beta_delta_t = P[i].actual_v0 * delta_t; - - double b = sqrt((r_ab_norm + r_ab_something_norm) * (r_ab_norm + r_ab_something_norm) - (v_beta_delta_t) * (v_beta_delta_t)) / 2; - - // - // $\vectorarrow{f}_{\alpha \beta}(\vectorarrow{r}_{\alpha \beta}) = \frac{V^0_{\alpha \beta}}{\sigma}e^{-b / \sigma} \grad_{\vectorarrow{r}_{\alpha \beta}}b$ - // - // TODO: what is $V^0_{\alpha \beta}$ probably some number? - // - // - // $\grad_{\vectorarrow{r}_{\alpha \beta}} = \frac{(\Arrowvert \vectorarrow{r}_{\alpha \beta} \Arrowvert + \Arrowvert \vectorarrow{r}_{\alpha \beta} - v_\beta \Delta t \vectorarrow{e}_\beta \Arrowvert) (\frac{\vectorarrow{r}_{\alpha \beta}}{\Arrowvert \vectorarrow{r}_{\alpha \beta} \Arrowvert} + \frac{\vectorarrow{r}_{\alpha \beta} - v_\beta \Delta t \vectorarrow{e}_\beta}{\Arrowvert \vectorarrow{r}_{\alpha \beta} - v_\beta \Delta t \vectorarrow{e}_\beta \Arrowvert})}{4b}$ - // - Vector2d delta_r_alpha_beta = (((r_ab / r_ab_norm) + (r_ab_something / r_ab_something_norm)) * (r_ab_norm + r_ab_something_norm)) / (4 * b); - - // $\vectorarrow{f}_{\alpha \beta}$ - Vector2d f_alpha_beta_r_alpha_beta = delta_r_alpha_beta * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); - - p->F_i += f_alpha_beta_r_alpha_beta * w(get_desired_direction(*p), f_alpha_beta_r_alpha_beta * -1); - } +// # 2 DIVs +double g(double v_max_alpha, double w_alpha_norm) { + if (w_alpha_norm <= v_max_alpha) { + return 1; } + return v_max_alpha / w_alpha_norm; } -// TODO: missing -void compute_repulsive_effects_from_borders(Sidewalk &S, Pedestrian *p) -{ +// # 8 flops +void compute_acceleration_term(Pedestrian *p) { + // # 2 flops + Vector2d desired_velocity = p->e * p->desired_v0; + Vector2d actual_velocity = p->curr_vel; + // # 6 flops = 4 ADDs, 2 DIVs + p->f_i += (desired_velocity - actual_velocity) / tau_alpha; +} - Vector2d *obstacles = S.obstacles; - int n_obs = S.n_obstacles; +// # 55(N - 1) = 55N - 55 flops +void compute_repulsive_effects_from_pedestrians(Pedestrian *pedestrians, Pedestrian *p, int N) { - for (int i = 0; i < n_obs; i++) - { - Vector2d obstacle = obstacles[i]; - // TODO: find out the variables and parameters - // - // $\vectorarrow{F}_{\alpha B}(\vectorarrow{r}_{\alpha B}) = \frac{U^0_{\alpha B}}{R} e^{- \Arrowvert \vectorarrow{r}_{\alpha B} \Arrowvert / R} \frac{\vectorarrow{r}_{\alpha B}}{\Arrowvert \vectorarrow{r}_{\alpha B} \Arrowvert}$ - // - // double e = 2.718; - // we could just use exp + for (int i = 0; i < N; i++) { - // $\vectorarrow{r}_{\alpha B}$ - Vector2d r_ab = p->curr_pos - obstacle; - // $\Arrowvert \vectorarrow{r}_{\alpha B} \Arrowvert$ + if (pedestrians + i == p) + continue; + + // # Optional: Do not consider far away pedestrians from current pedestrian + // # Might get better speed up, remove if not + // if ((pedestrians[i].curr_pos - p->curr_pos).norm() > DISTANCE) { + // continue; + // } + + // # 2 ADDs + Vector2d r_ab = p->curr_pos - pedestrians[i].curr_pos; + + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD double r_ab_norm = r_ab.norm(); - if (r_ab_norm < 1e-6) - continue; - // repulsive force vector (Direction * Magnitude) (Edquation 6) - Vector2d f_ab = (r_ab / r_ab_norm) * (U0_AB / R) * exp(-r_ab_norm / R); - p->F_i += f_ab * w(get_desired_direction(*p), f_ab * -1); + + Vector2d e_b = pedestrians[i].e; + + // # 2 MULTs + double s_b = pedestrians[i].desired_v0 * delta_t; + + // # 4 flops = 2 ADDs, 2 MULTs + Vector2d r_ab2 = r_ab - (e_b * s_b); + + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD + double r_ab2_norm = r_ab2.norm(); + + // # 7 flops = 1 SQRT, 3 ADDs, 2 MULTs, 1 DIV + double b = sqrt((r_ab_norm + r_ab2_norm) * (r_ab_norm + r_ab2_norm) - s_b * s_b) / 2; + + // # 12 flops = 6 DIVs, 3 ADDs, 3 MULTs + Vector2d b_grad = (r_ab / r_ab_norm + r_ab2 / r_ab2_norm) * (r_ab_norm + r_ab2_norm) / (4 * b); + + // # 6 flops = 3 MULTs, 2 DIVs, 1 EXP + Vector2d f_ped = b_grad * ((V0_AB / SIGMA) * (exp(-b / SIGMA))); + + // # 14 flops: 9 MULTs, 4 ADDs, 1 SQRT + p->f_i += f_ped * w(p->e, f_ped * -1); + } +} + +// # Assume that the four borders are also 4 obstacles +// # 36 n_obstacles flops = 144 flops +void compute_repulsive_effects_from_borders(Board &board, Pedestrian *p) { + + Obstacle *obstacles = board.obstacles; + int n_borders = board.n_obstacles; + + for (int i = 0; i < n_borders; i++) { + + Obstacle obstacle = obstacles[i]; + + // # 17 flops + Vector2d r_aB = p->curr_pos - obstacle.find_nearest_obstacle_point(p->curr_pos); + + // # 17 flops = 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD, 2 MULTs, 1 DIV, 2 MULTs, 1 EXP, 1 SQRT, 2 MULTs, 1 ADD, 1 DIV + Vector2d f_obst = (r_aB / r_aB.norm()) * (U0_AB / R) * std::exp(-r_aB.norm() / R); + + // # 2 ADDs + p->f_i += f_obst; } } diff --git a/forces.hpp b/forces.hpp new file mode 100644 index 0000000..3d2bd4a --- /dev/null +++ b/forces.hpp @@ -0,0 +1,37 @@ +#pragma once +#include "board.hpp" +#include "pedestrian.hpp" +#include + +const double tau_alpha = 0.5; + +const double V0_AB = 2.1; + +const double SIGMA = 0.3; + +const double U0_AB = 10.0; + +const double R = 0.2; + +const double c = 0.5; + +const double phi = 100.0 / 180.0 * M_PI; + +const double cos_phi = cos(phi); + +const double delta_t = 2; + +const double dt = 0.01; + +// # Optional (forces.cpp: compute_repulsive_effects_from_pedestrians) +// const double DISTANCE = 2.0; + +double w(Vector2d e, Vector2d f); + +double g(double v_max_alpha, double w_alpha_norm); + +void compute_acceleration_term(Pedestrian *p); + +void compute_repulsive_effects_from_pedestrians(Pedestrian *P, Pedestrian *p, int N); + +void compute_repulsive_effects_from_borders(Board &B, Pedestrian *p); diff --git a/main b/main new file mode 100755 index 0000000..6df5076 Binary files /dev/null and b/main differ diff --git a/main.cpp b/main.cpp index d9af3d6..6e00475 100644 --- a/main.cpp +++ b/main.cpp @@ -1,130 +1,111 @@ -#include +#include "board.hpp" +#include "forces.hpp" +#include "math.h" +#include "pedestrian.hpp" +#include "tsc_x86.h" +#include "vector2d.hpp" #include -#include -#include -#include -#include -#include + +#define BOARD_WIDTH 10.0 +#define BOARD_LENGTH 50.0 using namespace std; // Numer of pedestrians // $n_p$ -const int n_p = 100; +// const int n_p = 100; // Number of iterations // $n_{iter}$ -const int n_iter = 100; +// const int n_iter = 100; -Pedestrian *init_pedestrians() -{ +Pedestrian *init_pedestrians(int n_p) { Pedestrian *P = new Pedestrian[n_p]; - for (int i = 0; i < n_p; i++) - { - P[i] = Pedestrian(); + std::random_device rd; // use for truly random simulation + std::mt19937 gen(rd()); // use rd instead of 42 as seed for real randomness + std::normal_distribution velocity_gen(1.34, 0.26); // 1.34 and 0.26 are the mean and stdrd deviation used in paper + std::uniform_real_distribution x_gen(2, BOARD_LENGTH - 2); + std::uniform_real_distribution y_gen(2, BOARD_WIDTH - 2); + for (int i = 0; i < n_p / 2; i++) { + Vector2d start_pos = Vector2d(1, y_gen(gen)); + Vector2d destination = Vector2d(BOARD_LENGTH - 1, y_gen(gen)); + double desired_v0 = velocity_gen(gen); + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(WHITE, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d()); + } + + for (int i = n_p / 2; i < n_p; i++) { + Vector2d start_pos = Vector2d(BOARD_LENGTH - 1, y_gen(gen)); + Vector2d destination = Vector2d(1, y_gen(gen)); + double desired_v0 = velocity_gen(gen); + double v_max = 1.3 * desired_v0; + P[i] = Pedestrian(BLACK, (destination - start_pos) / (destination - start_pos).norm() * desired_v0, start_pos, (destination - start_pos) / (destination - start_pos).norm(), v_max, desired_v0, destination, Vector2d(0, 0)); } return P; } -Sidewalk init_sidewalk(int width, int length) -{ +int main(int argc, const char *argv[]) { - Vector2d obs[] = {Vector2d(2.0, 1.0), Vector2d(4.0, 1.0), Vector2d(6.0, 2.0), Vector2d(6.0, 4.0)}; - return Sidewalk(width, length, 4, obs); -} + Obstacle obs[] = {}; + Board board = Board(BOARD_LENGTH, BOARD_WIDTH, 0, obs); -int main() -{ + int n_p = atoi(argv[1]); + int n_iter = atoi(argv[2]); + Pedestrian *pedestrians = init_pedestrians(n_p); - // ! DONT TOUCH + // # Start here or right after function signature + uint64_t start = start_tsc(); - // $\vec{v}^0_i$ - // Velocity at iteration $t$ for pedestrian $i$ - double v[n_iter][n_p]; + // # (8 + 55N - 55 + 144 + 4 + 8 + 4 + 10)*n_p*n_iter + for (int t = 0; t < n_iter; t++) { + // Pass1: conmpute all forces from the current state + for (int i = 0; i < n_p; i++) { - // $\vec{r}^0_i$ - // Intended destination at iteration $t$ for pedestrian $i$ - Vector2d r[n_iter][n_p]; + Pedestrian &p = pedestrians[i]; - // $\vec{e}_i$ - // Desired direction to the next way point at iteration $t$ for pedestrian $i$ - Vector2d e[n_iter][n_p]; + p.f_i.reset(); - // $v_i^{max}$ - // Maximum allowed speed for pedestrian $i$ - double v_max[n_p]; - - // $v_i^{0}$ - // Desired speed for pedestrian $i$ - double v_0[n_p]; - - // $\vec{F}_i$ - // Total social force for pedestrian $i$ - Vector2d F[n_p][n_p]; - - // $\vec{r}_0$ - // Desired destination of pedestrian $n_p$ - Vector2d r_0[n_p]; - - // create sidewalk - Sidewalk S = init_sidewalk(10, 15); - - // store Array of Structs, store all pedestrians - Pedestrian *P = init_pedestrians(); - - // Our time unit (how long a timestep goes) - double dt = 1; - - // for each time frame - for (int t = 0; t < n_iter; t++) - { - // for each pedestrian - for (int i = 0; i < n_p; i++) - { - // update... - Pedestrian &p = P[i]; - - // reset total social force - p.F_i.reset(); - - // - // $\vectorarrow{F}_\alpha(\vectorarrow{v}_\alpha, v^0_\alpha \vectorarrow{e}_\alpha) \vectorarrow{e}_\alpha(t) = \frac{1}{\tau_\alpha}(v^0_\alpha \vectorarrow{e}_\alpha - \vectorarrow{v}_\alpha)$ - // + // # 8 flops compute_acceleration_term(&p); - // $\vectorarrow{f}_{\alpha \beta}(\vectorarrow{r}_{\alpha \beta}) = -\gradient_{\vectorarrow{r}_{\alpha \beta}} V_{\alpha \beta}[b(\vectorarrow{r}_{\alpha \beta})]$ - compute_repulsive_effects_from_pedestrians(P, &p, n_p); + // # 55N - 55 flops + compute_repulsive_effects_from_pedestrians(pedestrians, &p, n_p); - // TODO: finalize this method - // - // $\vectorarrow{F}_{\alpha B}(\vectorarrow{r}_{\alpha B}) = \frac{U^0_{\alpha B}}{R} e^{- \Arrowvert \vectorarrow{r}_{\alpha B} \Arrowvert / R} \frac{\vectorarrow{r}_{\alpha B}}{\Arrowvert \vectorarrow{r}_{\alpha B} \Arrowvert}$ - // - compute_repulsive_effects_from_borders(S, &p); - - // TODO: Update $\vec{w}_i$ - // $\vectorarrow{w}_i += \vectorarrow{v}_i + \vectorarrow{F}_idt$ - // Intermediate velocity: current_velocity + force impulse (Force * time) (Equation1) - Vector2d w_i = p.curr_vel + p.F_i * dt; - - // TODO: Update $\vec{v}_i^{t+1}$ - // - // $\vec{v}_i^{t+1} += \vectorarrow{w}_ig(\frac{v^{max}_i}{\Arrowvert \vectorarrow{w}_i \Arrowvert}) $ - // Limit velocity to vMax - double w_norm = w_i.norm(); - double g = (w_norm < 1e-6) ? 1.0 : fmin(1.0, p.vMax / w_norm); - p.next_vel = w_i * g; - - // TODO: Update $\vec{r}_i^{t+1}$ - // $\vec{r}_i^{t+1} += \vectorarrow{r}^t_i + v^t_idt$ - p.next_pos = p.curr_pos + p.curr_vel * dt; + // # 144 flops + compute_repulsive_effects_from_borders(board, &p); } + // Pass2: integrate the forces and update the current state + for (int i = 0; i < n_p; i++) { + Pedestrian &p = pedestrians[i]; + // # 4 flops = 2 ADDs, 2 MULTs + Vector2d w_alpha = p.curr_vel + p.f_i * dt; - for (int i = 0; i < n_p; i++) - { - // move each pedestrian to their next position - P[i].update(); + // # 8 flops = 2 MULTs, 2 DIVs, 1 SQRT, 2 MULTs, 1 ADD + p.curr_vel = w_alpha * g(p.v_max, w_alpha.norm()); + + // # 4 flops = 2 ADDs, 2 MULTs + p.curr_pos = p.curr_pos + p.curr_vel * dt; + + // # 10 flops = 5 ADDs, 2 DIVs, 1 SQRT, 2 MULTs + p.e = (p.destination - p.curr_pos) / (p.destination - p.curr_pos).norm(); } } + int W = (8 + 55 * n_p - 55 + 144 + 4 + 8 + 4 + 10) * n_p * n_iter; + std::cout << "W(n): "; + std::cout << W; + std::cout << " flops\n"; + double T = stop_tsc(start); + std::cout << "T(n): "; + std::cout << T; + std::cout << " cycles\n"; + + double P = W / T; + + std::cout << "P(n): "; + std::cout << P; + std::cout << " flops/cycle\n"; + + delete[] pedestrians; return 0; } diff --git a/pedestrian.hpp b/pedestrian.hpp new file mode 100644 index 0000000..54b3abe --- /dev/null +++ b/pedestrian.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "random" +#include "vector2d.hpp" + +// For visual simulation +enum Color { BLACK, WHITE }; + +struct Pedestrian { + Color color; + Vector2d curr_vel; + Vector2d curr_pos; + Vector2d e; + double v_max; + double desired_v0; + Vector2d destination; + Vector2d f_i; + + Pedestrian() : color(WHITE), curr_vel(0.0, 0.0), curr_pos(0.0, 0.0), e(0.0, 0.0), v_max(0.0), desired_v0(0.0), destination(0.0, 0.0), f_i(0.0, 0.0) {} + + Pedestrian(Color clr, Vector2d c_v, Vector2d c_p, Vector2d e, double v_max, double desired_v0, Vector2d d, Vector2d f) : color(clr), curr_vel(c_v), curr_pos(c_p), e(e), v_max(v_max), desired_v0(desired_v0), destination(d), f_i(f) {} +}; diff --git a/questions.md b/questions.md new file mode 100644 index 0000000..6de7008 --- /dev/null +++ b/questions.md @@ -0,0 +1,6 @@ +- [ ] SQRT == 1 flop? EXP == 1 flop? +- [ ] Where to start measurements, do we include measurements? +- [ ] Can Vector2d be reused? +- [ ] How precise do we have to be? How do we define "correctness"? +- [ ] Do borders count as obstacles? +- [ ] Do we use a fixed machine, frequency scaling, etc. diff --git a/tsc_x86.h b/tsc_x86.h index 4e4baf4..be144a3 100644 --- a/tsc_x86.h +++ b/tsc_x86.h @@ -1,24 +1,24 @@ /* ==================== GNU C and possibly other UNIX compilers ===================== */ - #ifndef _WIN32 +#ifndef _WIN32 - #if defined(__GNUC__) || defined(__linux__) - #define VOLATILE __volatile__ - #define ASM __asm__ - #else - /* if we're neither compiling with gcc or under linux, we can hope - * the following lines work, they probably won't */ - #define ASM asm - #define VOLATILE - #endif +#if defined(__GNUC__) || defined(__linux__) +#define VOLATILE __volatile__ +#define ASM __asm__ +#else +/* if we're neither compiling with gcc or under linux, we can hope + * the following lines work, they probably won't */ +#define ASM asm +#define VOLATILE +#endif - #define myInt64 unsigned long long - #define INT32 unsigned int +#define myInt64 unsigned long long +#define INT32 unsigned int /* ======================== WIN32 ======================= */ #else - #define myInt64 signed __int64 - #define INT32 unsigned __int32 +#define myInt64 signed __int64 +#define INT32 unsigned __int32 #endif @@ -27,68 +27,57 @@ * Time Stamp Counter. The Intel manuals contain more information. */ - #define COUNTER_LO(a) ((a).int32.lo) #define COUNTER_HI(a) ((a).int32.hi) #define COUNTER_VAL(a) ((a).int64) -#define COUNTER(a) \ - ((unsigned long long)COUNTER_VAL(a)) +#define COUNTER(a) ((unsigned long long)COUNTER_VAL(a)) -#define COUNTER_DIFF(a,b) \ - (COUNTER(a)-COUNTER(b)) +#define COUNTER_DIFF(a, b) (COUNTER(a) - COUNTER(b)) /* ==================== GNU C and possibly other UNIX compilers ===================== */ #ifndef _WIN32 - typedef union - { myInt64 int64; - struct {INT32 lo, hi;} int32; - } tsc_counter; +typedef union { + myInt64 int64; + struct { + INT32 lo, hi; + } int32; +} tsc_counter; - #define RDTSC(cpu_c) \ - ASM VOLATILE ("rdtsc" : "=a" ((cpu_c).int32.lo), "=d"((cpu_c).int32.hi)) - #define CPUID() \ - ASM VOLATILE ("cpuid" : : "a" (0) : "bx", "cx", "dx" ) +#define RDTSC(cpu_c) ASM VOLATILE("rdtsc" : "=a"((cpu_c).int32.lo), "=d"((cpu_c).int32.hi)) +#define CPUID() ASM VOLATILE("cpuid" : : "a"(0) : "bx", "cx", "dx") /* ======================== WIN32 ======================= */ #else - typedef union - { myInt64 int64; - struct {INT32 lo, hi;} int32; - } tsc_counter; +typedef union { + myInt64 int64; + struct { + INT32 lo, hi; + } int32; +} tsc_counter; - #define RDTSC(cpu_c) \ - { __asm rdtsc \ - __asm mov (cpu_c).int32.lo,eax \ - __asm mov (cpu_c).int32.hi,edx \ - } +#define RDTSC(cpu_c) {__asm rdtsc __asm mov(cpu_c).int32.lo, eax __asm mov(cpu_c).int32.hi, edx} - #define CPUID() \ - { \ - __asm mov eax, 0 \ - __asm cpuid \ - } +#define CPUID() {__asm mov eax, 0 __asm cpuid} #endif - static void init_tsc() { - ; // no need to initialize anything for x86 + ; // no need to initialize anything for x86 } static myInt64 start_tsc(void) { - tsc_counter start; - CPUID(); - RDTSC(start); - return COUNTER_VAL(start); + tsc_counter start; + CPUID(); + RDTSC(start); + return COUNTER_VAL(start); } static myInt64 stop_tsc(myInt64 start) { - tsc_counter end; - RDTSC(end); - CPUID(); - return COUNTER_VAL(end) - start; + tsc_counter end; + RDTSC(end); + CPUID(); + return COUNTER_VAL(end) - start; } - diff --git a/vector2d.hpp b/vector2d.hpp new file mode 100644 index 0000000..b9bf1f0 --- /dev/null +++ b/vector2d.hpp @@ -0,0 +1,26 @@ +#pragma once +#include + +struct Vector2d { + double x, y; + Vector2d(double a, double b) : x(a), y(b) {} + Vector2d() : x(.0), y(.0) {} + Vector2d operator+(Vector2d v) const { return Vector2d(v.x + x, v.y + y); } + Vector2d operator-(Vector2d v) const { return Vector2d(x - v.x, y - v.y); } + Vector2d operator-(double a) const { return Vector2d(x - a, y - a); } + Vector2d operator+(double a) const { return Vector2d(x + a, y + a); } + Vector2d operator/(double a) const { return Vector2d(x / a, y / a); } + Vector2d operator*(double a) const { return Vector2d(x * a, y * a); } + Vector2d operator+=(const Vector2d &v) { + *this = *this + v; + return *this; + } + // # 4 flops = 1 SQRT, 2 MULTs, 1 ADD + double norm() { return sqrt(x * x + y * y); } + // # 3 flops = 2 MULTs, 1 ADD + double dot(Vector2d v) { return x * v.x + y * v.y; } + void reset() { + x = 0; + y = 0; + } +};