How to calculate reconciliation factor in CRC32 for CVN number and CALID - crc32

How to calculate reconciliation factor in CRC32 for CVN number and CALID
Can someone help he with this How to calculate reconciliation factor in CRC32 and crc16.
but calculated factor value should be like to get same sum Every time for CVN calculation
I want formula of get offset or Reconciliation factor for CRC32 calculation .
Let me clarify for this : CVN : calibration Vehicle network identification . The solution as i want for ex: I have two different structure where we have 10 paramater and from that check sum value as ex 0xfefeABCD and 0x12345678 and on that structure im have to add one more parameter which calibration factor , When I add this parameter on both structure the checksum value get modify , but I need a algorithm for that to get same checksum value for both the structure by adding calibration factor and offset . NOTE : both structure have same 10 Variables but value is different , Im not having idea about value of these structure ,but still i need same checksum by adding factor value on structure .
Im using this function :
As the data im passing to function is :
final result will be store in buffer
Please let me know what thing im missing so to get result as same we want .
Im sure that im missing something .
Start code :
#include <stdio.h>
#define CRCPOLY 0xEDB88320
#define CRCINV 0x5B358FD3 // inverse poly of (x ^N) mod CRCPOLY
#define INITXOR 0xFFFFFFFF
#define FINALXOR 0xFFFFFFFF
void make_crc_revtable ( unsigned int * crc_revtable ) ;
int crc32_bitoriented ( unsigned char * buffer , int length );
unsigned int crc_table[256];
unsigned char buffer[]= { 0x0, 0x1, 0x2, 0x3, 0x4,0x5, 0x6, 0x7, 0x8, 0x9,0xA, 0xB, 0xC, 0xD, 0xE,0xF, 0x0, 0x1, 0x2, 0x3,0x0, 0x0, 0x0, 0x0, 0x0 };
unsigned int crc_revtable [256];
unsigned int tcrcreg ;
unsigned int CRC_32 ;
unsigned int fix_pos = 21;
unsigned int length = 256;
void fix_crc_pos ( unsigned char * buffer ,int length ,unsigned int tcrcreg ,int fix_pos ,unsigned int * crc_table ,unsigned int * crc_revtable )
{
int i;
// make sure fix_pos is within 0..( length -1)
fix_pos = ((fix_pos % length) + length) % length;
// calculate crc register at position fix_pos ; this is essentially crc32 ()
unsigned int crcreg = INITXOR ;
for (i = 0; i < fix_pos ; ++i)
{
crcreg = (crcreg >> 8) ^ crc_table[((crcreg ^ buffer [i]) & 0xFF)];
}
// inject crcreg as content
for (i = 0; i < 4; ++i)
{
buffer[fix_pos + i] = ((crcreg >> i * 8) & 0xFF);
}
// calculate crc backwards to fix_pos , beginning at the end
tcrcreg = (tcrcreg ^FINALXOR) ;
for (i = length - 1; i >= fix_pos ; --i)
{
tcrcreg = ((tcrcreg << 8) ^ (crc_revtable[tcrcreg >> 3*8] ^ buffer[i]));
}
// inject new content
for (i = 0; i < 4; ++i)
{
buffer[fix_pos + i] = (tcrcreg >> i * 8) & 0xFF;
}
}
void make_crc_revtable ( unsigned int *crc_revtable )
{
unsigned int c;
int n , k;
for (n = 0; n < 256; n ++)
{
c = n << 3*8;
for (k = 0; k < 8; k ++)
{
if (( c & 0x80000000 ) != 0)
{
c = ((( c ^ CRCPOLY ) << 1) | 1);
}
else
{
c = (c <<1);
}
}
crc_revtable [n] = c;
}
}
void make_crc_table ( unsigned int * table )
{
unsigned int c;
int n , k;
for (n = 0; n < 256; n ++)
{
c = n ;
for (k = 0; k < 8; k ++)
{
if (( c & 1) != 0)
{
c = CRCPOLY ^ ( c >> 1);
}
else
{
c = c >> 1;
}
}
table [n] = c;
}
}
int crc32_bitoriented ( unsigned char * buffer , int length )
{
int i , j;
unsigned int crcreg = INITXOR ;
for (j = 0; j < length ; ++ j )
{
unsigned char b = buffer [ j ];
for (i = 0; i < 8; ++ i)
{
if (( crcreg ^ b ) & 1)
{
crcreg = ( crcreg >> 1) ^ CRCPOLY ;
}
else
{
crcreg >>= 1;
}
b >>= 1;
}
}
return crcreg ^ FINALXOR ;
}
int main()
{
length = sizeof(buffer);
CRC_32 = crc32_bitoriented( buffer , length );
printf("\nCRC_32 :%x ",CRC_32);
make_crc_table(&crc_table[0]);
make_crc_revtable(&crc_revtable[0]);
fix_crc_pos(buffer, length, tcrcreg, fix_pos, &crc_table[0], &crc_revtable[0]);
printf("\nModified Buffer:\n");
for(int i=1;i<=length ;i++)
{
printf("0x%x ",buffer[i-1]);
if(0== (i%5))
{
printf("\n");
}
}printf("\n");
CRC_32 = crc32_bitoriented( buffer , length );
printf("\nFinal CRC_32 :%x ",CRC_32);
return 0;
}
----------------------END Code---------------
How do we get Offset and reconciliation factor value to get same CRC every time ?
unchanged data in buffer:
0x0 0x1 0x2 0x3 0x4
0x5 0x6 0x7 0x8 0x9
0xA 0xB 0xC 0xD 0xE
0xF 0x0 0x1 0x2 0x3
0x0 0x0 0x0 0x0 0x0

