make style

This commit is contained in:
Philippe Teuwen 2021-01-14 11:39:45 +01:00
parent 2fbdada593
commit f2bc066858
33 changed files with 4734 additions and 5173 deletions

View file

@ -506,7 +506,7 @@ Check column "offline" for their availability.
|command |offline |description |command |offline |description
|------- |------- |----------- |------- |------- |-----------
|`hw help `|Y |`This help` |`hw help `|Y |`This help`
|`hw connect `|Y |`connect Proxmark3 to serial port` |`hw connect `|Y |`Connect Proxmark3 to serial port`
|`hw dbg `|N |`Set Proxmark3 debug level` |`hw dbg `|N |`Set Proxmark3 debug level`
|`hw detectreader `|N |`Detect external reader field` |`hw detectreader `|N |`Detect external reader field`
|`hw fpgaoff `|N |`Set FPGA off` |`hw fpgaoff `|N |`Set FPGA off`

View file

@ -33,8 +33,7 @@ typedef cl_uint cl_dx9_media_adapter_set_khr;
#if defined(_WIN32) #if defined(_WIN32)
#include <d3d9.h> #include <d3d9.h>
typedef struct _cl_dx9_surface_info_khr typedef struct _cl_dx9_surface_info_khr {
{
IDirect3DSurface9 *resource; IDirect3DSurface9 *resource;
HANDLE shared_handle; HANDLE shared_handle;
} cl_dx9_surface_info_khr; } cl_dx9_surface_info_khr;

View file

