Bit manipulation - replace a "block" - bit-manipulation

I'm trying to write a function that will replace a certain "block" of size 2^x with a new value.
For example, if I had a number 1110 1000 0010 and I wanted to replace block 2 (max block size of 2^4) with 0110, I would get 0110 1000 0010. Likewise, If I wanted the to replace block 2 with 110 (max block size of 2^3) I would get 111 110 000 010, or 1111 1000 0010.
replace_block (value, x, blockNumber, newValue) {
value |= ((value >> (blockNumber*x) & ~((1 << x) – 1)) | newValue) << (blockNumber*x)
}
Step by step process of what I'm trying to do with this code:
1. Shift the block we want all the way to the right
2. Mask that block with 0's
3. Mask that block again, but with the new value we want
4. Shift the block all the way back to the original position
5. Or the bits in the block with the original value
This is what I have so far but I don't think it's correct.
Note: The rightmost block is block 0.

First we need the mask: The mask is a set of ones "block size" wide shifted to the right block.
set of ones "block size" wide: (1<<size) - 1
set of ones "block size" wide shifted to the right place: ((1<<size)-1) << (number*size)
First we clear the bits in the spot that will be replaced then we stick in the bits we want.
old value with the masked bits cleared: oldv & ~mask
new value shifted to right place: newv << (number*size)
new value shifted to right place and masked to right width: (newv<<(number*size)) & mask
old bits cleared and new value inserted: (oldv&~mask) | ((newv<<(number*size))&mask)
So the bit of code you want is:
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
A test program:
#include <stdio.h>
void printbits(int n) {
unsigned int i = 1<<(sizeof(n) * 8 - 1);
while (i > 0) {
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
int main(void) {
int size,number,mask,oldv,newv,result;
size=4;number=2;oldv=0;newv=15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=4;number=2;oldv=~0;newv=0;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=3;number=2;oldv=0;newv=7;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=3;number=2;oldv=~0;newv=0;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=4;number=4;oldv=0xAAAAAAAA;newv=0x15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
size=5;number=3;oldv=0xAAAAAAAA;newv=0x15;
mask=(((1<<size)-1)<<(number*size));
result=((oldv&~mask)|((newv<<(number*size))&mask));
printf("\nsize = %d\nnumber = %d",size,number);
printf("\noldv = "); printbits(oldv);
printf("\nnewv = "); printbits(newv);
printf("\nmask = "); printbits(mask);
printf("\nomask = "); printbits(oldv&~mask);
printf("\nnmask = "); printbits((newv<<(number*size))&mask);
printf("\nresult = "); printbits(result);
printf("\n");
return 0;
}
And the result:
size = 4
number = 2
oldv = 00000000000000000000000000000000
newv = 00000000000000000000000000001111
mask = 00000000000000000000111100000000
omask = 00000000000000000000000000000000
nmask = 00000000000000000000111100000000
result = 00000000000000000000111100000000
size = 4
number = 2
oldv = 11111111111111111111111111111111
newv = 00000000000000000000000000000000
mask = 00000000000000000000111100000000
omask = 11111111111111111111000011111111
nmask = 00000000000000000000000000000000
result = 11111111111111111111000011111111
size = 3
number = 2
oldv = 00000000000000000000000000000000
newv = 00000000000000000000000000000111
mask = 00000000000000000000000111000000
omask = 00000000000000000000000000000000
nmask = 00000000000000000000000111000000
result = 00000000000000000000000111000000
size = 3
number = 2
oldv = 11111111111111111111111111111111
newv = 00000000000000000000000000000000
mask = 00000000000000000000000111000000
omask = 11111111111111111111111000111111
nmask = 00000000000000000000000000000000
result = 11111111111111111111111000111111
size = 4
number = 4
oldv = 10101010101010101010101010101010
newv = 00000000000000000000000000010101
mask = 00000000000011110000000000000000
omask = 10101010101000001010101010101010
nmask = 00000000000001010000000000000000
result = 10101010101001011010101010101010
size = 5
number = 3
oldv = 10101010101010101010101010101010
newv = 00000000000000000000000000010101
mask = 00000000000011111000000000000000
omask = 10101010101000000010101010101010
nmask = 00000000000010101000000000000000
result = 10101010101010101010101010101010

Related

Function that reverse one part(half) of integer

I want to write a function to reverse one of two parts of number :
Input is: num = 1234567; part = 2
and output is: 1234765
So here is part that can be only 1 or 2
Now I know how to get part 1
int firstPartOfInt(int num) {
int ret = num;
digits = 1, halfDig = 10;
while (num > 9) {
ret = ret / 10;
digits++;
}
halfDigits = digits / 2;
for (int i = 1; i < halfDigits; i++) {
halfDigits *= 10;
}
ret = num;
while (num > halfDigits) {
ret = ret / 10;
}
return ret;
}
But I don't know how to get part 2 and reverse the number. If you post code here please do not use vector<> and other C++ feature not compatible with C
One way is to calculate the total number of digits in the number and then calculate a new number extracting digits from the original number in a certain order, complexity O(number-of-digits):
#include <stdio.h>
#include <stdlib.h>
unsigned reverse_decimal_half(unsigned n, unsigned half) {
unsigned char digits[sizeof(n) * 3];
unsigned digits10 = 0;
do digits[digits10++] = n % 10;
while(n /= 10);
unsigned result = 0;
switch(half) {
case 1:
for(unsigned digit = digits10 / 2; digit < digits10; ++digit)
result = result * 10 + digits[digit];
for(unsigned digit = digits10 / 2; digit--;)
result = result * 10 + digits[digit];
break;
case 2:
for(unsigned digit = digits10; digit-- > digits10 / 2;)
result = result * 10 + digits[digit];
for(unsigned digit = 0; digit < digits10 / 2; ++digit)
result = result * 10 + digits[digit];
break;
default:
abort();
}
return result;
}
int main() {
printf("%u %u %u\n", 0, 1, reverse_decimal_half(0, 1));
printf("%u %u %u\n", 12345678, 1, reverse_decimal_half(12345678, 1));
printf("%u %u %u\n", 12345678, 2, reverse_decimal_half(12345678, 2));
printf("%u %u %u\n", 123456789, 1, reverse_decimal_half(123456789, 1));
printf("%u %u %u\n", 123456789, 2, reverse_decimal_half(123456789, 2));
}
Outputs:
0 1 0
12345678 1 43215678
12345678 2 12348765
123456789 1 543216789
123456789 2 123459876
if understand this question well you need to reverse half of the decimal number. If the number has odd number of digits I assume that the first part is longer (for example 12345 - the first part is 123 the second 45). Because reverse is artihmetic the reverse the part 1 of 52001234 is 521234.
https://godbolt.org/z/frXvCM
(some numbers when reversed may wrap around - it is not checked)
int getndigits(unsigned number)
{
int ndigits = 0;
while(number)
{
ndigits++;
number /= 10;
}
return ndigits;
}
unsigned reverse(unsigned val, int ndigits)
{
unsigned left = 1, right = 1, result = 0;
while(--ndigits) left *= 10;
while(left)
{
result += (val / left) * right;
right *= 10;
val = val % left;
left /= 10;
}
return result;
}
unsigned reversehalf(unsigned val, int part)
{
int ndigits = getndigits(val);
unsigned parts[2], digits[2], left = 1;
if(ndigits < 3 || (ndigits == 3 && part == 2))
{
return val;
}
digits[0] = digits[1] = ndigits / 2;
if(digits[0] + digits[1] < ndigits) digits[0]++;
for(int dig = 0; dig < digits[1]; dig++) left *= 10;
parts[0] = val / left;
parts[1] = val % left;
parts[part - 1] = reverse(parts[part - 1], digits[part - 1]);
val = parts[0] * left + parts[1];
return val;
}
int main()
{
for(int number = 0; number < 40; number++)
{
unsigned num = rand();
printf("%u \tpart:%d\trev:%u\n", num,(number & 1) + 1,reversehalf(num, (number & 1) + 1));
}
}
My five cents.:)
#include <iostream>
int reverse_part_of_integer( int value, bool first_part = false )
{
const int Base = 10;
size_t n = 0;
int tmp = value;
do
{
++n;
} while ( tmp /= Base );
if ( first_part && n - n / 2 > 1 || !first_part && n / 2 > 1 )
{
n = n / 2;
int divider = 1;
while ( n-- ) divider *= Base;
int first_half = value / divider;
int second_half = value % divider;
int tmp = first_part ? first_half : second_half;
value = 0;
do
{
value = Base * value + tmp % Base;
} while ( tmp /= Base );
value = first_part ? value * divider + second_half
: first_half * divider +value;
}
return value;
}
int main()
{
int value = -123456789;
std::cout << "initial value: "
<< value << '\n';
std::cout << "First part reversed: "
<< reverse_part_of_integer( value, true ) << '\n';
std::cout << "Second part reversed: "
<< reverse_part_of_integer( value ) << '\n';
}
The program output is
initial value: -123456789
First part reversed: -543216789
Second part reversed: -123459876
Just for fun, a solution that counts only half the number of digits before reversing:
constexpr int base{10};
constexpr int partial_reverse(int number, int part)
{
// Split the number finding its "halfway"
int multiplier = base;
int abs_number = number < 0 ? -number : number;
int parts[2] = {0, abs_number};
while (parts[1] >= multiplier)
{
multiplier *= base;
parts[1] /= base;
}
multiplier /= base;
parts[0] = abs_number % multiplier;
// Now reverse only one of the two parts
int tmp = parts[part];
parts[part] = 0;
while (tmp)
{
parts[part] = parts[part] * base + tmp % base;
tmp /= base;
}
// Then rebuild the number
int reversed = parts[0] + multiplier * parts[1];
return number < 0 ? -reversed : reversed;
}
int main()
{
static_assert(partial_reverse(123, 0) == 123);
static_assert(partial_reverse(-123, 1) == -213);
static_assert(partial_reverse(1000, 0) == 1000);
static_assert(partial_reverse(1009, 1) == 109);
static_assert(partial_reverse(123456, 0) == 123654);
static_assert(partial_reverse(1234567, 0) == 1234765);
static_assert(partial_reverse(-1234567, 1) == -4321567);
}

How to generate nested tree of offsets based on index permutation C++

I got the following offsets which reset and go to the next offset if offset value hits 0xC. The last offset is always 0xC.
How do I generate the nested tree of offsets based on index.
The pattern goes like this
0,C = index 0
4,C = index 1
8,C = index 2
0,0,C = index 3
0,4,C = index 4
0,8,C = index 5
4,0,C = index 6
4,4,C = index 7
4,8,C = index 8
8,0,C = index 9
8,4,C = index 10
8,8,C = index 11
0,0,0,C = index 12
and so on and so on.
#include <stdio.h>
int main() {
int offset_0 = 0;
int offset_1 = 0;
int offset_2 = 0;
int offset_3 = 0;
int offset_4 = 0;
bool offset_0_activate = true;
bool offset_1_activate = false;
bool offset_2_activate = false;
bool offset_3_activate = false;
int index = 100;
for (int i = 0; i < index; i++) {
offset_0 += 4;
if (offset_0 == 0xC) {
offset_0 = 0;
offset_1 += 4;
}
if (offset_1 == 0xC) {
offset_1 = 0;
offset_2 += 4;
}
if (offset_2 == 0xC) {
offset_2 = 0;
offset_3 += 4;
}
if (offset_3 == 0xC) {
break;
}
printf("index = %d offset0 = %d offset1 = %d offset2 = %d offset3 = %d\n", i, offset_0, offset_1, offset_2, offset_3);
}
}

visual studio access violation reading location 0xc0000005

I am receiving data in TCP in C++ using Qt library. I store the received packet in a QByteArray, but after reading the whole data, I face this error in debug. At the end, I try to clear the buffer, but I face this problem while trying to clear it too.
Here is my code :
void AvaNPortTester::scoket_readyRead()
{
ui.lineEdit_Sending_Status_->setText("Sent");
ui.lineEdit_Sending_Status_->setStyleSheet("QLineEdit { background: rgb(50, 255, 50); }");
tcpSocket_data_buffer_.append(tcpSocket_->readAll());
//qDebug() << serialport_data_buffer_.size();
//auto ddd = QString::number(tcpSocket_data_buffer_.size());// +" : " + tcpSocket_data_buffer_.toHex();
//ui.lableSocketRead->setText(ddd);
bool read_aain = false;
QByteArray dummy(int(1446), Qt::Initialization::Uninitialized);
int reminded_data = 0;
int dummy_size = 0;
int frame_size = 0;
int l_size = 0;
int total_size_rcvd = tcpSocket_data_buffer_.size();
//int total_size_rcvd_b = total_size_rcvd_b;
int temp = 0;
while (total_size_rcvd != 0)
{
if(total_size_rcvd != 0){
auto packet = tcpSocket_data_buffer_.mid(0, 1446);
auto rem = tcpSocket_data_buffer_.mid(1446);//****1146
tcpSocket_data_buffer_ = rem;
QDataStream streamdata(packet);
uint8_t Sync_Header[3];
auto ss = streamdata.readRawData((char*)&Sync_Header, 3);
uint8_t Total_size[2];
ss = streamdata.readRawData((char*)&Total_size, 2);
int t_size = Total_size[0] * 256 + Total_size[1];
uint8_t Reserved[2];
ss = streamdata.readRawData((char*)&Reserved, 2);
frame_size = t_size - 2;
reminded_data = t_size - 2;
while (frame_size != 0)
{
uint8_t portid;
ss = streamdata.readRawData((char*)&portid, 1);
//ui.lineEdit_FileSize->setText(QString::number(fileSend_2Ser->size()));
uint8_t ProtocolID;
ss = streamdata.readRawData((char*)&ProtocolID, 1);
uint8_t MoreFragmentFlag;
ss = streamdata.readRawData((char*)&MoreFragmentFlag, 1);
uint8_t Seq;
ss = streamdata.readRawData((char*)&Seq, 1);
uint8_t size[2];
ss = streamdata.readRawData((char*)&size, 2);
l_size = size[0] * 256 + size[1];
if (packet_flags.Ser2Eth.packet_started[portid] == false) {
uint8_t DDCMP_Header[14];
ss = streamdata.readRawData((char*)&DDCMP_Header, 14);
packet_flags.Ser2Eth.protocol_payload_size[portid] = DDCMP_Header[7] + 256 * DDCMP_Header[8];
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
packet_flags.Ser2Eth.packet_started[portid] = true;
}
QByteArray ddcmp_datap(int(l_size), Qt::Initialization::Uninitialized);
streamdata.readRawData(ddcmp_datap.data(), l_size - 14);
if ((pre_more_frag == 0) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 0) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = l_size + 16;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 1)) {
packet_flags.Ser2Eth.packet_ended[portid] = false;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
else if ((pre_more_frag == 1) && (MoreFragmentFlag == 0)) {
packet_flags.Ser2Eth.packet_ended[portid] = true;
packet_flags.Ser2Eth.protocol_payload_size[portid] = packet_flags.Ser2Eth.protocol_payload_size[portid] + l_size;
temp = packet_flags.Ser2Eth.protocol_payload_size[portid];
}
if (MoreFragmentFlag == 1) {
pre_more_frag = 1;
}
else {
pre_more_frag = 0;
}
int ff = 0;
if (packet_flags.Ser2Eth.packet_ended[portid] == true) {
packet_flags.Ser2Eth.packet_started[portid] = false;
packet_flags.Ser2Eth.packet_started[portid] = false;
set_port_id_flag(portid, packet_flags.Ser2Eth.protocol_payload_size[portid], ProtocolID);
pre_more_frag = 0;
}
reminded_data = reminded_data - 6 - l_size;
//ui.lableSocketRead->setText(ddcmp_datap.toHex());
frame_size = frame_size - l_size - 6;
}//end of while (frame_size != 0)
uint8_t sync_footer[3];
streamdata.readRawData((char *)&sync_footer, 3);
dummy_size = 1446 - t_size - 8;
uint8_t dummy_data[1000];
streamdata.readRawData((char *)&dummy_data, dummy_size);
total_size_rcvd = total_size_rcvd - 1446;
if (total_size_rcvd == 0) {
tcpSocket_data_buffer_.clear();
}
} //end of if
}//end of while()
}

