Here’s my code on C.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <ctype.h>
#define m_VALUES 16
#define M_VALUE 1
#define T_EXP_VALUE 3
#define SIZE_BIT 32
#define G_VALUES (int64)pow((double)2, (double)22)
#define NUM_ITERATIONS (int64)pow((double)2, (double)14)
typedef unsigned long long int64;
int64 MontgExp(int64 base, int64 exp, int64 mod);
int64 decimal_to_binary(int64);
void extended_euclid(int64 a, int64 b, int64 *x, int64 *y, int64 *d);
int64 contains(int64 num, int64 *arr, int64 size);
int64 main()
{
int64 bigPrimeNumber = 7;
int64 i = 0, j = 0;
int64 bArr[m_VALUES];
int64 T = (int64)pow((double)2, (double)T_EXP_VALUE);
int64 *dArr, *cArr;
int64 M = 0;
int64 *gArr;
int64 R = 0;
int64 tmp = 0;
int k = 0;
srand(time(NULL));
memset(bArr, 0, m_VALUES * sizeof(int64));
dArr = (int64*)malloc(T * sizeof(int64));
memset(dArr, 0, T * sizeof(int64));
cArr = (int64*)malloc(T * sizeof(int64));
memset(cArr, 0, T * sizeof(int64));
gArr = (int64*)malloc(G_VALUES * sizeof(int64));
memset(gArr, 0, G_VALUES * sizeof(int64));
for (i = 0; i < m_VALUES; i++)
bArr[i] = MontgExp(2, rand(), bigPrimeNumber);
for (i = 0; i < T; i++) {
dArr[i] = MontgExp(2, rand(), bigPrimeNumber);
cArr[i] = bArr[i];
}
for (i = 0; i < T; i++) {
M = 0;
while (M != M_VALUE) {
cArr[i] = (bArr[decimal_to_binary(cArr[i])] * cArr[i]) % bigPrimeNumber;
M++;
}
}
srand(time(NULL));
for (i = 0; i < G_VALUES; i++) {
tmp = rand();
if (tmp != 0) {
gArr[i] = rand();
}
}
tmp = 0;
for (i = 0; i < G_VALUES; i++) {
k++;
if (k >= 52428) {
k = 0;
printf("%c", 219);
fflush(stdout);
}
tmp = (bArr[decimal_to_binary(gArr[i])] * gArr[i]) % bigPrimeNumber;
if (contains(tmp, cArr, T) == 1) {
R++;
continue;
}
for (j = 1; j < NUM_ITERATIONS; j++) {
tmp = (bArr[decimal_to_binary(tmp)] * tmp) % bigPrimeNumber;
if (contains(tmp, cArr, T) == 1) {
R++;
break;
}
}
}
printf ("Num of R-points is - %d", R);
printf("Press any key...");
getchar();
return 0;
}
int64 MontgExp(int64 base, int64 exp, int64 mod)
{
int64 z = 0;
if (exp == 0) return 1;
z = MontgExp(base, exp / 2, mod);
if (exp % 2 == 0)
return (z*z) % mod;
else
return (base*z*z) % mod;
}
int64 decimal_to_binary(int64 n)
{
int c = 0, d = 0, count;
char *pointer, charLSB[5];
int64 LSB = 0, i = 0;
count = 0;
pointer = (char*)malloc(SIZE_BIT+1);
memset(pointer, 0, (SIZE_BIT + 1) * sizeof(char));
if ( pointer == NULL )
exit(EXIT_FAILURE);
memset(charLSB, 0, 5 * sizeof(char));
for ( c = SIZE_BIT - 1 ; c >= 0 ; c-- ) {
d = n >> c;
if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';
count++;
}
*(pointer+count) = '\0';
strncpy(charLSB, pointer + (32 - 4), 4);
charLSB[4] = '\0';
for (i = 0; i < 5; i++) {
if (charLSB[i] == '0')
LSB *= 2;
if (charLSB[i] == '1')
LSB = 2 * LSB + 1;
}
free(pointer);
return LSB;
}
void extended_euclid(int64 a, int64 b, int64 *x, int64 *y, int64 *d)
{
int64 q = 0, r = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0;
if (b == 0) {
*d = a, *x = 1, *y = 0;
return;
}
x2 = 1, x1 = 0, y2 = 0, y1 = 1;
while (b > 0) {
q = a / b, r = a - q * b;
*x = x2 - q * x1, *y = y2 - q * y1;
a = b, b = r;
x2 = x1, x1 = *x, y2 = y1, y1 = *y;
}
*d = a, *x = x2, *y = y2;
}
int64 contains(int64 num, int64 *arr, int64 size)
{
int64 i = 0, result = 0;
for (i = 0; i < size; i++)
if (*(arr + i) == num) {
result = 1;
break;
}
return result;
}
If i create VS C porject, it is compiled very well, but if i create CUDA project, there for about 20 error in functions. For example, one of the errors is connected with using strncpy (it is undefined). Help me, please, to understand, why my code is not compiled.