From f8bd133373a49759533ebf2fc77f14a54e75a986 Mon Sep 17 00:00:00 2001 From: jsteube Date: Wed, 22 Nov 2023 03:36:28 +0000 Subject: [PATCH] Add new function count_bits_32() in inc_common.cl --- OpenCL/inc_common.cl | 18 ++++++++++++++++++ OpenCL/inc_common.h | 2 ++ 2 files changed, 20 insertions(+) diff --git a/OpenCL/inc_common.cl b/OpenCL/inc_common.cl index c9474a0b5..818b32e69 100644 --- a/OpenCL/inc_common.cl +++ b/OpenCL/inc_common.cl @@ -2992,6 +2992,24 @@ DECLSPEC int hc_execute_keyboard_layout_mapping (PRIVATE_AS u32 *w, const int pw return out_len; } +DECLSPEC int count_bits_32 (const u32 v0, const u32 v1) +{ + u32 r = v0 ^ v1; + + if (r == 0) return 0; + + // from https://stackoverflow.com/questions/109023/count-the-number-of-set-bits-in-a-32-bit-integer + + r = r - ((r >> 1) & 0x55555555); // add pairs of bits + r = (r & 0x33333333) + ((r >> 2) & 0x33333333); // quads + r = (r + (r >> 4)) & 0x0F0F0F0F; // groups of 8 + r *= 0x01010101; // horizontal sum of bytes + + // return just that top byte (after truncating to 32-bit even when int is wider than uint32_t) + + return r >> 24; +} + /** * vector functions */ diff --git a/OpenCL/inc_common.h b/OpenCL/inc_common.h index 940e3b015..434de8866 100644 --- a/OpenCL/inc_common.h +++ b/OpenCL/inc_common.h @@ -315,6 +315,8 @@ DECLSPEC int is_valid_printable_8 (const u8 v); DECLSPEC int is_valid_printable_32 (const u32 v); DECLSPEC int hc_find_keyboard_layout_map (const u32 search, const int search_len, LOCAL_AS keyboard_layout_mapping_t *s_keyboard_layout_mapping_buf, const int keyboard_layout_mapping_cnt); DECLSPEC int hc_execute_keyboard_layout_mapping (PRIVATE_AS u32 *w, const int pw_len, LOCAL_AS keyboard_layout_mapping_t *s_keyboard_layout_mapping_buf, const int keyboard_layout_mapping_cnt); +DECLSPEC int count_bits_32 (const u32 v0, const u32 v1); + DECLSPEC void make_utf16be (PRIVATE_AS const u32x *in, PRIVATE_AS u32x *out1, PRIVATE_AS u32x *out2); DECLSPEC void make_utf16beN (PRIVATE_AS const u32x *in, PRIVATE_AS u32x *out1, PRIVATE_AS u32x *out2); DECLSPEC void make_utf16beN_S (PRIVATE_AS const u32 *in, PRIVATE_AS u32 *out1, PRIVATE_AS u32 *out2);