Skip to content

Commit

Permalink
Added new algorithm for Jacobi symbol.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Jul 4, 2024
1 parent f6df7a2 commit a65e5ef
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 29 deletions.
5 changes: 5 additions & 0 deletions include/relic_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ ull_t arch_cycles(void);
*/
uint_t arch_lzcnt(dig_t);

/**
* Return the number of trailing zeros in an integer.
*/
uint_t arch_tzcnt(dig_t);

#if ARCH == AVR

/**
Expand Down
2 changes: 2 additions & 0 deletions include/relic_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,10 @@ typedef struct _ctx_t {
/** Function pointer to underlying lznct implementation. */
#if ARCH == X86
unsigned int (*lzcnt_ptr)(dig_t);
unsigned int (*tzcnt_ptr)(dig_t);
#elif ARCH == X64 || ARCH == A64
unsigned int (*lzcnt_ptr)(ull_t);
unsigned int (*tzcnt_ptr)(ull_t);
#endif
} ctx_t;

Expand Down
8 changes: 8 additions & 0 deletions src/arch/relic_arch_a64.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand Down Expand Up @@ -177,6 +178,8 @@ void arch_init(void) {
if (ctx != NULL) {
core_get()->lzcnt_ptr =
(has_lzcnt_hard() ? lzcnt64_hard : lzcnt64_soft);
core_get()->tzcnt_ptr =
(has_lzcnt_hard() ? tzcnt64_hard : tzcnt64_soft);
}

#if TIMER == CYCLE
Expand All @@ -199,6 +202,7 @@ void arch_clean(void) {
ctx_t *ctx = core_get();
if (ctx != NULL) {
core_get()->lzcnt_ptr = NULL;
core_get()->tzcnt_ptr = NULL;
}
}

Expand Down Expand Up @@ -234,3 +238,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}
9 changes: 9 additions & 0 deletions src/arch/relic_arch_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "relic_types.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand Down Expand Up @@ -111,3 +112,11 @@ uint_t arch_lzcnt(uint_t x) {
return lzcnt64_gcc_arm(x);
#endif
}

uint_t arch_tzcnt(uint_t x) {
#ifdef WSIZE == 32
return tzcnt32_gcc_arm(x);
#elif WSIZE == 64
return tzcnt64_gcc_arm(x);
#endif
}
12 changes: 12 additions & 0 deletions src/arch/relic_arch_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ uint_t arch_lzcnt() {
}
return 0;
}

uint_t arch_tzcnt() {
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
}
29 changes: 29 additions & 0 deletions src/arch/relic_arch_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,32 @@ uint_t arch_lzcnt() {
return 0;
#endif
}

uint_t arch_tzcnt() {
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
#if WSIZE == 8
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
#elif WSIZE == 16
int offset;

if (a & 0xFF == 0) {
offset = 8;
} else {
offset = 0;
}
a = a >> offset;
if (a >> 4 != 0) {
return table[a & 0xF] + offset;
} else {
return table[a >> 4] + 4 + offset;
}
return 0;
#endif
}
43 changes: 43 additions & 0 deletions src/arch/relic_arch_none.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,46 @@ uint_t arch_lzcnt(dig_t a) {
#endif
#endif
}

uint_t arch_tzcnt(dig_t a) {
#if WSIZE == 8 || WSIZE == 16
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
#endif
#if WSIZE == 8
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
#elif WSIZE == 16
int offset;

if (a & 0xFF == 0) {
offset = 8;
} else {
offset = 0;
}
a = a >> offset;
if (a >> 4 != 0) {
return table[a & 0xF] + offset;
} else {
return table[a >> 4] + 4 + offset;
}
return 0;
#elif WSIZE == 32
#ifdef _MSC_VER
return __tzcnt(a);
#else
return __builtin_ctz(a);
#endif
#elif WSIZE == 64
#ifdef _MSC_VER
return __tzcnt64(a);
#else
return __builtin_ctzll(a);
#endif
#endif
}
8 changes: 8 additions & 0 deletions src/arch/relic_arch_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand All @@ -51,13 +52,16 @@ void arch_init(void) {
if (ctx != NULL) {
core_get()->lzcnt_ptr =
(has_lzcnt_hard() ? lzcnt64_hard : lzcnt64_soft);
core_get()->tzcnt_ptr =
(has_tzcnt_hard() ? tzcnt64_hard : tzcnt64_soft);
}
}

void arch_clean(void) {
ctx_t *ctx = core_get();
if (ctx != NULL) {
core_get()->lzcnt_ptr = NULL;
core_get()->tzcnt_ptr = NULL;
}
}

Expand Down Expand Up @@ -103,3 +107,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}
6 changes: 6 additions & 0 deletions src/arch/relic_arch_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/*============================================================================*/
/* Public definitions */
/*============================================================================*/

void arch_init(void) {
core_get()->lzcnt_ptr = (has_lzcnt_hard() ? lzcnt32_hard : lzcnt32_soft);
core_get()->tzcnt_ptr = (has_tzcnt_hard() ? tzcnt32_hard : tzcnt32_soft);
}

void arch_clean(void) {
Expand All @@ -64,3 +66,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((uint32_t)x) - (8 * sizeof(uint32_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr((uint32_t)x) - (8 * sizeof(uint32_t) - WSIZE);
}
Loading

0 comments on commit a65e5ef

Please sign in to comment.