@ -391,8 +391,7 @@ clGetDeviceImageInfoQCOM(cl_device_id device,
void *param_value, void *param_value,
size_t *param_value_size_ret); size_t *param_value_size_ret);
typedef struct _cl_mem_ext_host_ptr typedef struct _cl_mem_ext_host_ptr {
{
/* Type of external memory allocation. */ /* Type of external memory allocation. */
/* Legal values will be defined in layered extensions. */ /* Legal values will be defined in layered extensions. */
cl_uint allocation_type; cl_uint allocation_type;
@ -417,8 +416,7 @@ typedef struct _cl_mem_ext_host_ptr
#define CL_MEM_ION_HOST_PTR_QCOM 0x40A8 #define CL_MEM_ION_HOST_PTR_QCOM 0x40A8
typedef struct _cl_mem_ion_host_ptr typedef struct _cl_mem_ion_host_ptr {
{
/* Type of external memory allocation. */ /* Type of external memory allocation. */
/* Must be CL_MEM_ION_HOST_PTR_QCOM for ION allocations. */ /* Must be CL_MEM_ION_HOST_PTR_QCOM for ION allocations. */
cl_mem_ext_host_ptr ext_host_ptr; cl_mem_ext_host_ptr ext_host_ptr;
@ -438,8 +436,7 @@ typedef struct _cl_mem_ion_host_ptr
#define CL_MEM_ANDROID_NATIVE_BUFFER_HOST_PTR_QCOM 0x40C6 #define CL_MEM_ANDROID_NATIVE_BUFFER_HOST_PTR_QCOM 0x40C6
typedef struct _cl_mem_android_native_buffer_host_ptr typedef struct _cl_mem_android_native_buffer_host_ptr {
{
/* Type of external memory allocation. */ /* Type of external memory allocation. */
/* Must be CL_MEM_ANDROID_NATIVE_BUFFER_HOST_PTR_QCOM for Android native buffers. */ /* Must be CL_MEM_ANDROID_NATIVE_BUFFER_HOST_PTR_QCOM for Android native buffers. */
cl_mem_ext_host_ptr ext_host_ptr; cl_mem_ext_host_ptr ext_host_ptr;
@ -661,8 +658,7 @@ typedef cl_uint cl_version_khr;
#define CL_NAME_VERSION_MAX_NAME_SIZE_KHR 64 #define CL_NAME_VERSION_MAX_NAME_SIZE_KHR 64
typedef struct _cl_name_version_khr typedef struct _cl_name_version_khr {
{
cl_version_khr version; cl_version_khr version;
char name[CL_NAME_VERSION_MAX_NAME_SIZE_KHR]; char name[CL_NAME_VERSION_MAX_NAME_SIZE_KHR];
} cl_name_version_khr; } cl_name_version_khr;

View file

@ -41,8 +41,7 @@ extern "C" {
/** /**
* Rounding mode used when converting to cl_half. * Rounding mode used when converting to cl_half.
*/ */
typedef enum typedef enum {
{
CL_HALF_RTE, // round to nearest even CL_HALF_RTE, // round to nearest even
CL_HALF_RTZ, // round towards zero CL_HALF_RTZ, // round towards zero
CL_HALF_RTP, // round towards positive infinity CL_HALF_RTP, // round towards positive infinity
@ -59,20 +58,14 @@ typedef enum
* Utility to deal with values that overflow when converting to half precision. * Utility to deal with values that overflow when converting to half precision.
*/ */
static inline cl_half cl_half_handle_overflow(cl_half_rounding_mode rounding_mode, static inline cl_half cl_half_handle_overflow(cl_half_rounding_mode rounding_mode,
uint16_t sign) uint16_t sign) {
{ if (rounding_mode == CL_HALF_RTZ) {
if (rounding_mode == CL_HALF_RTZ)
{
// Round overflow towards zero -> largest finite number (preserving sign) // Round overflow towards zero -> largest finite number (preserving sign)
return (sign << 15) | CL_HALF_MAX_FINITE_MAG; return (sign << 15) | CL_HALF_MAX_FINITE_MAG;
} } else if (rounding_mode == CL_HALF_RTP && sign) {
else if (rounding_mode == CL_HALF_RTP && sign)
{
// Round negative overflow towards positive infinity -> most negative finite number // Round negative overflow towards positive infinity -> most negative finite number
return (1 << 15) | CL_HALF_MAX_FINITE_MAG; return (1 << 15) | CL_HALF_MAX_FINITE_MAG;
} } else if (rounding_mode == CL_HALF_RTN && !sign) {
else if (rounding_mode == CL_HALF_RTN && !sign)
{
// Round positive overflow towards negative infinity -> largest finite number // Round positive overflow towards negative infinity -> largest finite number
return CL_HALF_MAX_FINITE_MAG; return CL_HALF_MAX_FINITE_MAG;
} }
@ -85,15 +78,11 @@ static inline cl_half cl_half_handle_overflow(cl_half_rounding_mode rounding_mod
* Utility to deal with values that underflow when converting to half precision. * Utility to deal with values that underflow when converting to half precision.
*/ */
static inline cl_half cl_half_handle_underflow(cl_half_rounding_mode rounding_mode, static inline cl_half cl_half_handle_underflow(cl_half_rounding_mode rounding_mode,
uint16_t sign) uint16_t sign) {
{ if (rounding_mode == CL_HALF_RTP && !sign) {
if (rounding_mode == CL_HALF_RTP && !sign)
{
// Round underflow towards positive infinity -> smallest positive value // Round underflow towards positive infinity -> smallest positive value
return (sign << 15) | 1; return (sign << 15) | 1;
} } else if (rounding_mode == CL_HALF_RTN && sign) {
else if (rounding_mode == CL_HALF_RTN && sign)
{
// Round underflow towards negative infinity -> largest negative value // Round underflow towards negative infinity -> largest negative value
return (sign << 15) | 1; return (sign << 15) | 1;
} }
@ -106,11 +95,9 @@ static inline cl_half cl_half_handle_underflow(cl_half_rounding_mode rounding_mo
/** /**
* Convert a cl_float to a cl_half. * Convert a cl_float to a cl_half.
*/ */
static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode rounding_mode) static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode rounding_mode) {
{
// Type-punning to get direct access to underlying bits // Type-punning to get direct access to underlying bits
union union {
{
cl_float f; cl_float f;
uint32_t i; uint32_t i;
} f32; } f32;
@ -133,43 +120,35 @@ static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode round
uint32_t lsb_pos = CL_FLT_MANT_DIG - CL_HALF_MANT_DIG; uint32_t lsb_pos = CL_FLT_MANT_DIG - CL_HALF_MANT_DIG;
// Check for NaN / infinity // Check for NaN / infinity
if (f_exp == 0xFF) if (f_exp == 0xFF) {
{ if (f_mant) {
if (f_mant)
{
// NaN -> propagate mantissa and silence it // NaN -> propagate mantissa and silence it
uint16_t h_mant = (uint16_t)(f_mant >> lsb_pos); uint16_t h_mant = (uint16_t)(f_mant >> lsb_pos);
h_mant |= 0x200; h_mant |= 0x200;
return (sign << 15) | CL_HALF_EXP_MASK | h_mant; return (sign << 15) | CL_HALF_EXP_MASK | h_mant;
} } else {
else
{
// Infinity -> zero mantissa // Infinity -> zero mantissa
return (sign << 15) | CL_HALF_EXP_MASK; return (sign << 15) | CL_HALF_EXP_MASK;
} }
} }
// Check for zero // Check for zero
if (!f_exp && !f_mant) if (!f_exp && !f_mant) {
{
return (sign << 15); return (sign << 15);
} }
// Check for overflow // Check for overflow
if (exp >= CL_HALF_MAX_EXP) if (exp >= CL_HALF_MAX_EXP) {
{
return cl_half_handle_overflow(rounding_mode, sign); return cl_half_handle_overflow(rounding_mode, sign);
} }
// Check for underflow // Check for underflow
if (exp < (CL_HALF_MIN_EXP - CL_HALF_MANT_DIG - 1)) if (exp < (CL_HALF_MIN_EXP - CL_HALF_MANT_DIG - 1)) {
{
return cl_half_handle_underflow(rounding_mode, sign); return cl_half_handle_underflow(rounding_mode, sign);
} }
// Check for value that will become denormal // Check for value that will become denormal
if (exp < -14) if (exp < -14) {
{
// Denormal -> include the implicit 1 from the FP32 mantissa // Denormal -> include the implicit 1 from the FP32 mantissa
h_exp = 0; h_exp = 0;
f_mant |= 1 << (CL_FLT_MANT_DIG - 1); f_mant |= 1 << (CL_FLT_MANT_DIG - 1);
@ -184,16 +163,12 @@ static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode round
// Check whether we need to round // Check whether we need to round
uint32_t halfway = 1 << (lsb_pos - 1); uint32_t halfway = 1 << (lsb_pos - 1);
uint32_t mask = (halfway << 1) - 1; uint32_t mask = (halfway << 1) - 1;
switch (rounding_mode) switch (rounding_mode) {
{
case CL_HALF_RTE: case CL_HALF_RTE:
if ((f_mant & mask) > halfway) if ((f_mant & mask) > halfway) {
{
// More than halfway -> round up // More than halfway -> round up
h_mant += 1; h_mant += 1;
} } else if ((f_mant & mask) == halfway) {
else if ((f_mant & mask) == halfway)
{
// Exactly halfway -> round to nearest even // Exactly halfway -> round to nearest even
if (h_mant & 0x1) if (h_mant & 0x1)
h_mant += 1; h_mant += 1;
@ -203,15 +178,13 @@ static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode round
// Mantissa has already been truncated -> do nothing // Mantissa has already been truncated -> do nothing
break; break;
case CL_HALF_RTP: case CL_HALF_RTP:
if ((f_mant & mask) && !sign) if ((f_mant & mask) && !sign) {
{
// Round positive numbers up // Round positive numbers up
h_mant += 1; h_mant += 1;
} }
break; break;
case CL_HALF_RTN: case CL_HALF_RTN:
if ((f_mant & mask) && sign) if ((f_mant & mask) && sign) {
{
// Round negative numbers down // Round negative numbers down
h_mant += 1; h_mant += 1;
} }
@ -219,8 +192,7 @@ static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode round
} }
// Check for mantissa overflow // Check for mantissa overflow
if (h_mant & 0x400) if (h_mant & 0x400) {
{
h_exp += 1; h_exp += 1;
h_mant = 0; h_mant = 0;
} }
@ -232,11 +204,9 @@ static inline cl_half cl_half_from_float(cl_float f, cl_half_rounding_mode round
/** /**
* Convert a cl_double to a cl_half. * Convert a cl_double to a cl_half.
*/ */
static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rounding_mode) static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rounding_mode) {
{
// Type-punning to get direct access to underlying bits // Type-punning to get direct access to underlying bits
union union {
{
cl_double d; cl_double d;
uint64_t i; uint64_t i;
} f64; } f64;
@ -259,43 +229,35 @@ static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rou
uint32_t lsb_pos = CL_DBL_MANT_DIG - CL_HALF_MANT_DIG; uint32_t lsb_pos = CL_DBL_MANT_DIG - CL_HALF_MANT_DIG;
// Check for NaN / infinity // Check for NaN / infinity
if (d_exp == 0x7FF) if (d_exp == 0x7FF) {
{ if (d_mant) {
if (d_mant)
{
// NaN -> propagate mantissa and silence it // NaN -> propagate mantissa and silence it
uint16_t h_mant = (uint16_t)(d_mant >> lsb_pos); uint16_t h_mant = (uint16_t)(d_mant >> lsb_pos);
h_mant |= 0x200; h_mant |= 0x200;
return (sign << 15) | CL_HALF_EXP_MASK | h_mant; return (sign << 15) | CL_HALF_EXP_MASK | h_mant;
} } else {
else
{
// Infinity -> zero mantissa // Infinity -> zero mantissa
return (sign << 15) | CL_HALF_EXP_MASK; return (sign << 15) | CL_HALF_EXP_MASK;
} }
} }
// Check for zero // Check for zero
if (!d_exp && !d_mant) if (!d_exp && !d_mant) {
{
return (sign << 15); return (sign << 15);
} }
// Check for overflow // Check for overflow
if (exp >= CL_HALF_MAX_EXP) if (exp >= CL_HALF_MAX_EXP) {
{
return cl_half_handle_overflow(rounding_mode, sign); return cl_half_handle_overflow(rounding_mode, sign);
} }
// Check for underflow // Check for underflow
if (exp < (CL_HALF_MIN_EXP - CL_HALF_MANT_DIG - 1)) if (exp < (CL_HALF_MIN_EXP - CL_HALF_MANT_DIG - 1)) {
{
return cl_half_handle_underflow(rounding_mode, sign); return cl_half_handle_underflow(rounding_mode, sign);
} }
// Check for value that will become denormal // Check for value that will become denormal
if (exp < -14) if (exp < -14) {
{
// Include the implicit 1 from the FP64 mantissa // Include the implicit 1 from the FP64 mantissa
h_exp = 0; h_exp = 0;
d_mant |= (uint64_t)1 << (CL_DBL_MANT_DIG - 1); d_mant |= (uint64_t)1 << (CL_DBL_MANT_DIG - 1);
@ -310,16 +272,12 @@ static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rou
// Check whether we need to round // Check whether we need to round
uint64_t halfway = (uint64_t)1 << (lsb_pos - 1); uint64_t halfway = (uint64_t)1 << (lsb_pos - 1);
uint64_t mask = (halfway << 1) - 1; uint64_t mask = (halfway << 1) - 1;
switch (rounding_mode) switch (rounding_mode) {
{
case CL_HALF_RTE: case CL_HALF_RTE:
if ((d_mant & mask) > halfway) if ((d_mant & mask) > halfway) {
{
// More than halfway -> round up // More than halfway -> round up
h_mant += 1; h_mant += 1;
} } else if ((d_mant & mask) == halfway) {
else if ((d_mant & mask) == halfway)
{
// Exactly halfway -> round to nearest even // Exactly halfway -> round to nearest even
if (h_mant & 0x1) if (h_mant & 0x1)
h_mant += 1; h_mant += 1;
@ -329,15 +287,13 @@ static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rou
// Mantissa has already been truncated -> do nothing // Mantissa has already been truncated -> do nothing
break; break;
case CL_HALF_RTP: case CL_HALF_RTP:
if ((d_mant & mask) && !sign) if ((d_mant & mask) && !sign) {
{
// Round positive numbers up // Round positive numbers up
h_mant += 1; h_mant += 1;
} }
break; break;
case CL_HALF_RTN: case CL_HALF_RTN:
if ((d_mant & mask) && sign) if ((d_mant & mask) && sign) {
{
// Round negative numbers down // Round negative numbers down
h_mant += 1; h_mant += 1;
} }
@ -345,8 +301,7 @@ static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rou
} }
// Check for mantissa overflow // Check for mantissa overflow
if (h_mant & 0x400) if (h_mant & 0x400) {
{
h_exp += 1; h_exp += 1;
h_mant = 0; h_mant = 0;
} }
@ -358,11 +313,9 @@ static inline cl_half cl_half_from_double(cl_double d, cl_half_rounding_mode rou
/** /**
* Convert a cl_half to a cl_float. * Convert a cl_half to a cl_float.
*/ */
static inline cl_float cl_half_to_float(cl_half h) static inline cl_float cl_half_to_float(cl_half h) {
{
// Type-punning to get direct access to underlying bits // Type-punning to get direct access to underlying bits
union union {
{
cl_float f; cl_float f;
uint32_t i; uint32_t i;
} f32; } f32;
@ -381,18 +334,14 @@ static inline cl_float cl_half_to_float(cl_half h)
uint32_t f_exp = exp + CL_FLT_MAX_EXP - 1; uint32_t f_exp = exp + CL_FLT_MAX_EXP - 1;
// Check for NaN / infinity // Check for NaN / infinity
if (h_exp == 0x1F) if (h_exp == 0x1F) {
{ if (h_mant) {
if (h_mant)
{
// NaN -> propagate mantissa and silence it // NaN -> propagate mantissa and silence it
uint32_t f_mant = h_mant << (CL_FLT_MANT_DIG - CL_HALF_MANT_DIG); uint32_t f_mant = h_mant << (CL_FLT_MANT_DIG - CL_HALF_MANT_DIG);
f_mant |= 0x400000; f_mant |= 0x400000;
f32.i = (sign << 31) | 0x7F800000 | f_mant; f32.i = (sign << 31) | 0x7F800000 | f_mant;
return f32.f; return f32.f;
} } else {
else
{
// Infinity -> zero mantissa // Infinity -> zero mantissa
f32.i = (sign << 31) | 0x7F800000; f32.i = (sign << 31) | 0x7F800000;
return f32.f; return f32.f;
@ -400,21 +349,16 @@ static inline cl_float cl_half_to_float(cl_half h)
} }
// Check for zero / denormal // Check for zero / denormal
if (h_exp == 0) if (h_exp == 0) {
{ if (h_mant == 0) {
if (h_mant == 0)
{
// Zero -> zero exponent // Zero -> zero exponent
f_exp = 0; f_exp = 0;
} } else {
else
{
// Denormal -> normalize it // Denormal -> normalize it
// - Shift mantissa to make most-significant 1 implicit // - Shift mantissa to make most-significant 1 implicit
// - Adjust exponent accordingly // - Adjust exponent accordingly
uint32_t shift = 0; uint32_t shift = 0;
while ((h_mant & 0x400) == 0) while ((h_mant & 0x400) == 0) {
{
h_mant <<= 1; h_mant <<= 1;
shift++; shift++;
} }

View file

@ -525,8 +525,7 @@ typedef unsigned int cl_GLenum;
/* Define cl_vector types */ /* Define cl_vector types */
/* ---- cl_charn ---- */ /* ---- cl_charn ---- */
typedef union typedef union {
{
cl_char CL_ALIGNED(2) s[2]; cl_char CL_ALIGNED(2) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_char x, y; }; __CL_ANON_STRUCT__ struct { cl_char x, y; };
@ -538,8 +537,7 @@ typedef union
#endif #endif
} cl_char2; } cl_char2;
typedef union typedef union {
{
cl_char CL_ALIGNED(4) s[4]; cl_char CL_ALIGNED(4) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_char x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_char x, y, z, w; };
@ -557,8 +555,7 @@ typedef union
/* cl_char3 is identical in size, alignment and behavior to cl_char4. See section 6.1.5. */ /* cl_char3 is identical in size, alignment and behavior to cl_char4. See section 6.1.5. */
typedef cl_char4 cl_char3; typedef cl_char4 cl_char3;
typedef union typedef union {
{
cl_char CL_ALIGNED(8) s[8]; cl_char CL_ALIGNED(8) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_char x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_char x, y, z, w; };
@ -576,8 +573,7 @@ typedef union
#endif #endif
} cl_char8; } cl_char8;
typedef union typedef union {
{
cl_char CL_ALIGNED(16) s[16]; cl_char CL_ALIGNED(16) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_char x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_char x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -600,8 +596,7 @@ typedef union
/* ---- cl_ucharn ---- */ /* ---- cl_ucharn ---- */
typedef union typedef union {
{
cl_uchar CL_ALIGNED(2) s[2]; cl_uchar CL_ALIGNED(2) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uchar x, y; }; __CL_ANON_STRUCT__ struct { cl_uchar x, y; };
@ -613,8 +608,7 @@ typedef union
#endif #endif
} cl_uchar2; } cl_uchar2;
typedef union typedef union {
{
cl_uchar CL_ALIGNED(4) s[4]; cl_uchar CL_ALIGNED(4) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w; };
@ -632,8 +626,7 @@ typedef union
/* cl_uchar3 is identical in size, alignment and behavior to cl_uchar4. See section 6.1.5. */ /* cl_uchar3 is identical in size, alignment and behavior to cl_uchar4. See section 6.1.5. */
typedef cl_uchar4 cl_uchar3; typedef cl_uchar4 cl_uchar3;
typedef union typedef union {
{
cl_uchar CL_ALIGNED(8) s[8]; cl_uchar CL_ALIGNED(8) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w; };
@ -651,8 +644,7 @@ typedef union
#endif #endif
} cl_uchar8; } cl_uchar8;
typedef union typedef union {
{
cl_uchar CL_ALIGNED(16) s[16]; cl_uchar CL_ALIGNED(16) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_uchar x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -675,8 +667,7 @@ typedef union
/* ---- cl_shortn ---- */ /* ---- cl_shortn ---- */
typedef union typedef union {
{
cl_short CL_ALIGNED(4) s[2]; cl_short CL_ALIGNED(4) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_short x, y; }; __CL_ANON_STRUCT__ struct { cl_short x, y; };
@ -688,8 +679,7 @@ typedef union
#endif #endif
} cl_short2; } cl_short2;
typedef union typedef union {
{
cl_short CL_ALIGNED(8) s[4]; cl_short CL_ALIGNED(8) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_short x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_short x, y, z, w; };
@ -707,8 +697,7 @@ typedef union
/* cl_short3 is identical in size, alignment and behavior to cl_short4. See section 6.1.5. */ /* cl_short3 is identical in size, alignment and behavior to cl_short4. See section 6.1.5. */
typedef cl_short4 cl_short3; typedef cl_short4 cl_short3;
typedef union typedef union {
{
cl_short CL_ALIGNED(16) s[8]; cl_short CL_ALIGNED(16) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_short x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_short x, y, z, w; };
@ -726,8 +715,7 @@ typedef union
#endif #endif
} cl_short8; } cl_short8;
typedef union typedef union {
{
cl_short CL_ALIGNED(32) s[16]; cl_short CL_ALIGNED(32) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_short x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_short x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -750,8 +738,7 @@ typedef union
/* ---- cl_ushortn ---- */ /* ---- cl_ushortn ---- */
typedef union typedef union {
{
cl_ushort CL_ALIGNED(4) s[2]; cl_ushort CL_ALIGNED(4) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ushort x, y; }; __CL_ANON_STRUCT__ struct { cl_ushort x, y; };
@ -763,8 +750,7 @@ typedef union
#endif #endif
} cl_ushort2; } cl_ushort2;
typedef union typedef union {
{
cl_ushort CL_ALIGNED(8) s[4]; cl_ushort CL_ALIGNED(8) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w; };
@ -782,8 +768,7 @@ typedef union
/* cl_ushort3 is identical in size, alignment and behavior to cl_ushort4. See section 6.1.5. */ /* cl_ushort3 is identical in size, alignment and behavior to cl_ushort4. See section 6.1.5. */
typedef cl_ushort4 cl_ushort3; typedef cl_ushort4 cl_ushort3;
typedef union typedef union {
{
cl_ushort CL_ALIGNED(16) s[8]; cl_ushort CL_ALIGNED(16) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w; };
@ -801,8 +786,7 @@ typedef union
#endif #endif
} cl_ushort8; } cl_ushort8;
typedef union typedef union {
{
cl_ushort CL_ALIGNED(32) s[16]; cl_ushort CL_ALIGNED(32) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_ushort x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -825,8 +809,7 @@ typedef union
/* ---- cl_halfn ---- */ /* ---- cl_halfn ---- */
typedef union typedef union {
{
cl_half CL_ALIGNED(4) s[2]; cl_half CL_ALIGNED(4) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_half x, y; }; __CL_ANON_STRUCT__ struct { cl_half x, y; };
@ -838,8 +821,7 @@ typedef union
#endif #endif
} cl_half2; } cl_half2;
typedef union typedef union {
{
cl_half CL_ALIGNED(8) s[4]; cl_half CL_ALIGNED(8) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_half x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_half x, y, z, w; };
@ -857,8 +839,7 @@ typedef union
/* cl_half3 is identical in size, alignment and behavior to cl_half4. See section 6.1.5. */ /* cl_half3 is identical in size, alignment and behavior to cl_half4. See section 6.1.5. */
typedef cl_half4 cl_half3; typedef cl_half4 cl_half3;
typedef union typedef union {
{
cl_half CL_ALIGNED(16) s[8]; cl_half CL_ALIGNED(16) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_half x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_half x, y, z, w; };
@ -876,8 +857,7 @@ typedef union
#endif #endif
} cl_half8; } cl_half8;
typedef union typedef union {
{
cl_half CL_ALIGNED(32) s[16]; cl_half CL_ALIGNED(32) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_half x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_half x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -899,8 +879,7 @@ typedef union
} cl_half16; } cl_half16;
/* ---- cl_intn ---- */ /* ---- cl_intn ---- */
typedef union typedef union {
{
cl_int CL_ALIGNED(8) s[2]; cl_int CL_ALIGNED(8) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_int x, y; }; __CL_ANON_STRUCT__ struct { cl_int x, y; };
@ -912,8 +891,7 @@ typedef union
#endif #endif
} cl_int2; } cl_int2;
typedef union typedef union {
{
cl_int CL_ALIGNED(16) s[4]; cl_int CL_ALIGNED(16) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_int x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_int x, y, z, w; };
@ -931,8 +909,7 @@ typedef union
/* cl_int3 is identical in size, alignment and behavior to cl_int4. See section 6.1.5. */ /* cl_int3 is identical in size, alignment and behavior to cl_int4. See section 6.1.5. */
typedef cl_int4 cl_int3; typedef cl_int4 cl_int3;
typedef union typedef union {
{
cl_int CL_ALIGNED(32) s[8]; cl_int CL_ALIGNED(32) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_int x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_int x, y, z, w; };
@ -950,8 +927,7 @@ typedef union
#endif #endif
} cl_int8; } cl_int8;
typedef union typedef union {
{
cl_int CL_ALIGNED(64) s[16]; cl_int CL_ALIGNED(64) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_int x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_int x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -974,8 +950,7 @@ typedef union
/* ---- cl_uintn ---- */ /* ---- cl_uintn ---- */
typedef union typedef union {
{
cl_uint CL_ALIGNED(8) s[2]; cl_uint CL_ALIGNED(8) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uint x, y; }; __CL_ANON_STRUCT__ struct { cl_uint x, y; };
@ -987,8 +962,7 @@ typedef union
#endif #endif
} cl_uint2; } cl_uint2;
typedef union typedef union {
{
cl_uint CL_ALIGNED(16) s[4]; cl_uint CL_ALIGNED(16) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uint x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_uint x, y, z, w; };
@ -1006,8 +980,7 @@ typedef union
/* cl_uint3 is identical in size, alignment and behavior to cl_uint4. See section 6.1.5. */ /* cl_uint3 is identical in size, alignment and behavior to cl_uint4. See section 6.1.5. */
typedef cl_uint4 cl_uint3; typedef cl_uint4 cl_uint3;
typedef union typedef union {
{
cl_uint CL_ALIGNED(32) s[8]; cl_uint CL_ALIGNED(32) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uint x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_uint x, y, z, w; };
@ -1025,8 +998,7 @@ typedef union
#endif #endif
} cl_uint8; } cl_uint8;
typedef union typedef union {
{
cl_uint CL_ALIGNED(64) s[16]; cl_uint CL_ALIGNED(64) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_uint x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_uint x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -1048,8 +1020,7 @@ typedef union
} cl_uint16; } cl_uint16;
/* ---- cl_longn ---- */ /* ---- cl_longn ---- */
typedef union typedef union {
{
cl_long CL_ALIGNED(16) s[2]; cl_long CL_ALIGNED(16) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_long x, y; }; __CL_ANON_STRUCT__ struct { cl_long x, y; };
@ -1061,8 +1032,7 @@ typedef union
#endif #endif
} cl_long2; } cl_long2;
typedef union typedef union {
{
cl_long CL_ALIGNED(32) s[4]; cl_long CL_ALIGNED(32) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_long x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_long x, y, z, w; };
@ -1080,8 +1050,7 @@ typedef union
/* cl_long3 is identical in size, alignment and behavior to cl_long4. See section 6.1.5. */ /* cl_long3 is identical in size, alignment and behavior to cl_long4. See section 6.1.5. */
typedef cl_long4 cl_long3; typedef cl_long4 cl_long3;
typedef union typedef union {
{
cl_long CL_ALIGNED(64) s[8]; cl_long CL_ALIGNED(64) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_long x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_long x, y, z, w; };
@ -1099,8 +1068,7 @@ typedef union
#endif #endif
} cl_long8; } cl_long8;
typedef union typedef union {
{
cl_long CL_ALIGNED(128) s[16]; cl_long CL_ALIGNED(128) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_long x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_long x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -1123,8 +1091,7 @@ typedef union
/* ---- cl_ulongn ---- */ /* ---- cl_ulongn ---- */
typedef union typedef union {
{
cl_ulong CL_ALIGNED(16) s[2]; cl_ulong CL_ALIGNED(16) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ulong x, y; }; __CL_ANON_STRUCT__ struct { cl_ulong x, y; };
@ -1136,8 +1103,7 @@ typedef union
#endif #endif
} cl_ulong2; } cl_ulong2;
typedef union typedef union {
{
cl_ulong CL_ALIGNED(32) s[4]; cl_ulong CL_ALIGNED(32) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w; };
@ -1155,8 +1121,7 @@ typedef union
/* cl_ulong3 is identical in size, alignment and behavior to cl_ulong4. See section 6.1.5. */ /* cl_ulong3 is identical in size, alignment and behavior to cl_ulong4. See section 6.1.5. */
typedef cl_ulong4 cl_ulong3; typedef cl_ulong4 cl_ulong3;
typedef union typedef union {
{
cl_ulong CL_ALIGNED(64) s[8]; cl_ulong CL_ALIGNED(64) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w; };
@ -1174,8 +1139,7 @@ typedef union
#endif #endif
} cl_ulong8; } cl_ulong8;
typedef union typedef union {
{
cl_ulong CL_ALIGNED(128) s[16]; cl_ulong CL_ALIGNED(128) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_ulong x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -1199,8 +1163,7 @@ typedef union
/* --- cl_floatn ---- */ /* --- cl_floatn ---- */
typedef union typedef union {
{
cl_float CL_ALIGNED(8) s[2]; cl_float CL_ALIGNED(8) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_float x, y; }; __CL_ANON_STRUCT__ struct { cl_float x, y; };
@ -1212,8 +1175,7 @@ typedef union
#endif #endif
} cl_float2; } cl_float2;
typedef union typedef union {
{
cl_float CL_ALIGNED(16) s[4]; cl_float CL_ALIGNED(16) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_float x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_float x, y, z, w; };
@ -1231,8 +1193,7 @@ typedef union
/* cl_float3 is identical in size, alignment and behavior to cl_float4. See section 6.1.5. */ /* cl_float3 is identical in size, alignment and behavior to cl_float4. See section 6.1.5. */
typedef cl_float4 cl_float3; typedef cl_float4 cl_float3;
typedef union typedef union {
{
cl_float CL_ALIGNED(32) s[8]; cl_float CL_ALIGNED(32) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_float x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_float x, y, z, w; };
@ -1250,8 +1211,7 @@ typedef union
#endif #endif
} cl_float8; } cl_float8;
typedef union typedef union {
{
cl_float CL_ALIGNED(64) s[16]; cl_float CL_ALIGNED(64) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_float x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_float x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };
@ -1274,8 +1234,7 @@ typedef union
/* --- cl_doublen ---- */ /* --- cl_doublen ---- */
typedef union typedef union {
{
cl_double CL_ALIGNED(16) s[2]; cl_double CL_ALIGNED(16) s[2];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_double x, y; }; __CL_ANON_STRUCT__ struct { cl_double x, y; };
@ -1287,8 +1246,7 @@ typedef union
#endif #endif
} cl_double2; } cl_double2;
typedef union typedef union {
{
cl_double CL_ALIGNED(32) s[4]; cl_double CL_ALIGNED(32) s[4];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_double x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_double x, y, z, w; };
@ -1306,8 +1264,7 @@ typedef union
/* cl_double3 is identical in size, alignment and behavior to cl_double4. See section 6.1.5. */ /* cl_double3 is identical in size, alignment and behavior to cl_double4. See section 6.1.5. */
typedef cl_double4 cl_double3; typedef cl_double4 cl_double3;
typedef union typedef union {
{
cl_double CL_ALIGNED(64) s[8]; cl_double CL_ALIGNED(64) s[8];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_double x, y, z, w; }; __CL_ANON_STRUCT__ struct { cl_double x, y, z, w; };
@ -1325,8 +1282,7 @@ typedef union
#endif #endif
} cl_double8; } cl_double8;
typedef union typedef union {
{
cl_double CL_ALIGNED(128) s[16]; cl_double CL_ALIGNED(128) s[16];
#if __CL_HAS_ANON_STRUCT__ #if __CL_HAS_ANON_STRUCT__
__CL_ANON_STRUCT__ struct { cl_double x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; }; __CL_ANON_STRUCT__ struct { cl_double x, y, z, w, __spacer4, __spacer5, __spacer6, __spacer7, __spacer8, __spacer9, sa, sb, sc, sd, se, sf; };

View file

@ -4,16 +4,14 @@
//#if FORCE_HITAG2_FULL == 0 //#if FORCE_HITAG2_FULL == 0
// return a single bit from a value // return a single bit from a value
int bitn (uint64_t x, int bit) int bitn(uint64_t x, int bit) {
{
const uint64_t bitmask = (uint64_t)(1) << bit; const uint64_t bitmask = (uint64_t)(1) << bit;
return (x & bitmask) ? 1 : 0; return (x & bitmask) ? 1 : 0;
} }
// the sub-function R that rollback depends upon // the sub-function R that rollback depends upon
int fnR (uint64_t x) int fnR(uint64_t x) {
{
// renumbered bits because my state is 0-47, not 1-48 // renumbered bits because my state is 0-47, not 1-48
return (bitn(x, 1) ^ bitn(x, 2) ^ bitn(x, 5) ^ return (bitn(x, 1) ^ bitn(x, 2) ^ bitn(x, 5) ^
bitn(x, 6) ^ bitn(x, 7) ^ bitn(x, 15) ^ bitn(x, 6) ^ bitn(x, 7) ^ bitn(x, 15) ^
@ -32,8 +30,7 @@ int fb(unsigned int i) {
} }
// the filter function that generates a bit of output from the prng state // the filter function that generates a bit of output from the prng state
int fnf (uint64_t s) int fnf(uint64_t s) {
{
const unsigned int x1 = (unsigned int)((bitn(s, 2) << 0) | (bitn(s, 3) << 1) | (bitn(s, 5) << 2) | (bitn(s, 6) << 3)); const unsigned int x1 = (unsigned int)((bitn(s, 2) << 0) | (bitn(s, 3) << 1) | (bitn(s, 5) << 2) | (bitn(s, 6) << 3));
const unsigned int x2 = (unsigned int)((bitn(s, 8) << 0) | (bitn(s, 12) << 1) | (bitn(s, 14) << 2) | (bitn(s, 15) << 3)); const unsigned int x2 = (unsigned int)((bitn(s, 8) << 0) | (bitn(s, 12) << 1) | (bitn(s, 14) << 2) | (bitn(s, 15) << 3));
const unsigned int x3 = (unsigned int)((bitn(s, 17) << 0) | (bitn(s, 21) << 1) | (bitn(s, 23) << 2) | (bitn(s, 26) << 3)); const unsigned int x3 = (unsigned int)((bitn(s, 17) << 0) | (bitn(s, 21) << 1) | (bitn(s, 23) << 2) | (bitn(s, 26) << 3));
@ -119,8 +116,7 @@ uint32_t hitag2_nstep(Hitag_State *pstate, uint32_t steps) {
* uint32_t serialnum - 32 bit tag serial number * uint32_t serialnum - 32 bit tag serial number
* uint32_t initvector - 32 bit random IV from reader, part of tag authentication * uint32_t initvector - 32 bit random IV from reader, part of tag authentication
*/ */
void hitag2_init (Hitag_State *pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector) void hitag2_init(Hitag_State *pstate, uint64_t sharedkey, uint32_t serialnum, uint32_t initvector) {
{
// init state, from serial number and lowest 16 bits of shared key // init state, from serial number and lowest 16 bits of shared key
uint64_t cur_state = ((sharedkey & 0xFFFF) << 32) | serialnum; uint64_t cur_state = ((sharedkey & 0xFFFF) << 32) | serialnum;
@ -182,8 +178,7 @@ void hitag2_init (Hitag_State *pstate, uint64_t sharedkey, uint32_t serialnum, u
// try state // try state
// todo, changes arguments, only what is needed // todo, changes arguments, only what is needed
bool try_state (uint64_t s, uint32_t uid, uint32_t aR2, uint32_t nR1, uint32_t nR2, uint64_t *key) bool try_state(uint64_t s, uint32_t uid, uint32_t aR2, uint32_t nR1, uint32_t nR2, uint64_t *key) {
{
#if defined(DEBUG_HITAG2) && DEBUG_HITAG2 == 1 #if defined(DEBUG_HITAG2) && DEBUG_HITAG2 == 1
printf("s : %lu, uid: %u, aR2: %u, nR1: %u, nR2: %u\n", s, uid, aR2, nR1, nR2); printf("s : %lu, uid: %u, aR2: %u, nR1: %u, nR2: %u\n", s, uid, aR2, nR1, nR2);
fflush(stdout); fflush(stdout);
@ -213,8 +208,7 @@ bool try_state (uint64_t s, uint32_t uid, uint32_t aR2, uint32_t nR1, uint32_t n
fflush(stdout); fflush(stdout);
#endif #endif
for (int i = 0; i < 32; i++) for (int i = 0; i < 32; i++) {
{
hstate.shiftreg = ((hstate.shiftreg) << 1) | ((uid >> (31 - i)) & 0x1); hstate.shiftreg = ((hstate.shiftreg) << 1) | ((uid >> (31 - i)) & 0x1);
b = (b << 1) | (unsigned int) fnf(hstate.shiftreg); b = (b << 1) | (unsigned int) fnf(hstate.shiftreg);
} }
@ -233,8 +227,7 @@ bool try_state (uint64_t s, uint32_t uid, uint32_t aR2, uint32_t nR1, uint32_t n
// test key // test key
hitag2_init(&hstate, keyrev, uid, nR2); hitag2_init(&hstate, keyrev, uid, nR2);
if ((aR2 ^ hitag2_nstep (&hstate, 32)) == 0xffffffff) if ((aR2 ^ hitag2_nstep(&hstate, 32)) == 0xffffffff) {
{
*key = rev64(keyrev); *key = rev64(keyrev);
#if DEBUGME >= 2 #if DEBUGME >= 2

View file

@ -27,8 +27,7 @@
#define rev32(X) (rev16(X) + (rev16(X >> 16) << 16)) #define rev32(X) (rev16(X) + (rev16(X >> 16) << 16))
#define rev64(X) (rev32(X) + (rev32(X >> 32) << 32)) #define rev64(X) (rev32(X) + (rev32(X >> 32) << 32))
typedef struct typedef struct {
{
uint64_t shiftreg; // naive shift register, required for nonlinear fn input uint64_t shiftreg; // naive shift register, required for nonlinear fn input
uint64_t lfsr; // fast lfsr, used to make software faster uint64_t lfsr; // fast lfsr, used to make software faster
} Hitag_State; } Hitag_State;

File diff suppressed because it is too large Load diff

View file

@ -24,19 +24,15 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#include "opencl.h" #include "opencl.h"
bool plat_dev_enabled (unsigned int id, unsigned int *sel, unsigned int cnt, unsigned int cur_type, unsigned int allow_type) bool plat_dev_enabled(unsigned int id, unsigned int *sel, unsigned int cnt, unsigned int cur_type, unsigned int allow_type) {
{
// usefull only with devices // usefull only with devices
if (allow_type != CL_DEVICE_TYPE_ALL) if (allow_type != CL_DEVICE_TYPE_ALL) {
{
if (cur_type != allow_type) return false; if (cur_type != allow_type) return false;
} }
if (sel[0] == 0xff) return true; // all if (sel[0] == 0xff) return true; // all
else else {
{ for (unsigned int i = 0; i < cnt; i++) {
for (unsigned int i = 0; i < cnt; i++)
{
if (sel[i] == (id + 1)) return true; if (sel[i] == (id + 1)) return true;
} }
} }
@ -44,15 +40,13 @@ bool plat_dev_enabled (unsigned int id, unsigned int *sel, unsigned int cnt, uns
return false; return false;
} }
int runKernel (opencl_ctx_t *ctx, uint32_t cand_base, uint64_t *matches, uint32_t *matches_found, size_t id) int runKernel(opencl_ctx_t *ctx, uint32_t cand_base, uint64_t *matches, uint32_t *matches_found, size_t id) {
{
int err = 0; int err = 0;
size_t global_ws[3] = { ctx->global_ws[id], GLOBAL_WS_1, GLOBAL_WS_2 }; size_t global_ws[3] = { ctx->global_ws[id], GLOBAL_WS_1, GLOBAL_WS_2 };
size_t local_ws[3] = { ctx->local_ws[id], 1, 1 }; size_t local_ws[3] = { ctx->local_ws[id], 1, 1 };
if (ctx->profiling) if (ctx->profiling) {
{
printf("[%zu] global_ws %zu, ctx->local_ws: %zu\n", id, global_ws[0], local_ws[0]); printf("[%zu] global_ws %zu, ctx->local_ws: %zu\n", id, global_ws[0], local_ws[0]);
fflush(stdout); fflush(stdout);
} }
@ -90,8 +84,7 @@ int runKernel (opencl_ctx_t *ctx, uint32_t cand_base, uint64_t *matches, uint32_
return -1; return -1;
} }
if (ctx->profiling) if (ctx->profiling) {
{
err = clWaitForEvents(1, &event); err = clWaitForEvents(1, &event);
if (err != CL_SUCCESS) { if (err != CL_SUCCESS) {
printf("[%zu] Error: clWaitForEvents() failed (%d)\n", id, err); printf("[%zu] Error: clWaitForEvents() failed (%d)\n", id, err);
@ -134,16 +127,11 @@ int runKernel (opencl_ctx_t *ctx, uint32_t cand_base, uint64_t *matches, uint32_
return -1; return -1;
} }
if (matches_found[0] > 0) if (matches_found[0] > 0) {
{ if (ctx->force_hitag2_opencl) {
if (ctx->force_hitag2_opencl)
{
if (matches_found[0] != 1) printf("[%zu] BUG: if match the counter must be 1. Here %u are founds\n", id, matches_found[0]); if (matches_found[0] != 1) printf("[%zu] BUG: if match the counter must be 1. Here %u are founds\n", id, matches_found[0]);
} } else {
else if (matches_found[0] > (uint32_t)(ctx->global_ws[id]*WGS_MATCHES_FACTOR)) {
{
if (matches_found[0] > (uint32_t)(ctx->global_ws[id]*WGS_MATCHES_FACTOR))
{
printf("[%zu] BUG: the next clEnqueueReadBuffer will crash. 'matches' buffer (%u) is lower than requested (%u)\n", id, (uint32_t)(ctx->global_ws[id]*WGS_MATCHES_FACTOR), matches_found[0]); printf("[%zu] BUG: the next clEnqueueReadBuffer will crash. 'matches' buffer (%u) is lower than requested (%u)\n", id, (uint32_t)(ctx->global_ws[id]*WGS_MATCHES_FACTOR), matches_found[0]);
} }
} }

View file

@ -43,8 +43,7 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#define MAX_OPENCL_DEVICES 16 #define MAX_OPENCL_DEVICES 16
// defines structures // defines structures
typedef struct compute_device_ctx typedef struct compute_device_ctx {
{
char name[0xff]; char name[0xff];
char vendor[0x40]; char vendor[0x40];
char version[0x40]; char version[0x40];
@ -67,8 +66,7 @@ typedef struct compute_device_ctx
} compute_device_ctx_t; } compute_device_ctx_t;
typedef struct compute_platform_ctx typedef struct compute_platform_ctx {
{
unsigned int device_cnt; unsigned int device_cnt;
unsigned int compute_units_max; unsigned int compute_units_max;
@ -90,8 +88,7 @@ typedef struct compute_platform_ctx
} compute_platform_ctx_t; } compute_platform_ctx_t;
typedef struct opencl_ctx typedef struct opencl_ctx {
{
char *kernelSource[1]; char *kernelSource[1];
size_t kernelSource_len; size_t kernelSource_len;

View file

@ -25,8 +25,7 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#include "queue.h" #include "queue.h"
#if TEST_UNIT == 1 #if TEST_UNIT == 1
int wu_queue_print (wu_queue_ctx_t *ctx) int wu_queue_print(wu_queue_ctx_t *ctx) {
{
wu_queue_item_t *ptr = 0; //NULL; wu_queue_item_t *ptr = 0; //NULL;
size_t sum = 0; size_t sum = 0;
int ret = -1; int ret = -1;
@ -36,14 +35,12 @@ int wu_queue_print (wu_queue_ctx_t *ctx)
pthread_mutex_lock(&ctx->queue_mutex); pthread_mutex_lock(&ctx->queue_mutex);
if ((ret = wu_queue_done (ctx)) != 0) if ((ret = wu_queue_done(ctx)) != 0) {
{
pthread_mutex_unlock(&ctx->queue_mutex); pthread_mutex_unlock(&ctx->queue_mutex);
return ret; return ret;
} }
switch (ctx->queue_type) switch (ctx->queue_type) {
{
case QUEUE_TYPE_FORWARD: case QUEUE_TYPE_FORWARD:
ptr = ctx->queue_head; ptr = ctx->queue_head;
printf("> show queue contents in FORWARD mode, from head\n"); printf("> show queue contents in FORWARD mode, from head\n");
@ -64,34 +61,28 @@ int wu_queue_print (wu_queue_ctx_t *ctx)
printf("# Queue size: %zu\n", ctx->queue_size); printf("# Queue size: %zu\n", ctx->queue_size);
do do {
{
sum += ptr->data.id; sum += ptr->data.id;
if (cnt++ < 4) printf("# ID %zu, OFF %zu, MAX %zu\n", ptr->data.id, ptr->data.off, ptr->data.max); if (cnt++ < 4) printf("# ID %zu, OFF %zu, MAX %zu\n", ptr->data.id, ptr->data.off, ptr->data.max);
if (ctx->queue_type == QUEUE_TYPE_FORWARD || ctx->queue_type == QUEUE_TYPE_RANDOM) if (ctx->queue_type == QUEUE_TYPE_FORWARD || ctx->queue_type == QUEUE_TYPE_RANDOM) {
{
if (!ptr->next) break; if (!ptr->next) break;
ptr = ptr->next; ptr = ptr->next;
} } else if (ctx->queue_type == QUEUE_TYPE_REVERSE) {
else if (ctx->queue_type == QUEUE_TYPE_REVERSE)
{
if (!ptr->prev) break; if (!ptr->prev) break;
ptr = ptr->prev; ptr = ptr->prev;
} }
} while (ptr); } while (ptr);
if (!ptr) if (!ptr) {
{
printf("! Fail: ptr must be not null here\n"); printf("! Fail: ptr must be not null here\n");
pthread_mutex_unlock(&ctx->queue_mutex); pthread_mutex_unlock(&ctx->queue_mutex);
return -1; return -1;
} }
switch (ctx->queue_type) switch (ctx->queue_type) {
{
case QUEUE_TYPE_RANDOM: case QUEUE_TYPE_RANDOM:
printf("> show queue contents in RANDOM mode, from end to head\n"); printf("> show queue contents in RANDOM mode, from end to head\n");
break; break;
@ -105,26 +96,21 @@ int wu_queue_print (wu_queue_ctx_t *ctx)
cnt = 0; cnt = 0;
do do {
{
sum -= ptr->data.id; sum -= ptr->data.id;
if (cnt++ < 4) printf("# ID %zu, OFF %zu, MAX %zu\n", ptr->data.id, ptr->data.off, ptr->data.max); if (cnt++ < 4) printf("# ID %zu, OFF %zu, MAX %zu\n", ptr->data.id, ptr->data.off, ptr->data.max);
if (ctx->queue_type == QUEUE_TYPE_FORWARD || ctx->queue_type == QUEUE_TYPE_RANDOM) if (ctx->queue_type == QUEUE_TYPE_FORWARD || ctx->queue_type == QUEUE_TYPE_RANDOM) {
{
if (!ptr->prev) break; if (!ptr->prev) break;
ptr = ptr->prev; ptr = ptr->prev;
} } else if (ctx->queue_type == QUEUE_TYPE_REVERSE) {
else if (ctx->queue_type == QUEUE_TYPE_REVERSE)
{
if (!ptr->next) break; if (!ptr->next) break;
ptr = ptr->next; ptr = ptr->next;
} }
} while (ptr); } while (ptr);
if (sum != 0) if (sum != 0) {
{
printf("! Fail: sum is not zero\n"); printf("! Fail: sum is not zero\n");
pthread_mutex_unlock(&ctx->queue_mutex); pthread_mutex_unlock(&ctx->queue_mutex);
return -1; return -1;
@ -136,41 +122,50 @@ int wu_queue_print (wu_queue_ctx_t *ctx)
#endif #endif
const char *wu_queue_strerror (int error) const char *wu_queue_strerror(int error) {
{ switch (error) {
switch (error) case QUEUE_EMPTY:
{ return (const char *) "QUERY_EMPTY";
case QUEUE_EMPTY: return (const char *) "QUERY_EMPTY"; case NO_ERROR:
case NO_ERROR: return (const char *) "NO_ERROR"; return (const char *) "NO_ERROR";
case ERROR_GENERIC: return (const char *) "ERROR_GENERIC"; case ERROR_GENERIC:
case ERROR_QUEUE_TYPE_INVALID: return (const char *) "ERROR_QUEUE_TYPE_INVALID"; return (const char *) "ERROR_GENERIC";
case ERROR_CTX_NULL: return (const char *) "ERROR_CTX_NULL"; case ERROR_QUEUE_TYPE_INVALID:
case ERROR_CTX_IS_INIT: return (const char *) "ERROR_CTX_IS_INIT"; return (const char *) "ERROR_QUEUE_TYPE_INVALID";
case ERROR_CTX_IS_NOT_INIT: return (const char *) "ERROR_CTX_IS_NOT_INIT"; case ERROR_CTX_NULL:
case ERROR_MUTEXATTR_INIT: return (const char *) "ERROR_MUTEXATTR_INIT"; return (const char *) "ERROR_CTX_NULL";
case ERROR_MUTEXATTR_SETTYPE: return (const char *) "ERROR_MUTEXATTR_SETTYPE"; case ERROR_CTX_IS_INIT:
case ERROR_MUTEX_INIT: return (const char *) "ERROR_MUTEX_INIT"; return (const char *) "ERROR_CTX_IS_INIT";
case ERROR_ALLOC: return (const char *) "ERROR_ALLOC"; case ERROR_CTX_IS_NOT_INIT:
return (const char *) "ERROR_CTX_IS_NOT_INIT";
case ERROR_MUTEXATTR_INIT:
return (const char *) "ERROR_MUTEXATTR_INIT";
case ERROR_MUTEXATTR_SETTYPE:
return (const char *) "ERROR_MUTEXATTR_SETTYPE";
case ERROR_MUTEX_INIT:
return (const char *) "ERROR_MUTEX_INIT";
case ERROR_ALLOC:
return (const char *) "ERROR_ALLOC";
case ERROR_UNDEFINED: case ERROR_UNDEFINED:
default: default:
return (const char *) "ERROR_UNDEFINED"; return (const char *) "ERROR_UNDEFINED";
} }
} }
const char *wu_queue_strdesc (wu_queue_type_t type) const char *wu_queue_strdesc(wu_queue_type_t type) {
{ switch (type) {
switch (type) case QUEUE_TYPE_FORWARD:
{ return (const char *) "FORWARD";
case QUEUE_TYPE_FORWARD: return (const char *) "FORWARD"; case QUEUE_TYPE_REVERSE:
case QUEUE_TYPE_REVERSE: return (const char *) "REVERSE"; return (const char *) "REVERSE";
case QUEUE_TYPE_RANDOM: return (const char *) "RANDOM"; case QUEUE_TYPE_RANDOM:
return (const char *) "RANDOM";
} }
return (const char *) "UNKNOWN"; return (const char *) "UNKNOWN";
} }
int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type) int wu_queue_init(wu_queue_ctx_t *ctx, wu_queue_type_t queue_type) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stdout, "[%s] enter\n", __func__); fprintf(stdout, "[%s] enter\n", __func__);
fflush(stdout); fflush(stdout);
@ -182,8 +177,7 @@ int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type)
if (ctx->init) return ERROR_CTX_IS_INIT; if (ctx->init) return ERROR_CTX_IS_INIT;
if (queue_type == QUEUE_TYPE_RANDOM) srand((unsigned int) time(0)); if (queue_type == QUEUE_TYPE_RANDOM) srand((unsigned int) time(0));
else if (queue_type != QUEUE_TYPE_FORWARD && queue_type != QUEUE_TYPE_REVERSE) else if (queue_type != QUEUE_TYPE_FORWARD && queue_type != QUEUE_TYPE_REVERSE) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "! Error, invalid 'queue_type'.\n"); fprintf(stderr, "! Error, invalid 'queue_type'.\n");
#endif #endif
@ -197,8 +191,7 @@ int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type)
int ret = 0; int ret = 0;
if ((ret = pthread_mutexattr_init (&ctx->queue_mutex_attr)) != 0) if ((ret = pthread_mutexattr_init(&ctx->queue_mutex_attr)) != 0) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "! Error, pthread_mutexattr_init() failed (%d): %s\n", ret, strerror(ret)); fprintf(stderr, "! Error, pthread_mutexattr_init() failed (%d): %s\n", ret, strerror(ret));
#endif #endif
@ -206,8 +199,7 @@ int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type)
return ERROR_MUTEXATTR_INIT; return ERROR_MUTEXATTR_INIT;
} }
if ((ret = pthread_mutexattr_settype (&ctx->queue_mutex_attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) if ((ret = pthread_mutexattr_settype(&ctx->queue_mutex_attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "! Error, pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK) failed (%d): %s\n", ret, strerror(ret)); fprintf(stderr, "! Error, pthread_mutexattr_settype(PTHREAD_MUTEX_ERRORCHECK) failed (%d): %s\n", ret, strerror(ret));
#endif #endif
@ -216,8 +208,7 @@ int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type)
return ERROR_MUTEXATTR_SETTYPE; return ERROR_MUTEXATTR_SETTYPE;
} }
if ((ret = pthread_mutex_init (&ctx->queue_mutex, &ctx->queue_mutex_attr)) != 0) if ((ret = pthread_mutex_init(&ctx->queue_mutex, &ctx->queue_mutex_attr)) != 0) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "! Error, pthread_mutex_init() failed (%d): %s\n", ret, strerror(ret)); fprintf(stderr, "! Error, pthread_mutex_init() failed (%d): %s\n", ret, strerror(ret));
#endif #endif
@ -230,23 +221,23 @@ int wu_queue_init (wu_queue_ctx_t *ctx, wu_queue_type_t queue_type)
return NO_ERROR; return NO_ERROR;
} }
int wu_queue_done (wu_queue_ctx_t *ctx) int wu_queue_done(wu_queue_ctx_t *ctx) {
{
if (!ctx) return ERROR_CTX_NULL; if (!ctx) return ERROR_CTX_NULL;
if (!ctx->init) return ERROR_CTX_IS_NOT_INIT; if (!ctx->init) return ERROR_CTX_IS_NOT_INIT;
switch (ctx->queue_type) switch (ctx->queue_type) {
{ case QUEUE_TYPE_RANDOM:
case QUEUE_TYPE_RANDOM: return (ctx->queue_head == NULL); return (ctx->queue_head == NULL);
case QUEUE_TYPE_FORWARD: return (ctx->queue_head == NULL); case QUEUE_TYPE_FORWARD:
case QUEUE_TYPE_REVERSE: return (ctx->queue_tail == NULL); return (ctx->queue_head == NULL);
case QUEUE_TYPE_REVERSE:
return (ctx->queue_tail == NULL);
} }
return ERROR_QUEUE_TYPE_INVALID; return ERROR_QUEUE_TYPE_INVALID;
} }
int wu_queue_push (wu_queue_ctx_t *ctx, size_t id, size_t off, size_t max) int wu_queue_push(wu_queue_ctx_t *ctx, size_t id, size_t off, size_t max) {
{
if (!ctx) return ERROR_CTX_NULL; if (!ctx) return ERROR_CTX_NULL;
if (!ctx->init) return ERROR_CTX_IS_NOT_INIT; if (!ctx->init) return ERROR_CTX_IS_NOT_INIT;
@ -258,8 +249,7 @@ int wu_queue_push (wu_queue_ctx_t *ctx, size_t id, size_t off, size_t max)
if (ctx->queue_head == 0) first = 1; if (ctx->queue_head == 0) first = 1;
if (!(ptr = (wu_queue_item_t *) malloc (sizeof (wu_queue_item_t)))) if (!(ptr = (wu_queue_item_t *) malloc(sizeof(wu_queue_item_t)))) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "! Error: malloc() failed (%d): %s\n", errno, strerror(errno)); fprintf(stderr, "! Error: malloc() failed (%d): %s\n", errno, strerror(errno));
#endif #endif
@ -295,8 +285,7 @@ int wu_queue_push (wu_queue_ctx_t *ctx, size_t id, size_t off, size_t max)
return NO_ERROR; return NO_ERROR;
} }
int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove) int wu_queue_pop(wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove) {
{
if (!ctx) return ERROR_CTX_NULL; if (!ctx) return ERROR_CTX_NULL;
if (!ctx->init) return ERROR_CTX_IS_NOT_INIT; if (!ctx->init) return ERROR_CTX_IS_NOT_INIT;
@ -306,8 +295,7 @@ int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove)
pthread_mutex_lock(&ctx->queue_mutex); pthread_mutex_lock(&ctx->queue_mutex);
if ((ret = wu_queue_done (ctx)) != 0) if ((ret = wu_queue_done(ctx)) != 0) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stderr, "ret from wu_queue_done() (%d): %s\n", ret, wu_queue_strerror(ret)); fprintf(stderr, "ret from wu_queue_done() (%d): %s\n", ret, wu_queue_strerror(ret));
#endif #endif
@ -315,8 +303,7 @@ int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove)
return ret; return ret;
} }
switch (ctx->queue_type) switch (ctx->queue_type) {
{
case QUEUE_TYPE_FORWARD: case QUEUE_TYPE_FORWARD:
ptr = ctx->queue_head; ptr = ctx->queue_head;
break; break;
@ -326,29 +313,25 @@ int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove)
case QUEUE_TYPE_RANDOM: case QUEUE_TYPE_RANDOM:
ptr = ctx->queue_head; ptr = ctx->queue_head;
rnd = rand() % (int) ctx->queue_size; rnd = rand() % (int) ctx->queue_size;
for (int r = 0; r < rnd; r++) for (int r = 0; r < rnd; r++) {
{
ptrPrev = ptr; ptrPrev = ptr;
ptr = ptr->next; ptr = ptr->next;
} }
break; break;
} }
if (!ptr) if (!ptr) {
{
pthread_mutex_unlock(&ctx->queue_mutex); pthread_mutex_unlock(&ctx->queue_mutex);
return ERROR_GENERIC; return ERROR_GENERIC;
} }
if (!remove) if (!remove) {
{
wu->id = ptr->data.id; wu->id = ptr->data.id;
wu->off = ptr->data.off; wu->off = ptr->data.off;
wu->max = ptr->data.max; wu->max = ptr->data.max;
} }
switch (ctx->queue_type) switch (ctx->queue_type) {
{
case QUEUE_TYPE_FORWARD: case QUEUE_TYPE_FORWARD:
ctx->queue_head = (ctx->queue_head)->next; ctx->queue_head = (ctx->queue_head)->next;
break; break;
@ -360,12 +343,9 @@ int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove)
fprintf(stdout, "pop id %ld\n", wu->id); fprintf(stdout, "pop id %ld\n", wu->id);
fflush(stdout); fflush(stdout);
#endif #endif
if (ptrPrev == NULL) if (ptrPrev == NULL) {
{
ctx->queue_head = (ctx->queue_head)->next; ctx->queue_head = (ctx->queue_head)->next;
} } else {
else
{
ptrPrev->next = ptr->next; ptrPrev->next = ptr->next;
} }
break; break;
@ -383,8 +363,7 @@ int wu_queue_pop (wu_queue_ctx_t *ctx, wu_queue_data_t *wu, short remove)
return NO_ERROR; return NO_ERROR;
} }
int wu_queue_destroy (wu_queue_ctx_t *ctx) int wu_queue_destroy(wu_queue_ctx_t *ctx) {
{
#if TEST_UNIT == 1 #if TEST_UNIT == 1
fprintf(stdout, "[%s] enter\n", __func__); fprintf(stdout, "[%s] enter\n", __func__);
fflush(stdout); fflush(stdout);
@ -400,8 +379,7 @@ int wu_queue_destroy (wu_queue_ctx_t *ctx)
// unload the queue // unload the queue
while ((ret = wu_queue_pop(ctx, 0, 1)) == 0) {}; while ((ret = wu_queue_pop(ctx, 0, 1)) == 0) {};
if (ret != QUEUE_EMPTY) if (ret != QUEUE_EMPTY) {
{
#if TEST_UNIT #if TEST_UNIT
fprintf(stderr, "! Error, wu_queue_pop() failed (%d): %s\n", ret, wu_queue_strerror(ret)); fprintf(stderr, "! Error, wu_queue_pop() failed (%d): %s\n", ret, wu_queue_strerror(ret));
#endif #endif
@ -433,10 +411,8 @@ int wu_queue_destroy (wu_queue_ctx_t *ctx)
} }
#if TEST_UNIT == 1 #if TEST_UNIT == 1
int main (void) int main(void) {
{ unsigned int profiles[11][2] = {
unsigned int profiles[11][2] =
{
{ 16384, 5 }, // 0, best for Intel GPU's with Neo { 16384, 5 }, // 0, best for Intel GPU's with Neo
{ 8192, 6 }, // 1, only for Intel NEO { 8192, 6 }, // 1, only for Intel NEO
{ 4096, 7 }, // 2 (old 0) seems the best for all others (also NVIDIA) :D Apple/Intel GPU's stable here { 4096, 7 }, // 2 (old 0) seems the best for all others (also NVIDIA) :D Apple/Intel GPU's stable here
@ -467,12 +443,10 @@ int main (void)
int types_max = (int)(sizeof(types) / sizeof(wu_queue_type_t)); int types_max = (int)(sizeof(types) / sizeof(wu_queue_type_t));
int ret = 0; int ret = 0;
for (i = 0; i < types_max; i++) for (i = 0; i < types_max; i++) {
{
printf("[%d] trying wu_queue_init() in %s mode\n", i, wu_queue_strdesc(types[i])); printf("[%d] trying wu_queue_init() in %s mode\n", i, wu_queue_strdesc(types[i]));
if ((ret = wu_queue_init (&ctx, types[i])) != 0) if ((ret = wu_queue_init(&ctx, types[i])) != 0) {
{
fprintf(stderr, "[%d] Error: wu_queue_init(%s) failed (%d): %s\n", i, wu_queue_strdesc(types[i]), ret, wu_queue_strerror(ret)); fprintf(stderr, "[%d] Error: wu_queue_init(%s) failed (%d): %s\n", i, wu_queue_strdesc(types[i]), ret, wu_queue_strerror(ret));
err++; err++;
continue; continue;
@ -480,12 +454,10 @@ int main (void)
printf("[%d] trying wu_queue_push()\n", i); printf("[%d] trying wu_queue_push()\n", i);
for (id = 0; id < max; id++) for (id = 0; id < max; id++) {
{
sum += id; sum += id;
ret = wu_queue_push(&ctx, id, id << chunk, max); ret = wu_queue_push(&ctx, id, id << chunk, max);
if (ret != 0) if (ret != 0) {
{
fprintf(stderr, "[%d] Error: wu_queue_push(%zu) failed (%d): %s\n", i, id, ret, wu_queue_strerror(ret)); fprintf(stderr, "[%d] Error: wu_queue_push(%zu) failed (%d): %s\n", i, id, ret, wu_queue_strerror(ret));
err++; err++;
continue; continue;
@ -494,8 +466,7 @@ int main (void)
printf("[%d] push sum: %zu\n", i, sum); printf("[%d] push sum: %zu\n", i, sum);
if (wu_queue_print (&ctx) == -1) if (wu_queue_print(&ctx) == -1) {
{
fprintf(stderr, "[%d] wu_queue_print() error\n", i); fprintf(stderr, "[%d] wu_queue_print() error\n", i);
err++; err++;
continue; continue;
@ -505,8 +476,7 @@ int main (void)
while ((ret = wu_queue_pop(&ctx, &wu, 0)) == 0) sum -= wu.id; while ((ret = wu_queue_pop(&ctx, &wu, 0)) == 0) sum -= wu.id;
if (ret != QUEUE_EMPTY) if (ret != QUEUE_EMPTY) {
{
fprintf(stderr, "[%d] Error: wu_queue_pop() failed (%d): %s\n", i, ret, wu_queue_strerror(ret)); fprintf(stderr, "[%d] Error: wu_queue_pop() failed (%d): %s\n", i, ret, wu_queue_strerror(ret));
err++; err++;
continue; continue;
@ -514,23 +484,20 @@ int main (void)
printf("[%d] pop sum: %zu\n", i, sum); printf("[%d] pop sum: %zu\n", i, sum);
if (sum != 0) if (sum != 0) {
{
fprintf(stderr, "[%d] Fail: sum is not zero (%zu)\n", i, sum); fprintf(stderr, "[%d] Fail: sum is not zero (%zu)\n", i, sum);
err++; err++;
continue; continue;
} }
if (wu_queue_print (&ctx) == -1) if (wu_queue_print(&ctx) == -1) {
{
fprintf(stderr, "[%d] wu_queue_print() error\n", i); fprintf(stderr, "[%d] wu_queue_print() error\n", i);
err++; err++;
continue; continue;
} }
printf("[%d] trying wu_queue_destroy()\n", i); printf("[%d] trying wu_queue_destroy()\n", i);
if ((ret = wu_queue_destroy (&ctx)) != 0) if ((ret = wu_queue_destroy(&ctx)) != 0) {
{
fprintf(stderr, "! Error: wu_queue_destroy() failed (%d): %s\n", ret, wu_queue_strerror(ret)); fprintf(stderr, "! Error: wu_queue_destroy() failed (%d): %s\n", ret, wu_queue_strerror(ret));
err++; err++;
continue; continue;
@ -539,8 +506,7 @@ int main (void)
printf("Catched %zu/%zu error(s).\n", err, err_max); printf("Catched %zu/%zu error(s).\n", err, err_max);
if (err == err_max) if (err == err_max) {
{
printf("Self-Test pass\n"); printf("Self-Test pass\n");
return 0; return 0;
} }

View file

@ -42,8 +42,7 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#include <pthread.h> #include <pthread.h>
// enum errors // enum errors
typedef enum wu_queue_error typedef enum wu_queue_error {
{
QUEUE_EMPTY = 1, QUEUE_EMPTY = 1,
NO_ERROR = 0, NO_ERROR = 0,
ERROR_GENERIC = -1, ERROR_GENERIC = -1,
@ -60,8 +59,7 @@ typedef enum wu_queue_error
} wu_queue_error_t; } wu_queue_error_t;
// enum queue types // enum queue types
typedef enum wu_queue_type typedef enum wu_queue_type {
{
QUEUE_TYPE_FORWARD = 0, QUEUE_TYPE_FORWARD = 0,
QUEUE_TYPE_REVERSE, QUEUE_TYPE_REVERSE,
QUEUE_TYPE_RANDOM QUEUE_TYPE_RANDOM
@ -69,8 +67,7 @@ typedef enum wu_queue_type
} wu_queue_type_t; } wu_queue_type_t;
// hold wu data // hold wu data
typedef struct wu_queue_data typedef struct wu_queue_data {
{
size_t id; size_t id;
size_t off; size_t off;
size_t max; size_t max;
@ -80,16 +77,14 @@ typedef struct wu_queue_data
// lists // lists
typedef struct wu_queue_item wu_queue_item_t; typedef struct wu_queue_item wu_queue_item_t;
struct wu_queue_item struct wu_queue_item {
{
wu_queue_data_t data; wu_queue_data_t data;
wu_queue_item_t *next; wu_queue_item_t *next;
wu_queue_item_t *prev; wu_queue_item_t *prev;
}; };
// main ctx // main ctx
typedef struct wu_queue_ctx typedef struct wu_queue_ctx {
{
unsigned int init; unsigned int init;
wu_queue_type_t queue_type; wu_queue_type_t queue_type;

View file

@ -24,32 +24,44 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#include "threads.h" #include "threads.h"
const char *thread_strerror (int error) const char *thread_strerror(int error) {
{ switch (error) {
switch (error) case THREAD_NOERROR:
{ return (const char *) "No error";
case THREAD_NOERROR: return (const char *) "No error"; case THREAD_ERROR_CTX_IS_NULL:
case THREAD_ERROR_CTX_IS_NULL: return (const char *) "CTX IS NULL"; return (const char *) "CTX IS NULL";
case THREAD_ERROR_CTX_IS_INIT: return (const char *) "CTX IS INIT"; case THREAD_ERROR_CTX_IS_INIT:
case THREAD_ERROR_TYPE_INVALID: return (const char *) "INVALID TYPE"; return (const char *) "CTX IS INIT";
case THREAD_ERROR_COUNT_INVALID: return (const char *) "INVALID THREAD COUNT"; case THREAD_ERROR_TYPE_INVALID:
case THREAD_ERROR_ATTR_SETDETACH: return (const char *) "SETDETACHSTATE FAILED"; return (const char *) "INVALID TYPE";
case THREAD_ERROR_ATTR: return (const char *) "INIT ATTR FAILED"; case THREAD_ERROR_COUNT_INVALID:
case THREAD_ERROR_MUTEXATTR: return (const char *) "INIT MUTEXATTR FAILED"; return (const char *) "INVALID THREAD COUNT";
case THREAD_ERROR_CREATE: return (const char *) "PTHREAD CREATE FAILED"; case THREAD_ERROR_ATTR_SETDETACH:
case THREAD_ERROR_MUTEX: return (const char *) "INIT MUTEXFAILED"; return (const char *) "SETDETACHSTATE FAILED";
case THREAD_ERROR_COND: return (const char *) "INIT COND FAILED"; case THREAD_ERROR_ATTR:
case THREAD_ERROR_MUTEX_USLEEP: return (const char *) "INIT MUTEX USLEEP FAILED"; return (const char *) "INIT ATTR FAILED";
case THREAD_ERROR_COND_USLEEP: return (const char *) "INIT COND USLEEP FAILED"; case THREAD_ERROR_MUTEXATTR:
case THREAD_ERROR_GENERIC: return (const char *) "GENERIC ERROR"; return (const char *) "INIT MUTEXATTR FAILED";
case THREAD_ERROR_ALLOC: return (const char *) "ALLOC FAILED"; case THREAD_ERROR_CREATE:
return (const char *) "PTHREAD CREATE FAILED";
case THREAD_ERROR_MUTEX:
return (const char *) "INIT MUTEXFAILED";
case THREAD_ERROR_COND:
return (const char *) "INIT COND FAILED";
case THREAD_ERROR_MUTEX_USLEEP:
return (const char *) "INIT MUTEX USLEEP FAILED";
case THREAD_ERROR_COND_USLEEP:
return (const char *) "INIT COND USLEEP FAILED";
case THREAD_ERROR_GENERIC:
return (const char *) "GENERIC ERROR";
case THREAD_ERROR_ALLOC:
return (const char *) "ALLOC FAILED";
} }
return (const char *) "GENERIC"; return (const char *) "GENERIC";
} }
int thread_init (thread_ctx_t *ctx, short type, size_t thread_count) int thread_init(thread_ctx_t *ctx, short type, size_t thread_count) {
{
if (!ctx) return THREAD_ERROR_CTX_IS_NULL; if (!ctx) return THREAD_ERROR_CTX_IS_NULL;
if (ctx->init) return THREAD_ERROR_CTX_IS_INIT; if (ctx->init) return THREAD_ERROR_CTX_IS_INIT;
if (type != THREAD_TYPE_ASYNC && type != THREAD_TYPE_SEQ) return THREAD_ERROR_TYPE_INVALID; if (type != THREAD_TYPE_ASYNC && type != THREAD_TYPE_SEQ) return THREAD_ERROR_TYPE_INVALID;
@ -62,28 +74,24 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
ctx->enable_condusleep = (type == THREAD_TYPE_ASYNC && thread_count == 1); ctx->enable_condusleep = (type == THREAD_TYPE_ASYNC && thread_count == 1);
ctx->thread_handles = (pthread_t *) calloc(thread_count, sizeof(pthread_t)); ctx->thread_handles = (pthread_t *) calloc(thread_count, sizeof(pthread_t));
if (!ctx->thread_handles) if (!ctx->thread_handles) {
{
return THREAD_ERROR_ALLOC; return THREAD_ERROR_ALLOC;
} }
ctx->thread_mutexs = (pthread_mutex_t *) calloc(thread_count, sizeof(pthread_mutex_t)); ctx->thread_mutexs = (pthread_mutex_t *) calloc(thread_count, sizeof(pthread_mutex_t));
if (!ctx->thread_mutexs) if (!ctx->thread_mutexs) {
{
free(ctx->thread_handles); free(ctx->thread_handles);
return THREAD_ERROR_ALLOC; return THREAD_ERROR_ALLOC;
} }
ctx->thread_conds = (pthread_cond_t *) calloc(thread_count, sizeof(pthread_cond_t)); ctx->thread_conds = (pthread_cond_t *) calloc(thread_count, sizeof(pthread_cond_t));
if (!ctx->thread_conds) if (!ctx->thread_conds) {
{
free(ctx->thread_handles); free(ctx->thread_handles);
free(ctx->thread_mutexs); free(ctx->thread_mutexs);
return THREAD_ERROR_ALLOC; return THREAD_ERROR_ALLOC;
} }
if (pthread_attr_init (&ctx->attr) != 0) if (pthread_attr_init(&ctx->attr) != 0) {
{
free(ctx->thread_handles); free(ctx->thread_handles);
free(ctx->thread_mutexs); free(ctx->thread_mutexs);
free(ctx->thread_conds); free(ctx->thread_conds);
@ -92,8 +100,7 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
pthread_attr_setdetachstate(&ctx->attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setdetachstate(&ctx->attr, PTHREAD_CREATE_JOINABLE);
if (pthread_mutexattr_init (&ctx->mutex_attr) != 0) if (pthread_mutexattr_init(&ctx->mutex_attr) != 0) {
{
free(ctx->thread_handles); free(ctx->thread_handles);
free(ctx->thread_mutexs); free(ctx->thread_mutexs);
free(ctx->thread_conds); free(ctx->thread_conds);
@ -103,10 +110,8 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
pthread_mutexattr_settype(&ctx->mutex_attr, PTHREAD_MUTEX_ERRORCHECK); pthread_mutexattr_settype(&ctx->mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
if (ctx->enable_condusleep) if (ctx->enable_condusleep) {
{ if (pthread_mutex_init(&ctx->thread_mutex_usleep, NULL) != 0) {
if (pthread_mutex_init (&ctx->thread_mutex_usleep, NULL) != 0)
{
free(ctx->thread_handles); free(ctx->thread_handles);
free(ctx->thread_mutexs); free(ctx->thread_mutexs);
free(ctx->thread_conds); free(ctx->thread_conds);
@ -115,8 +120,7 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
return THREAD_ERROR_MUTEX_USLEEP; return THREAD_ERROR_MUTEX_USLEEP;
} }
if (pthread_cond_init (&ctx->thread_cond_usleep, NULL) != 0) if (pthread_cond_init(&ctx->thread_cond_usleep, NULL) != 0) {
{
free(ctx->thread_handles); free(ctx->thread_handles);
free(ctx->thread_mutexs); free(ctx->thread_mutexs);
free(ctx->thread_conds); free(ctx->thread_conds);
@ -130,18 +134,14 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
int err = 0; int err = 0;
int z = 0; int z = 0;
for (z = 0; z < (int) ctx->thread_count; z++) for (z = 0; z < (int) ctx->thread_count; z++) {
{ if (ctx->type == THREAD_TYPE_ASYNC) {
if (ctx->type == THREAD_TYPE_ASYNC) if (pthread_mutex_init(&ctx->thread_mutexs[z], NULL) != 0) {
{
if (pthread_mutex_init (&ctx->thread_mutexs[z], NULL) != 0)
{
err = THREAD_ERROR_MUTEX; err = THREAD_ERROR_MUTEX;
break; break;
} }
if (pthread_cond_init (&ctx->thread_conds[z], NULL) != 0) if (pthread_cond_init(&ctx->thread_conds[z], NULL) != 0) {
{
pthread_mutex_destroy(&ctx->thread_mutexs[z]); pthread_mutex_destroy(&ctx->thread_mutexs[z]);
err = THREAD_ERROR_COND; err = THREAD_ERROR_COND;
break; break;
@ -149,18 +149,15 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
} }
} }
if (err != 0) if (err != 0) {
{
z--; // step back z--; // step back
for (; z >= 0; z--) for (; z >= 0; z--) {
{
pthread_cond_destroy(&ctx->thread_conds[z]); pthread_cond_destroy(&ctx->thread_conds[z]);
pthread_mutex_destroy(&ctx->thread_mutexs[z]); pthread_mutex_destroy(&ctx->thread_mutexs[z]);
} }
if (ctx->enable_condusleep) if (ctx->enable_condusleep) {
{
pthread_mutex_destroy(&ctx->thread_mutex_usleep); pthread_mutex_destroy(&ctx->thread_mutex_usleep);
pthread_cond_destroy(&ctx->thread_cond_usleep); pthread_cond_destroy(&ctx->thread_cond_usleep);
} }
@ -177,19 +174,16 @@ int thread_init (thread_ctx_t *ctx, short type, size_t thread_count)
return 0; return 0;
} }
int thread_destroy (thread_ctx_t *ctx) int thread_destroy(thread_ctx_t *ctx) {
{
if (!ctx) return -1; if (!ctx) return -1;
if (!ctx->init) return -2; if (!ctx->init) return -2;
if (ctx->enable_condusleep) if (ctx->enable_condusleep) {
{
pthread_cond_destroy(&ctx->thread_cond_usleep); pthread_cond_destroy(&ctx->thread_cond_usleep);
pthread_mutex_destroy(&ctx->thread_mutex_usleep); pthread_mutex_destroy(&ctx->thread_mutex_usleep);
} }
for (size_t z = 0; z < ctx->thread_count; z++) for (size_t z = 0; z < ctx->thread_count; z++) {
{
pthread_cond_destroy(&ctx->thread_conds[z]); pthread_cond_destroy(&ctx->thread_conds[z]);
pthread_mutex_destroy(&ctx->thread_mutexs[z]); pthread_mutex_destroy(&ctx->thread_mutexs[z]);
} }
@ -206,26 +200,21 @@ int thread_destroy (thread_ctx_t *ctx)
return 0; return 0;
} }
int thread_start (thread_ctx_t *ctx, thread_args_t *t_arg) int thread_start(thread_ctx_t *ctx, thread_args_t *t_arg) {
{
int err = 0; int err = 0;
int z = 0; int z = 0;
for (z = 0; z < (int) ctx->thread_count; z++) for (z = 0; z < (int) ctx->thread_count; z++) {
{ if (pthread_create(&ctx->thread_handles[z], &ctx->attr, (ctx->type == THREAD_TYPE_ASYNC) ? computing_process_async : computing_process, (void *) &t_arg[z]) != 0) {
if (pthread_create (&ctx->thread_handles[z], &ctx->attr, (ctx->type == THREAD_TYPE_ASYNC) ? computing_process_async : computing_process, (void *) &t_arg[z]) != 0)
{
err = THREAD_ERROR_CREATE; err = THREAD_ERROR_CREATE;
break; break;
} }
} }
if (err != 0) if (err != 0) {
{
z--; // step back z--; // step back
for (; z >= 0; z++) for (; z >= 0; z++) {
{
pthread_cancel(ctx->thread_handles[z]); pthread_cancel(ctx->thread_handles[z]);
pthread_join(ctx->thread_handles[z], NULL); pthread_join(ctx->thread_handles[z], NULL);
} }
@ -236,10 +225,8 @@ int thread_start (thread_ctx_t *ctx, thread_args_t *t_arg)
return 0; return 0;
} }
int thread_stop (thread_ctx_t *ctx) int thread_stop(thread_ctx_t *ctx) {
{ for (size_t z = 0; z < ctx->thread_count; z++) {
for (size_t z = 0; z < ctx->thread_count; z++)
{
if (ctx->type == THREAD_TYPE_ASYNC) pthread_cancel(ctx->thread_handles[z]); if (ctx->type == THREAD_TYPE_ASYNC) pthread_cancel(ctx->thread_handles[z]);
pthread_join(ctx->thread_handles[z], NULL); pthread_join(ctx->thread_handles[z], NULL);
} }
@ -248,8 +235,7 @@ int thread_stop (thread_ctx_t *ctx)
} }
__attribute__((format(printf, 1, 2))) __attribute__((format(printf, 1, 2)))
void tprintf (const char * restrict format, ...) void tprintf(const char *restrict format, ...) {
{
flockfile(stdout); flockfile(stdout);
va_list va_args; va_list va_args;
@ -262,24 +248,28 @@ void tprintf (const char * restrict format, ...)
fflush(stdout); fflush(stdout);
} }
const char *thread_status_strdesc (thread_status_t s) const char *thread_status_strdesc(thread_status_t s) {
{ switch (s) {
switch (s) case TH_START:
{ return (const char *) "START";
case TH_START: return (const char *) "START"; case TH_WAIT:
case TH_WAIT: return (const char *) "WAIT"; return (const char *) "WAIT";
case TH_PROCESSING: return (const char *) "PROCESSING"; case TH_PROCESSING:
case TH_ERROR: return (const char *) "ERROR"; return (const char *) "PROCESSING";
case TH_STOP: return (const char *) "STOP"; case TH_ERROR:
case TH_FOUND_KEY: return (const char *) "FOUND_KEY"; return (const char *) "ERROR";
case TH_END: return (const char *) "END"; case TH_STOP:
return (const char *) "STOP";
case TH_FOUND_KEY:
return (const char *) "FOUND_KEY";
case TH_END:
return (const char *) "END";
} }
return (const char *) "... or die tryin'"; return (const char *) "... or die tryin'";
} }
bool thread_setEnd (thread_ctx_t *ctx, thread_args_t *t_arg) bool thread_setEnd(thread_ctx_t *ctx, thread_args_t *t_arg) {
{
bool found = false; bool found = false;
size_t z; size_t z;
@ -287,11 +277,9 @@ bool thread_setEnd (thread_ctx_t *ctx, thread_args_t *t_arg)
int m_ret = 0; int m_ret = 0;
int c_ret = 0; int c_ret = 0;
for (z = 0; z < ctx->thread_count; z++) for (z = 0; z < ctx->thread_count; z++) {
{
m_ret = pthread_mutex_lock(&ctx->thread_mutexs[z]); m_ret = pthread_mutex_lock(&ctx->thread_mutexs[z]);
if (m_ret != 0) if (m_ret != 0) {
{
tprintf("[%zu] [%s] Error: pthread_mutex_lock() failed (%d): %s\n", z, __func__, m_ret, strerror(m_ret)); tprintf("[%zu] [%s] Error: pthread_mutex_lock() failed (%d): %s\n", z, __func__, m_ret, strerror(m_ret));
} }
@ -301,8 +289,7 @@ bool thread_setEnd (thread_ctx_t *ctx, thread_args_t *t_arg)
tprintf("[%zu] [%s] Thread status: %s\n", z, __func__, thread_status_strdesc(t_arg[z].status)); tprintf("[%zu] [%s] Thread status: %s\n", z, __func__, thread_status_strdesc(t_arg[z].status));
#endif #endif
if (tmp == TH_FOUND_KEY || tmp == TH_END || tmp == TH_ERROR) if (tmp == TH_FOUND_KEY || tmp == TH_END || tmp == TH_ERROR) {
{
if (tmp == TH_FOUND_KEY) found = true; if (tmp == TH_FOUND_KEY) found = true;
pthread_mutex_unlock(&ctx->thread_mutexs[z]); pthread_mutex_unlock(&ctx->thread_mutexs[z]);
continue; continue;
@ -314,15 +301,13 @@ bool thread_setEnd (thread_ctx_t *ctx, thread_args_t *t_arg)
t_arg[z].status = TH_STOP; t_arg[z].status = TH_STOP;
if (tmp == TH_WAIT) if (tmp == TH_WAIT) {
{
#if DEBUGME > 0 #if DEBUGME > 0
tprintf("[%zu] [%s] Send cond_signal to thread\n", z, __func__); tprintf("[%zu] [%s] Send cond_signal to thread\n", z, __func__);
#endif #endif
c_ret = pthread_cond_signal(&ctx->thread_conds[z]); c_ret = pthread_cond_signal(&ctx->thread_conds[z]);
if (c_ret != 0) if (c_ret != 0) {
{
tprintf("[%zu] [%s] Error: pthread_cond_signal() failed (%d): %s\n", z, __func__, c_ret, strerror(c_ret)); tprintf("[%zu] [%s] Error: pthread_cond_signal() failed (%d): %s\n", z, __func__, c_ret, strerror(c_ret));
} }
} }
@ -333,8 +318,7 @@ bool thread_setEnd (thread_ctx_t *ctx, thread_args_t *t_arg)
return found; return found;
} }
void *computing_process (void *arg) void *computing_process(void *arg) {
{
thread_args_t *a = (thread_args_t *) arg; thread_args_t *a = (thread_args_t *) arg;
uint64_t off = 0; uint64_t off = 0;
@ -355,16 +339,13 @@ void *computing_process (void *arg)
off = wu.off; off = wu.off;
a->slice = wu.id + 1; a->slice = wu.id + 1;
if (ctx->queue_ctx.queue_type == QUEUE_TYPE_RANDOM) if (ctx->queue_ctx.queue_type == QUEUE_TYPE_RANDOM) {
{
#if DEBUGME > 0 #if DEBUGME > 0
printf("[%zu] Slice %zu (off %zu), max %zu, remain %zu slice(s)\n", z, wu.id + 1, wu.off, wu.max, wu.rem); printf("[%zu] Slice %zu (off %zu), max %zu, remain %zu slice(s)\n", z, wu.id + 1, wu.off, wu.max, wu.rem);
#else #else
printf("[%zu] Slice %zu/%zu (%zu remain)\n", z, wu.id + 1, wu.max, wu.rem); printf("[%zu] Slice %zu/%zu (%zu remain)\n", z, wu.id + 1, wu.max, wu.rem);
#endif // DEBUGME #endif // DEBUGME
} } else {
else
{
#if DEBUGME > 0 #if DEBUGME > 0
printf("[%zu] Slice %zu/%zu, off %zu\n", z, wu.id + 1, wu.max, wu.off); printf("[%zu] Slice %zu/%zu, off %zu\n", z, wu.id + 1, wu.max, wu.off);
#else #else
@ -378,27 +359,22 @@ void *computing_process (void *arg)
a->r = false; a->r = false;
a->err = false; a->err = false;
if (ret < 1) // error or nada if (ret < 1) { // error or nada
{
if (ret == -1) a->err = true; if (ret == -1) a->err = true;
pthread_exit(NULL); pthread_exit(NULL);
} }
if (!ctx->force_hitag2_opencl) if (!ctx->force_hitag2_opencl) {
{
#if DEBUGME >= 2 #if DEBUGME >= 2
printf("[slave][%zu] master, I found %5u candidates @ slice %zu\n", z, matches_found[0], a->slice + 1); printf("[slave][%zu] master, I found %5u candidates @ slice %zu\n", z, matches_found[0], a->slice + 1);
fflush(stdout); fflush(stdout);
#endif #endif
for (uint32_t match = 0; match < matches_found[0]; match++) for (uint32_t match = 0; match < matches_found[0]; match++) {
{
a->r = try_state(matches[match], uid, aR2, nR1, nR2, &a->key); a->r = try_state(matches[match], uid, aR2, nR1, nR2, &a->key);
if (a->r) break; if (a->r) break;
} }
} } else {
else
{
// the OpenCL kernel return only one key if found, else nothing // the OpenCL kernel return only one key if found, else nothing
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -413,8 +389,7 @@ void *computing_process (void *arg)
pthread_exit(NULL); pthread_exit(NULL);
} }
void *computing_process_async (void *arg) void *computing_process_async(void *arg) {
{
thread_args_t *a = (thread_args_t *) arg; thread_args_t *a = (thread_args_t *) arg;
size_t z = a->device_id; size_t z = a->device_id;
@ -442,8 +417,7 @@ void *computing_process_async (void *arg)
// size_t slice = 0; // size_t slice = 0;
int ret = 0; int ret = 0;
if (status == TH_START) if (status == TH_START) {
{
#if TDEBUG >= 1 #if TDEBUG >= 1
printf("[slave][%zu] plat id %d, uid %u, aR2 %u, nR1 %u, nR2 %u, Initial status: %s\n", z, ctx->id_platform, uid, aR2, nR1, nR2, thread_status_strdesc(status)); printf("[slave][%zu] plat id %d, uid %u, aR2 %u, nR1 %u, nR2 %u, Initial status: %s\n", z, ctx->id_platform, uid, aR2, nR1, nR2, thread_status_strdesc(status));
#endif #endif
@ -451,28 +425,22 @@ void *computing_process_async (void *arg)
// proceed to next // proceed to next
} }
do // slave do { // slave
{ if (status == TH_WAIT) {
if (status == TH_WAIT)
{
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
// update thread status to WAIT, todo: check with multiple devices // update thread status to WAIT, todo: check with multiple devices
if (a->status == TH_END) // other threads found the key if (a->status == TH_END) { // other threads found the key
{
fflush(stdout); fflush(stdout);
status = TH_END; status = TH_END;
a->quit = true; a->quit = true;
pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]);
pthread_exit(NULL); pthread_exit(NULL);
} } else {
else
{
a->status = TH_WAIT; a->status = TH_WAIT;
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -499,8 +467,7 @@ void *computing_process_async (void *arg)
pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]);
if (status == TH_WAIT) if (status == TH_WAIT) {
{
#if TDEBUG >=1 #if TDEBUG >=1
printf("[slave] ! Error: need to be TH_PROCESSING or TH_END, not TH_WAIT ... exit\n"); printf("[slave] ! Error: need to be TH_PROCESSING or TH_END, not TH_WAIT ... exit\n");
fflush(stdout); fflush(stdout);
@ -509,8 +476,7 @@ void *computing_process_async (void *arg)
} }
} }
if (status == TH_ERROR) if (status == TH_ERROR) {
{
#if TDEBUG >= 1 #if TDEBUG >= 1
printf("[slave][%zu] master, got error signal, proceed with exit\n", z); printf("[slave][%zu] master, got error signal, proceed with exit\n", z);
fflush(stdout); fflush(stdout);
@ -518,8 +484,7 @@ void *computing_process_async (void *arg)
pthread_exit(NULL); pthread_exit(NULL);
} }
if (status == TH_PROCESSING) if (status == TH_PROCESSING) {
{
#if TDEBUG >= 2 #if TDEBUG >= 2
printf("[slave][%zu] master, got a work-unit, processing ...\n", z); printf("[slave][%zu] master, got a work-unit, processing ...\n", z);
fflush(stdout); fflush(stdout);
@ -530,16 +495,13 @@ void *computing_process_async (void *arg)
off = wu.off; off = wu.off;
a->slice = wu.id + 1; a->slice = wu.id + 1;
if (ctx->queue_ctx.queue_type == QUEUE_TYPE_RANDOM) if (ctx->queue_ctx.queue_type == QUEUE_TYPE_RANDOM) {
{
#if DEBUGME > 0 #if DEBUGME > 0
printf("[%zu] Slice %zu (off %zu), max %zu, remain %zu slice(s)\n", z, wu.id + 1, wu.off, wu.max, wu.rem); printf("[%zu] Slice %zu (off %zu), max %zu, remain %zu slice(s)\n", z, wu.id + 1, wu.off, wu.max, wu.rem);
#else #else
printf("[%zu] Slice %zu/%zu (%zu remain)\n", z, wu.id + 1, wu.max, wu.rem); printf("[%zu] Slice %zu/%zu (%zu remain)\n", z, wu.id + 1, wu.max, wu.rem);
#endif // DEBUGME #endif // DEBUGME
} } else {
else
{
#if DEBUGME > 0 #if DEBUGME > 0
printf("[%zu] Slice %zu/%zu, off %zu\n", z, wu.id + 1, wu.max, wu.off); printf("[%zu] Slice %zu/%zu, off %zu\n", z, wu.id + 1, wu.max, wu.off);
#else #else
@ -551,10 +513,8 @@ void *computing_process_async (void *arg)
ret = runKernel(ctx, (uint32_t) off, matches, matches_found, z); ret = runKernel(ctx, (uint32_t) off, matches, matches_found, z);
if (ret < 1) // error or nada if (ret < 1) { // error or nada
{ if (ret == -1) {
if (ret == -1)
{
// untested code // untested code
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
a->err = true; a->err = true;
@ -565,8 +525,7 @@ void *computing_process_async (void *arg)
fflush(stdout); fflush(stdout);
#endif #endif
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -592,8 +551,7 @@ void *computing_process_async (void *arg)
pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]);
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -606,17 +564,14 @@ void *computing_process_async (void *arg)
continue; continue;
} }
if (!ctx->force_hitag2_opencl) if (!ctx->force_hitag2_opencl) {
{
#if TDEBUG >= 1 #if TDEBUG >= 1
printf("[slave][%zu] master, we got %5u candidates. Proceed to validation\n", z, matches_found[0]); printf("[slave][%zu] master, we got %5u candidates. Proceed to validation\n", z, matches_found[0]);
fflush(stdout); fflush(stdout);
#endif #endif
for (uint32_t match = 0; match < matches_found[0]; match++) for (uint32_t match = 0; match < matches_found[0]; match++) {
{ if (a->quit) {
if (a->quit)
{
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
a->status = TH_END; a->status = TH_END;
pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]);
@ -625,8 +580,7 @@ void *computing_process_async (void *arg)
fflush(stdout); fflush(stdout);
#endif #endif
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -639,8 +593,7 @@ void *computing_process_async (void *arg)
} }
a->r = try_state(matches[match], uid, aR2, nR1, nR2, &a->key); a->r = try_state(matches[match], uid, aR2, nR1, nR2, &a->key);
if (a->r) if (a->r) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
a->s = matches[match]; a->s = matches[match];
status = a->status = TH_FOUND_KEY; status = a->status = TH_FOUND_KEY;
@ -651,8 +604,7 @@ void *computing_process_async (void *arg)
fflush(stdout); fflush(stdout);
#endif #endif
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -665,8 +617,7 @@ void *computing_process_async (void *arg)
} }
} }
if (a->quit) if (a->quit) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
a->status = TH_END; a->status = TH_END;
pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_unlock(&a->thread_ctx->thread_mutexs[z]);
@ -675,8 +626,7 @@ void *computing_process_async (void *arg)
fflush(stdout); fflush(stdout);
#endif #endif
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -691,9 +641,7 @@ void *computing_process_async (void *arg)
// setting internal status to wait // setting internal status to wait
status = TH_WAIT; status = TH_WAIT;
continue; continue;
} } else {
else
{
// the OpenCL kernel return only one key if found, else nothing // the OpenCL kernel return only one key if found, else nothing
pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]); pthread_mutex_lock(&a->thread_ctx->thread_mutexs[z]);
@ -707,8 +655,7 @@ void *computing_process_async (void *arg)
fflush(stdout); fflush(stdout);
#endif #endif
if (a->thread_ctx->enable_condusleep) if (a->thread_ctx->enable_condusleep) {
{
pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep); pthread_mutex_lock(&a->thread_ctx->thread_mutex_usleep);
pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond pthread_cond_signal(&a->thread_ctx->thread_cond_usleep); // unlock master/TH_PROCESSING cond
#if TDEBUG >= 1 #if TDEBUG >= 1
@ -721,16 +668,12 @@ void *computing_process_async (void *arg)
} }
} }
if (status >= TH_FOUND_KEY) if (status >= TH_FOUND_KEY) {
{
#if TDEBUG >= 1 #if TDEBUG >= 1
if (status == TH_FOUND_KEY) if (status == TH_FOUND_KEY) {
{
printf("[slave][%zu] master, TH_FOUND_KEY, if you see this message, something is wrong\n", z); printf("[slave][%zu] master, TH_FOUND_KEY, if you see this message, something is wrong\n", z);
fflush(stdout); fflush(stdout);
} } else if (status == TH_END) {
else if (status == TH_END)
{
printf("[slave][%zu] master, TH_END reached\n", z); printf("[slave][%zu] master, TH_END reached\n", z);
fflush(stdout); fflush(stdout);
} }

View file

@ -34,8 +34,7 @@ License: GNU General Public License v3 or any later version (see LICENSE.txt)
#include "opencl.h" #include "opencl.h"
#include "hitag2.h" #include "hitag2.h"
typedef enum thread_status typedef enum thread_status {
{
TH_START = 0, TH_START = 0,
TH_WAIT, TH_WAIT,
TH_PROCESSING, TH_PROCESSING,
@ -46,15 +45,13 @@ typedef enum thread_status
} thread_status_t; } thread_status_t;
typedef enum thread_type typedef enum thread_type {
{
THREAD_TYPE_SEQ = 0, THREAD_TYPE_SEQ = 0,
THREAD_TYPE_ASYNC THREAD_TYPE_ASYNC
} thread_type_t; } thread_type_t;
typedef enum thread_error typedef enum thread_error {
{
THREAD_NOERROR = 0, THREAD_NOERROR = 0,
THREAD_ERROR_CTX_IS_NULL = -1, THREAD_ERROR_CTX_IS_NULL = -1,
THREAD_ERROR_CTX_IS_INIT = -2, THREAD_ERROR_CTX_IS_INIT = -2,
@ -73,8 +70,7 @@ typedef enum thread_error
} thread_error_t; } thread_error_t;
typedef struct threads_ctx typedef struct threads_ctx {
{
short init; short init;
short type; short type;
@ -99,8 +95,7 @@ typedef struct threads_ctx
} thread_ctx_t; } thread_ctx_t;
// used by threads engine // used by threads engine
typedef struct thread_arg typedef struct thread_arg {
{
thread_status_t status; thread_status_t status;
unsigned char pad1[4]; unsigned char pad1[4];
size_t max_threads; size_t max_threads;