Skip to content

Commit

Permalink
fix: binary gcd trailing_zeros macro for visual studio
Browse files Browse the repository at this point in the history
  • Loading branch information
tolis-0 committed Dec 2, 2023
1 parent 5b38d9a commit eb9fc2c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions math/binary_gcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <intrin.h>
#define trailing_zeros(x) _tzcnt_u64(x)
#else
#define trailing_zeros(x) __builtin_ctzll(x)
#endif

/**
* @brief Get greatest common divisor of u and v without division
Expand All @@ -33,8 +39,8 @@ unsigned long binary_gcd(unsigned long u, unsigned long v)

// get number of trailing zero bits
// essentially powers of 2 in prime factorization
u_tz = __builtin_ctzll(u);
v_tz = __builtin_ctzll(v);
u_tz = trailing_zeros(u);
v_tz = trailing_zeros(v);

// keep minimum of powers of 2 because gcd(2u, 2v) = 2·gcd(u, v)
shift = (u_tz < v_tz) ? u_tz : v_tz;
Expand All @@ -46,7 +52,7 @@ unsigned long binary_gcd(unsigned long u, unsigned long v)
{
tmp = (u > v) ? u - v : v - u; // |u - v|
v = (u > v) ? v : u; // min(u, v)
u_tz = __builtin_ctzll(tmp);
u_tz = trailing_zeros(tmp);
u = tmp >> u_tz;
} while (u != 0);

Expand Down

0 comments on commit eb9fc2c

Please sign in to comment.