Fill struct in6_addr with unsigned char array

I'am doing DNS lookup tool in C++ and i am trying to get IPv6 from
unsigned char * (where it is stored in non readable format), copy it to struct in6_addr, then i want to convert it and print it.
struct in6_addr tmp2;
char buf[41];
memcpy(tmp2.s6_addr, answ[i].rdata, 128);
cout << answ[i].name << " IN AAAA " << inet_ntop(AF_INET6, tmp2.s6_addr, buf, 128) << endl;
My output should look like this,
www.domain.name.cz. IN AAAA 2001:67c:1220:809::93e5:917
but somehow it looks like this.
www.domain.name.cz IN AAAA 106:7c12:2008:900::
Generating RDATA
u_char *ReadName(unsigned char *readResponse, unsigned char *buffer, int *count) {
unsigned char *name;
unsigned int p = 0, jumped = 0, offset;
int i, j;
*count = 1;
name = (unsigned char *) malloc(256);
name[0] = '\0';
//read the names in 3www6google3com format
while (*readResponse != 0) {
if (*readResponse >= 192) {
offset = (*readResponse) * 256 + *(readResponse + 1) - 49152; //49152 = 11000000 00000000 ;)
readResponse = buffer + offset - 1;
jumped = 1; //we have jumped to another location so counting wont go up!
} else {
name[p++] = *readResponse;
}
readResponse = readResponse + 1;
if (jumped == 0) {
*count = *count + 1; //if we havent jumped to another location then we can count up
}
}
name[p] = '\0'; //string complete
if (jumped == 1) {
*count = *count + 1; //number of steps we actually moved forward in the packet
}
//now convert 3www6google3com0 to www.google.com
for (i = 0; i < (int) strlen((const char *) name); i++) {
p = name[i];
for (j = 0; j < (int) p; j++) {
name[i] = name[i + 1];
i = i + 1;
}
name[i] = '.';
}
name[i - 1] = '\0'; //remove the last dot
return name;
Thanks for your help!

What's wrong with this parallel algorithm?

I'm trying to write parallel algorithm in openCL for L-system Pythagoras Tree :
var:A,B;
const: (,);
axiom:A;
rules:(B->BB),(A->B[A]A)
But i can't get over 9th iteration. 10th iteration returns disordered string. Here is my kernel:
#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
#pragma OPENCL EXTENSION cl_amd_printf : enable
__kernel void l_system(int string_lenght){}
__kernel void l_system_interation(int string_lenght, __global char *sentence, __local char *string, __global int * local_char_num)
{
int local_x = (int)get_local_id(0);
int local_size = (int)get_local_size(0);
int x = (int)get_global_id(0);
int size = (int)get_global_size(0);
int group = (int)get_group_id(0);
int local_mem_index;
if(x < string_lenght){
//local mem index - offset for next group, copy char to local
local_mem_index = local_x * 5;
string[local_mem_index] = sentence[x];
if(local_x == 0){
//reset counter
atomic_xchg(&local_char_num[group], 0);
//atomic_add(&local_char_num[0], group);
}
}
barrier(CLK_LOCAL_MEM_FENCE);
barrier(CLK_GLOBAL_MEM_FENCE);
if(x < string_lenght){
if(string[local_mem_index] == 'A'){
atomic_add(&local_char_num[group], 5);
string[local_mem_index] = 'B';
string[local_mem_index + 1] = '(';
string[local_mem_index + 2] = 'A';
string[local_mem_index + 3] = ')';
string[local_mem_index + 4] = 'A';
}
else if(string[local_mem_index] == 'B'){
atomic_add(&local_char_num[group], 2);
string[local_mem_index + 1] = 'B';
//reset 3rd char of local_mem
string[local_mem_index + 2] = '0';
}
else{
atomic_add(&local_char_num[group], 1);
//reset 3rd char of local_mem
string[local_mem_index + 2] = '0';
string[local_mem_index + 2] = '0';
}
}
barrier(CLK_LOCAL_MEM_FENCE);
barrier(CLK_GLOBAL_MEM_FENCE);
//1 compute unit for every char from src
if(x < string_lenght){
//local first compute unit writes to result whole group string
if(local_x == 0){
int j = 0;
//find offset for write to result string
if(x != 0){
for(int l = 1;l <= group; l++)
{
j += atomic_xchg(&local_char_num[group-l], local_char_num[group-l]);
//if(l == 0)
}
atomic_xchg(&local_char_num[99+group], local_char_num[group]);
}
for(int i = 0; i < local_size; i++){
//only valid chars
if(string_lenght > (x+i)){
local_mem_index = i * 5;
//B rule, copy (,)
if(string[local_mem_index+2] != 'A'){
sentence[j++] = string[local_mem_index];
if(string[local_mem_index] == 'B'){
sentence[j++] = string[local_mem_index+1];
}
continue;//B,(,); next index;
}
else{ // A rule
sentence[j++] = string[local_mem_index];
sentence[j++] = string[local_mem_index+1];
sentence[j++] = string[local_mem_index+2];
sentence[j++] = string[local_mem_index+3];
sentence[j++] = string[local_mem_index+4];
}//if 'A'
//sentence[j] = 0;
}//if x+i
}//for
}// lx == 0
}
barrier(CLK_GLOBAL_MEM_FENCE);
}
I think, that something overflow anywhere, but can't find where... Maybe there is something wrong with my code in main:
cl_int letter_count = 0;
cl_int next_letter_count = 1;
for (int i = 0; i < iter_count; i++)
{
//printf("%s\n", sentence_init);
letter_count = next_letter_count;
next_letter_count = STRING_LENGTH_PAR((i + 1));
printf("in count: %d out count: %d\n", letter_count, next_letter_count);
CheckOpenCLError(clSetKernelArg(kernel_iteration, 0, sizeof(cl_int), &letter_count), "clSetKernelArg: letter_count");
CheckOpenCLError(clSetKernelArg(kernel_iteration, 2, sizeof(cl_char)* (local * RULE_SIZE + 1), NULL), "clSetKernelArg: tmp_string");
CheckOpenCLError(clEnqueueNDRangeKernel(queue, kernel_iteration, 1, NULL, &global, &local, 0, NULL, &kernel_iteration_event), "clEnqueueNDRangeKernel: kernel_iteration");
CheckOpenCLError(clFinish(queue), "clFinish");
kernel_computing_time += getEventTime(kernel_iteration_event);
}
CheckOpenCLError(clEnqueueReadBuffer(queue, sentence_dev, CL_TRUE, 0, sizeof(cl_char)* (next_letter_count), sentence_result, 0, NULL, &result_iteration_event), "clEnqueueReadBuffer: result_iteration_event");
cl_int *p = (cl_int*)malloc(sizeof(cl_int)*(STRING_LENGTH_PAR(iter_count)));
CheckOpenCLError(clEnqueueReadBuffer(queue, p_dev, CL_TRUE, 0, sizeof(cl_int)* (STRING_LENGTH_PAR(iter_count)), p, 0, NULL, &result_iteration_event), "clEnqueueReadBuffer: result_iteration_event");