Your code lost some things in copying. There needs to be a length parameter in fix_crc_pos(). In make_crc_revtable() you go to 256, not 25. You need a place to put the reverse table: uint32_t crc_revtable[256];. You need to make the forward table:
void make_crc_table(uint32_t *crc_table) {
for (int n = 0; n < 256; n++) {
uint32_t crc = n;
for (int k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ CRCPOLY : crc >> 1;
crc_table[n] = crc;
}
}
and a place to put that somewhere: uint32_t crc_table[256];.
Then fix_crc_pos() will work. You give it the data you want the CRC over, buffer with length length. You give it the CRC you want to force the data to have, tcrcreg and the offset in the data, fix_pos, where there are four bytes you are allowing it to modify to get that CRC. And you provide it with the two tables you built by calling the make functions.

Related

Getting random integer without 3 set bits in a row [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
Is there a performant way to generate an unbiased 64b random integer without 3 set bits in a row, assuming a fast-and-unbiased input PRNG? I don't care about 'wasting bits' of the input source.
That is, something better than the naive rejection-sampling approach:
uint64_t r;
do {
r = get_rand_64();
} while (r & (r >> 1) & (r >> 2));
...which "works", but is very slow. It looks like it's iterating ~187x on average or so.
One possibility I've explored is roughly:
bool p2 = get_rand_bit();
bool p1 = get_rand_bit();
uint64_t r = (p1 << 1) | p2;
for (int i = 2; i < 64; i++) {
bool p0 = (p1 && p2) ? false : get_rand_bit();
r |= p0 << i;
p2 = p1;
p1 = p0;
}
...however, this is still slow. Mainly because using this approach the entire calculation is bit-serial. EDIT: and it's also biased. Easiest to see with a 3-bit integer - 0b011 occurs 1/8th of the time, which is wrong (should be 1/7th).
I've tried doing various parallel fixups, but haven't been able to come up with anything unbiased. It's useful to play around with 4-bit integers first - e.g. setting all bits involved in a conflict to random values ends up biased, and drawing out the Markov chain for 4 bits makes that obvious
Is there a better way to do this?
I optimized the lexicographic decoder, resulting in a four-fold speedup relative to my previous answer. There are two new ideas:
Use the one-to-one correspondence implied by the recurrence T(n) = T(k−1) T(n−k) + T(k−2) T(n−k−1) + T(k−2) T(n−k−2) + T(k−3) T(n−k−1) to avoid working one bit at a time;
Cache the small words without 111 in addition to the recurrence values, incurring an L1 cache hit to save a number of arithmetic operations.
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
enum { kTribonacci14 = 5768 };
static uint64_t g_tribonacci[65];
static void InitTribonacci(void) {
for (unsigned i = 0; i < 65; i++) {
g_tribonacci[i] =
i < 3 ? 1 << i
: g_tribonacci[i - 1] + g_tribonacci[i - 2] + g_tribonacci[i - 3];
}
assert(g_tribonacci[14] == kTribonacci14);
}
static uint16_t g_words_no_111[kTribonacci14];
static void InitCachedWordsNo111(void) {
unsigned i = 0;
for (unsigned word = 0; word < ((unsigned)1 << 14); word++) {
if ((word & (word >> 1) & (word >> 2)) == 0) {
assert(i < kTribonacci14);
g_words_no_111[i++] = (uint16_t)word;
}
}
assert(i == kTribonacci14);
}
static bool CaseNo111(uint64_t *restrict result, unsigned *restrict n,
uint64_t *restrict index, unsigned left_n,
unsigned right_n) {
uint64_t left_count = g_tribonacci[left_n];
uint64_t right_count = g_tribonacci[right_n];
uint64_t product = left_count * right_count;
if (*index >= product) {
*index -= product;
return false;
}
*result = (*result << left_n) + g_words_no_111[*index / right_count];
*n = right_n;
*index %= right_count;
return true;
}
static void Append(uint64_t *result, uint64_t bit) {
*result = (*result << 1) + bit;
}
static uint64_t DecodeNo111(unsigned n, uint64_t index) {
assert(0 <= n && n <= 64);
assert(index < g_tribonacci[n]);
uint64_t result = 0;
while (n > 14) {
assert(g_tribonacci[n] == g_tribonacci[12] * g_tribonacci[n - 13] +
g_tribonacci[11] * g_tribonacci[n - 14] +
g_tribonacci[11] * g_tribonacci[n - 15] +
g_tribonacci[10] * g_tribonacci[n - 14]);
if (CaseNo111(&result, &n, &index, 12, n - 13)) {
Append(&result, 0);
} else if (CaseNo111(&result, &n, &index, 11, n - 14)) {
Append(&result, 0);
Append(&result, 1);
Append(&result, 0);
} else if (CaseNo111(&result, &n, &index, 11, n - 15)) {
Append(&result, 0);
Append(&result, 1);
Append(&result, 1);
Append(&result, 0);
} else if (CaseNo111(&result, &n, &index, 10, n - 14)) {
Append(&result, 0);
Append(&result, 1);
Append(&result, 1);
Append(&result, 0);
} else {
assert(false);
}
}
return (result << n) + g_words_no_111[index];
}
static void PrintWord(unsigned n, uint64_t word) {
assert(0 <= n && n <= 64);
while (n-- > 0) {
putchar('0' + ((word >> n) & 1));
}
putchar('\n');
}
int main(void) {
InitTribonacci();
InitCachedWordsNo111();
if ((false)) {
enum { kN = 20 };
for (uint64_t i = 0; i < g_tribonacci[kN]; i++) {
PrintWord(kN, DecodeNo111(kN, i));
}
}
uint64_t sum = 0;
uint64_t index = 0;
for (uint32_t i = 0; i < 10000000; i++) {
sum += DecodeNo111(64, index % g_tribonacci[64]);
index = (index * 2862933555777941757) + 3037000493;
}
return sum & 127;
}
From #John Coleman's comment, here's the start of an approach based on Tribonacci numbers. Basic idea:
Generate an unbiased number in the range [0..T(bits)), where T(0) = 1, T(1) = 2, T(2) = 4, T(n) = T(n-1) + T(n-2) + T(n-3).
Convert to Tribonacci representation.
You're done.
A minimal example is as follows:
// 1, 2, 4, TRIBO[n-3]+TRIBO[n-2]+TRIBO[n-1]
// possible minor perf optimization: reverse TRIBO
static const uint64_t TRIBO[65] = {1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, 121415, 223317, 410744, 755476, 1389537, 2555757, 4700770, 8646064, 15902591, 29249425, 53798080, 98950096, 181997601, 334745777, 615693474, 1132436852, 2082876103, 3831006429, 7046319384, 12960201916, 23837527729, 43844049029, 80641778674, 148323355432, 272809183135, 501774317241, 922906855808, 1697490356184, 3122171529233, 5742568741225, 10562230626642, 19426970897100, 35731770264967, 65720971788709, 120879712950776, 222332455004452, 408933139743937, 752145307699165, 1383410902447554, 2544489349890656, 4680045560037375, 8607945812375585, 15832480722303616, 29120472094716576, 53560898629395777, 98513851446415969];
// exclusive of max
extern uint64_t get_rand_64_range(uint64_t max);
uint64_t get_rand_no111(void) {
uint64_t idx = get_rand_64_range(TRIBO[64]);
uint64_t ret = 0;
for (int i = 63; i >= 0; i--) {
if (idx >= TRIBO[i]) {
ret |= ((uint64_t) 1) << i;
idx -= TRIBO[i];
}
// optional: if (idx == 0) {break;}
}
return ret;
}
(Warning: retyped from Python code. I suggest testing.)
This satisfies the 'unbiased' portion, and is indeed faster than the naive rejection-sampling approach, but unfortunately is still pretty slow, because it's looping ~64 times.
The idea behind the code below is to generate the upper 32 bits with the proper (non-uniform!) distribution, then generate the lower 32 conditional on the upper. On my laptop, it’s significantly faster than the baseline, and slightly faster than lexicographic decoding.
You can see the logic behind the non-uniform upper distribution with 4-bit outputs: 00 and 10 have four 2-bit lowers, 01 has three lowers, and 11 has two lowers.
#include <cstdint>
#include <random>
namespace {
using Generator = std::mt19937_64;
template <int bits> std::uint64_t GenerateUniform(Generator &gen) {
static_assert(0 <= bits && bits <= 63);
return gen() & ((std::uint64_t{1} << bits) - 1);
}
template <> std::uint64_t GenerateUniform<64>(Generator &gen) { return gen(); }
template <int bits> std::uint64_t GenerateNo111Baseline(Generator &gen) {
std::uint64_t r;
do {
r = GenerateUniform<bits>(gen);
} while (r & (r >> 1) & (r >> 2));
return r;
}
template <int bits> struct Tribonacci {
static constexpr std::uint64_t value = Tribonacci<bits - 1>::value +
Tribonacci<bits - 2>::value +
Tribonacci<bits - 3>::value;
};
template <> struct Tribonacci<0> { static constexpr std::uint64_t value = 1; };
template <> struct Tribonacci<-1> { static constexpr std::uint64_t value = 1; };
template <> struct Tribonacci<-2> { static constexpr std::uint64_t value = 0; };
template <int bits> std::uint64_t GenerateNo111(Generator &gen) {
constexpr int upper_bits = 16;
constexpr int lower_bits = bits - upper_bits;
const std::uint64_t upper = GenerateNo111Baseline<upper_bits>(gen);
for (;;) {
if ((upper & 1) == 0) {
return (upper << lower_bits) + GenerateNo111<lower_bits>(gen);
}
std::uint64_t outcome = std::uniform_int_distribution<std::uint64_t>{
0, Tribonacci<upper_bits>::value - 1}(gen);
if ((upper & 2) == 0) {
if (outcome < Tribonacci<upper_bits - 2>::value) {
return (upper << lower_bits) + (std::uint64_t{1} << (lower_bits - 1)) +
GenerateNo111<lower_bits - 2>(gen);
}
outcome -= Tribonacci<upper_bits - 2>::value;
}
if (outcome < Tribonacci<lower_bits - 1>::value) {
return (upper << lower_bits) + GenerateNo111<lower_bits - 1>(gen);
}
}
}
#define BASELINE(bits) \
template <> std::uint64_t GenerateNo111<bits>(Generator & gen) { \
return GenerateNo111Baseline<bits>(gen); \
}
BASELINE(0)
BASELINE(1)
BASELINE(2)
BASELINE(3)
BASELINE(4)
BASELINE(5)
BASELINE(6)
BASELINE(7)
BASELINE(8)
BASELINE(9)
BASELINE(10)
BASELINE(11)
BASELINE(12)
BASELINE(13)
BASELINE(14)
BASELINE(15)
BASELINE(16)
#undef BASELINE
static const std::uint64_t TRIBO[65] = {1,
2,
4,
7,
13,
24,
44,
81,
149,
274,
504,
927,
1705,
3136,
5768,
10609,
19513,
35890,
66012,
121415,
223317,
410744,
755476,
1389537,
2555757,
4700770,
8646064,
15902591,
29249425,
53798080,
98950096,
181997601,
334745777,
615693474,
1132436852,
2082876103,
3831006429,
7046319384,
12960201916,
23837527729,
43844049029,
80641778674,
148323355432,
272809183135,
501774317241,
922906855808,
1697490356184,
3122171529233,
5742568741225,
10562230626642,
19426970897100,
35731770264967,
65720971788709,
120879712950776,
222332455004452,
408933139743937,
752145307699165,
1383410902447554,
2544489349890656,
4680045560037375,
8607945812375585,
15832480722303616,
29120472094716576,
53560898629395777,
98513851446415969};
std::uint64_t get_rand_no111(Generator &gen) {
std::uint64_t idx =
std::uniform_int_distribution<std::uint64_t>{0, TRIBO[64] - 1}(gen);
std::uint64_t ret = 0;
for (int i = 63; i >= 0; --i) {
if (idx >= TRIBO[i]) {
ret |= std::uint64_t{1} << i;
idx -= TRIBO[i];
}
}
return ret;
}
} // namespace
int main() {
Generator gen{std::random_device{}()};
std::uint64_t sum = 0;
for (std::int32_t i = 0; i < 10000000; i++) {
if constexpr (true) {
sum += GenerateNo111<64>(gen);
} else {
sum += get_rand_no111(gen);
}
}
return sum & 127;
}
What about following simple idea:
Generate random r.
Find within this r window(s)-mask, contains 3 or more sequenced 1s.
If mask is 0 (no 3 or more sequenced bits) - return the r.
Substitute "incorrect" bits under that mask to new random ones.
Goto 2
Code sample (did not tested, compiled only):
uint64_t rand_no3() {
uint64_t r, mask;
for(r = get_rand_64() ; ; ) {
mask = r & (r >> 1) & (r >> 2);
mask |= (mask << 1) | (mask << 2);
if(mask == 0)
return r;
r ^= mask & get_rand_64();
}
}
Another variant of same code, with just single call get_rand_64():
uint64_t rand_no3() {
uint64_t r, mask = ~0ULL;
do {
r ^= mask & get_rand_64();
mask = r & (r >> 1) & (r >> 2);
mask |= (mask << 1) | (mask << 2);
} while(mask != 0);
return r;
}
I know, the last code does not init the r, but it is not matter, because of this variable will be overwritten in 1st loop iteration.
You could generate the number one bit at a time, keeping track of the number of consecutive set bits. Whenever you have two consecutive set bits, you insert an unset bit and set the count back to 0.

Error correction on small message (8-Bit) with high resilience, what is the best method?

I need to implement an ECC algorithm on an 8-bit message with 32 bits to work with (32, 8), being new to ECC I started to google and learn a bit about it and ended up coming across two ECC methods, Hamming codes and Reed Solomon. Given that I needed my message to be resilient to 4-8 random bit flips on average I disregarded Hammings and looked into Reed, however, after applying it to my problem I realized it is also not suitable for my use case because while a whole symbol (8 bits) could be flipped, because my errors tend to spread out (on average), it can usually only fix a single error...
Therefore in the end I just settled for my first instinct which is to just copy the data over like so:
00111010 --> 0000 0000 1111 1111 1111 0000 1111 0000
This way every bit is resilient up to 1 error (8 across all bits) by taking the most prominent bits on each actual bit from the encoded message, and every bit can be subject to two bitflips while still detecting there was an error (which is also usable for my use case, eg: input 45: return [45, 173] is still useful).
My question then is if there is any better method, while I am pretty sure there is, I am not sure where to go from here.
By "better method" I mean resilient to even more errors given the (32, 8) ratio.
You can get a distance-11 code pretty easily using randomization.
#include <stdio.h>
#include <stdlib.h>
int main() {
uint32_t codes[256];
for (int i = 0; i < 256; i++) {
printf("%d\n", i);
retry:
codes[i] = arc4random();
for (int j = 0; j < i; j++) {
if (__builtin_popcount(codes[i] ^ codes[j]) < 11) goto retry;
}
}
}
I made a test program for David Eisenstat's example, to show it works for 1 to 5 bits in error. Code is for Visual Studio.
#include <intrin.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int uint32_t;
/*----------------------------------------------------------------------*/
/* InitCombination - init combination */
/*----------------------------------------------------------------------*/
void InitCombination(int a[], int k, int n) {
for(int i = 0; i < k; i++)
a[i] = i;
--a[k-1];
}
/*----------------------------------------------------------------------*/
/* NextCombination - generate next combination */
/*----------------------------------------------------------------------*/
int NextCombination(int a[], int k, int n) {
int pivot = k - 1;
while (pivot >= 0 && a[pivot] == n - k + pivot)
--pivot;
if (pivot == -1)
return 0;
++a[pivot];
for (int i = pivot + 1; i < k; ++i)
a[i] = a[pivot] + i - pivot;
return 1;
}
/*----------------------------------------------------------------------*/
/* Rnd32 - return pseudo random 32 bit number */
/*----------------------------------------------------------------------*/
uint32_t Rnd32()
{
static uint32_t r = 0;
r = r*1664525+1013904223;
return r;
}
static uint32_t codes[256];
/*----------------------------------------------------------------------*/
/* main - test random hamming distance 11 code */
/*----------------------------------------------------------------------*/
int main() {
int ptn[5]; /* error bit indexes */
int i, j, n;
uint32_t m;
int o, p;
for (i = 0; i < 256; i++) { /* generate table */
retry:
codes[i] = Rnd32();
for (j = 0; j < i; j++) {
if (__popcnt(codes[i] ^ codes[j]) < 11) goto retry;
}
}
for(n = 1; n <= 5; n++){ /* test 1 to 5 bit error patterns */
InitCombination(ptn, n, 32);
while(NextCombination(ptn, n, 32)){
for(i = 0; i < 256; i++){
o = m = codes[i]; /* o = m = coded msg */
for(j = 0; j < n; j++){ /* add errors to m */
m ^= 1<<ptn[j];
}
for(j = 0; j < 256; j++){ /* search for code */
if((p =__popcnt(m ^ codes[j])) <= 5)
break;
}
if(i != j){ /* check for match */
printf("fail %u %u\n", i, j);
goto exit0;
}
}
}
}
exit0:
return 0;
}

Portable efficient alternative to PDEP without using BMI2?

The documentation for the parallel deposit instruction (PDEP) in Intel's Bit Manipulation Instruction Set 2 (BMI2) describes the following serial implementation for the instruction (C-like pseudocode):
U64 _pdep_u64(U64 val, U64 mask) {
U64 res = 0;
for (U64 bb = 1; mask; bb += bb) {
if (val & bb)
res |= mask & -mask;
mask &= mask - 1;
}
return res;
}
See also Intel's pdep insn ref manual entry.
This algorithm is O(n), where n is the number of set bits in mask, which obviously has a worst case of O(k) where k is the total number of bits in mask.
Is a more efficient worst case algorithm possible?
Is it possible to make a faster version that assumes that val has at most one bit set, ie either equals 0 or equals 1<<r for some value of r from 0 to 63?
The second part of the question, about the special case of a 1-bit deposit, requires two steps. In the first step, we need to determine the bit index r of the single 1-bit in val, with a suitable response in case val is zero. This can easily be accomplished via the POSIX function ffs, or if r is known by other means, as alluded to by the asker in comments. In the second step we need to identify bit index i of the r-th 1-bit in mask, if it exists. We can then deposit the r-th bit of val at bit i.
One way of finding the index of the r-th 1-bit in mask is to tally the 1-bits using a classical population count algorithm based on binary partitioning, and record all of the intermediate group-wise bit counts. We then perform a binary search on the recorded bit-count data to identify the position of the desired bit.
The following C-code demonstrates this using 64-bit data. Whether this is actually faster than the iterative method will very much depend on typical values of mask and val.
#include <stdint.h>
/* Find the index of the n-th 1-bit in mask, n >= 0
The index of the least significant bit is 0
Return -1 if there is no such bit
*/
int find_nth_set_bit (uint64_t mask, int n)
{
int t, i = n, r = 0;
const uint64_t m1 = 0x5555555555555555ULL; // even bits
const uint64_t m2 = 0x3333333333333333ULL; // even 2-bit groups
const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // even nibbles
const uint64_t m8 = 0x00ff00ff00ff00ffULL; // even bytes
uint64_t c1 = mask;
uint64_t c2 = c1 - ((c1 >> 1) & m1);
uint64_t c4 = ((c2 >> 2) & m2) + (c2 & m2);
uint64_t c8 = ((c4 >> 4) + c4) & m4;
uint64_t c16 = ((c8 >> 8) + c8) & m8;
uint64_t c32 = (c16 >> 16) + c16;
int c64 = (int)(((c32 >> 32) + c32) & 0x7f);
t = (c32 ) & 0x3f; if (i >= t) { r += 32; i -= t; }
t = (c16>> r) & 0x1f; if (i >= t) { r += 16; i -= t; }
t = (c8 >> r) & 0x0f; if (i >= t) { r += 8; i -= t; }
t = (c4 >> r) & 0x07; if (i >= t) { r += 4; i -= t; }
t = (c2 >> r) & 0x03; if (i >= t) { r += 2; i -= t; }
t = (c1 >> r) & 0x01; if (i >= t) { r += 1; }
if (n >= c64) r = -1;
return r;
}
/* val is either zero or has a single 1-bit.
Return -1 if val is zero, otherwise the index of the 1-bit
The index of the least significant bit is 0
*/
int find_bit_index (uint64_t val)
{
return ffsll (val) - 1;
}
uint64_t deposit_single_bit (uint64_t val, uint64_t mask)
{
uint64_t res = (uint64_t)0;
int r = find_bit_index (val);
if (r >= 0) {
int i = find_nth_set_bit (mask, r);
if (i >= 0) res = (uint64_t)1 << i;
}
return res;
}

Convert channel from 1 to 3 channels (Sobel Filter using webcam Opencv)

I am trying to create edge detection(sobel) filter from grayscale using basic code(own code). Here the code:
int dx [3][3] = { -1 , 0 , 1 ,
-2 , 0 , 2 ,
-1 , 0 , 1};
int dy [3][3] = {1 ,2 ,1 ,
0 ,0 ,0 ,
-1 , -2 , -1};
void h_grayscale( unsigned char* h_in, unsigned char* h_out)
{
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
int index = h_in[i*widthStep + j*channels];
int gray = 0.3*(index)+0.6*(index+1)+0.1*(index+2);
h_out[i*widthStepOutput+j]=gray;
}
}
}
void h_EdgeDetect( unsigned char* h_in, unsigned char* h_out)
{
//int widthStep2 = image_input->widthStep2/sizeof(uchar);
int s;
for (int i=1; i < height-2; i++)
for (int j=1; j < width-2; j++)
{
// apply kernel in X direction
int sum_x=0;
for(int m=-1; m<=1; m++)
for(int n=-1; n<=1; n++)
{
s=h_in[(i+m)*widthStep+(j+channels)+n]; // get the (i,j) pixel value
sum_x+=s*dx[m+1][n+1];
}
// apply kernel in Y direction
int sum_y=0;
for(int m=-1; m<=1; m++)
for(int n=-1; n<=1; n++)
{
s=h_in[(i+m)*widthStep+(j+channels)+n]; // get the (i,j) pixel value
sum_y+=s*dy[m+1][n+1];
}
int sum=abs(sum_x)+abs(sum_y);
if (sum>255)
sum=255;
h_out[i*widthStepOutput + j+channels]=sum; // set the (i,j) pixel value
}
}
main.cpp
int main(int argc, char** argv)
{
starttime = getTickCount();
int c;
CvCapture* capture = cvCaptureFromCAM(1);
while(1)
{
image_input=cvQueryFrame(capture);
channels = 1;
IplImage* image_output = cvCreateImage(cvGetSize(image_input),IPL_DEPTH_8U,channels);
unsigned char *h_out = (unsigned char*)image_output->imageData;
unsigned char *h_in = (unsigned char*)image_input->imageData;
width = image_input->width;
height = image_input->height;
widthStep = image_input->widthStep;
widthStepOutput = image_output->widthStep;
//grayscale operation
h_grayscale(h_in , h_out );
//Edge Detection
h_EdgeDetect ( h_in , h_out ) ;
cvShowImage("Original", image_input);
cvShowImage("CPU", image_output);
c=cvWaitKey(10);
if(c == 27)
break;
}
return 0;
}
But the result from webcame like this
the problem is the image becomes large when Sobel filter operation. Possibly because of the different channels when the grayscale filter and a Sobel filter. How to change the channels 1 to 3 (RGB)?? T_T
Thx before :)

