Skip to content

Commit

Permalink
update lzc fix to relic PR (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjblanke authored May 8, 2020
1 parent 153294c commit e4a8015
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 86 deletions.
5 changes: 1 addition & 4 deletions contrib/relic/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (NOT INHERIT)
endif(NOT INHERIT)
string(TOLOWER ${INHERIT} INHERIT_PATH)

set(CORE_SRCS relic_err.c relic_core.c relic_conf.c relic_util.c)
set(CORE_SRCS relic_err.c relic_core.c relic_conf.c relic_util.c relic_lzcnt.c)

if (ARCH)
string(TOLOWER ${ARCH} ARCH_PATH)
Expand Down Expand Up @@ -40,7 +40,6 @@ file(GLOB BC_SRCS bc/*.c)
file(GLOB MD_SRCS md/*.c)
file(GLOB EC_SRCS ec/*.c)
file(GLOB PC_SRCS pc/*.c)
file(GLOB REFCODE_SRCS refcode/*.c)

set(RELIC_SRCS ${CORE_SRCS})

Expand Down Expand Up @@ -108,8 +107,6 @@ if (WITH_PC)
list(APPEND RELIC_SRCS ${PC_SRCS})
endif(WITH_PC)

list(APPEND RELIC_SRCS ${REFCODE_SRCS})

if (WITH_CP)
if (WITH_BN)
list(APPEND RELIC_SRCS "cp/relic_cp_rsa.c")
Expand Down
54 changes: 0 additions & 54 deletions contrib/relic/src/refcode/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Count leading zero bits. Choice of public domain or MIT-0.
Count leading zero bits.
David Reid - [email protected]
Expand All @@ -9,6 +9,35 @@ total size in bits will be returned (32 for lzcnt32 and 64 for lzcnt64).
For x86/64 platforms, this will use the LZCNT instruction if available. On ARM it will try the CLZ instruction. If these are unavailable it
will fall back to compiler-specific built-ins. If these are unavailable it'll fall back to the generic implementation.
License
=======
Public Domain (www.unlicense.org)
-------------------------------------------------
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Functions
---------
lzcnt32_generic
Expand Down Expand Up @@ -401,6 +430,7 @@ unsigned int lzcnt64_soft(unsigned long long x)
#endif
}

#if 0

unsigned int lzcnt32(unsigned int x)
{
Expand All @@ -414,7 +444,7 @@ unsigned int lzcnt32(unsigned int x)
}
}

unsigned int lzcnt64(unsigned int x)
unsigned int lzcnt64(unsigned long long x)
{
#if defined(HAS_LZCNT64_HARD)
if (has_lzcnt_hard()) {
Expand All @@ -426,7 +456,6 @@ unsigned int lzcnt64(unsigned int x)
}
}

/*
#include <stdio.h>
int do_test32(unsigned int (* lzcnt32proc)(unsigned int), const char* procName)
{
Expand Down Expand Up @@ -589,4 +618,6 @@ int main(int argc, char** argv)
(void)argv;
return exitCode;
}
*/

#endif // 0

45 changes: 21 additions & 24 deletions contrib/relic/src/relic_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
#include "relic_util.h"
#include "relic_types.h"

int has_lzcnt_hard();
unsigned int lzcnt64_soft(unsigned long long x);
unsigned int lzcnt64_hard(unsigned long long x);

int bLZCChecked = 0;
int bLZCHasHW = 0;

#if ARCH == ARM && OPSYS == DROID
#include <android/log.h>
#endif
Expand Down Expand Up @@ -141,6 +134,14 @@ char util_conv_char(dig_t i) {
#endif
}

int hasLZCHW = 0;
int checkedLZC = 0;
int has_lzcnt_hard();
unsigned int lzcnt32_soft(unsigned int x);
unsigned int lzcnt32_hard(unsigned int x);
unsigned int lzcnt64_soft(unsigned long long x);
unsigned int lzcnt64_hard(unsigned long long x);

int util_bits_dig(dig_t a) {
#if WSIZE == 8 || WSIZE == 16
static const uint8_t table[16] = {
Expand Down Expand Up @@ -169,24 +170,20 @@ int util_bits_dig(dig_t a) {
return table[a >> 4] + 4 + offset;
}
return 0;
#elif WSIZE == 32
#ifdef _MSC_VER
return RLC_DIG - __lzcnt(a);
#else
return RLC_DIG - __builtin_clz(a);
#else // WSIZE == 32 or WSIZE == 64
if (checkedLZC == 0) {
hasLZCHW = has_lzcnt_hard();
checkedLZC = 1;
}
#if WSIZE == 32
if (hasLZCHW!=0)
return RLC_DIG - lzcnt32_hard(a);
return RLC_DIG - lzcnt32_soft(a);
#else // WSIZE == 64
if (hasLZCHW!=0)
return RLC_DIG - lzcnt64_hard(a);
return RLC_DIG - lzcnt64_soft(a);
#endif
#elif WSIZE == 64
if (bLZCChecked == 0)
{
bLZCHasHW = has_lzcnt_hard();
// printf("Hardware LZCNT %d\n", bLZCHasHW);
bLZCChecked = 1;
}

if (bLZCHasHW!=0)
return RLC_DIG - lzcnt64_hard(a);

return RLC_DIG - lzcnt64_soft(a);
#endif
}

Expand Down

0 comments on commit e4a8015

Please sign in to comment.