Counting with an Integer Divide-based routine - Is there a formulaic approach?

Consider a routine that counts by successive divide w/ remainder operations.
Starting with a 64-bit dividend, the routine divides by a constant divisor.
If the remainder is 0, the routine returns.
Otherwise, a new dividend is constructed by multiplying the remainder by 2^32 and adding the integer quotient.
In code:
/// ULong - 64 bit, unsigned
/// UInt - 32 bit, unsigned
const UInt Divisor;
int TrickyCounter( ULong Dividend)
{
int count = 0;
Ulong Quotient;
UInt Remainder;
do {
Quotient = Dividend/Divisor;
Remainder = Dividend%Divisor;
assert((Quotient >> 32) == 0);
count = count + 1;
Dividend = ((ULong)Remainder << 32) + Quotient;
} while (Remainder != 0);
return count;
}
With an arbitrary Divisor, is there a preferably non-iterating method to calculate the necessary Dividend to get the desired count?
For many initial dividends, this seems to quickly hit the "Assert" condition. Would some dividends cause this to loop forever?
If, instead of a count, the routine returns the quotient, can I calculate the Dividend to produce the number I want returned?
Uint TrickyNumber( ULong Dividend, int count)
{
Ulong Quotient = 0;
UInt Remainder;
while (count > 0)
Quotient = Dividend/Divisor;
Remainder = Dividend%Divisor;
assert((Quotient >> 32) == 0);
count = count - 1;
Dividend = ((ULong)Remainder << 32) + Quotient;
}
return (UInt)Quotient;
}
Would some dividends cause this to loop forever?
Dividend = 0x1ffffffffL, Divisor = 2 is a fairly obvious example, and the whole family (Divisor<<32)-1, Divisor are fixed points.
Working from these, many cyclic combinations of initial dividend and divisor can be found, and I'm sure there are more:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
size_t tricky_counter( uint64_t dividend, const uint32_t divisor )
{
const size_t cycle_buffer_size = 1024;
size_t count = 0;
uint64_t quotient;
uint32_t remainder;
uint64_t pre[cycle_buffer_size];
do {
pre[ count % cycle_buffer_size ] = dividend;
quotient = dividend/divisor;
remainder = dividend%divisor;
if ( (quotient >> 32) != 0) {
printf("quotient: 0x%" PRIx64 "\n", quotient);
}
count = count + 1;
dividend = ((uint64_t)remainder << 32) + quotient;
for (size_t i = 0; i < count && i<cycle_buffer_size;++i) {
if (pre[i] == dividend) {
size_t cycle = 0;
printf("dividend repeats: \n");
while (i != count % cycle_buffer_size) {
//~ printf(" 0x%" PRIx64 " / %" PRId32 " \n", pre[i], divisor);
i = (i + 1) % cycle_buffer_size;
++cycle;
}
printf(" 0x%" PRIx64 " / %" PRId32 " cycle size %zd \n", dividend, divisor, cycle);
return 0;
}
}
} while (remainder != 0);
return count;
}
int main ( void )
{
for (uint64_t k = 1; k < 256; ++k)
for (uint64_t x = 2; x < 1024; ++x)
tricky_counter( (x-1 << 32) + 0x01010101L * k, x);
}

Resources