-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_binary_pal.h
90 lines (64 loc) · 2.6 KB
/
is_binary_pal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef IS_BINARY_PAL_H
#define IS_BINARY_PAL_H
// we put the code in the header so that it can be inlined without much fuss.
#define PTR(x) ((x)->_mp_d)
#define MPN_SIZEINBASE_LITE(result, ptr, size) \
{ \
int __cnt; \
/* Calculate the total number of significant bits of X. */ \
(__cnt) = __builtin_clzll ((uint64_t*)(ptr))[(size)-1]; \
(result) = (size_t) (size) * GMP_NUMB_BITS - (__cnt); \
}
bool mpz_tstbit_lite ( mpz_srcptr u, unsigned bit_index ) __GMP_NOTHROW {
mp_srcptr p = PTR ( u ) + ( bit_index >> 6 );
return ( *p >> ( bit_index & 0x3F ) ) & 1;
}
int isBinaryPalandrome ( mpz_t val ) {
unsigned size = (val->_mp_size << 6) - __builtin_clzll( ((uint64_t*)val->_mp_d)[(val->_mp_size) - 1]);
if ( ! ( size & 1 ) ) return -1;
--size; // now points to last.
const unsigned halfsize = size >> 1;
for ( int i = 1; i < halfsize; i++ ) { // yes, I intentionally started i at 1, got a problem with that, punk?
if ( mpz_tstbit_lite ( val, i ) != mpz_tstbit_lite ( val, size - i ) )
return halfsize - i;
}
#pragma omp critical (output)
{
gmp_printf ( "\n%Zd\n", val );
char bfr[1024];
mpz_get_str ( bfr, 2, val );
printf ( "%s\n", bfr );
mpz_get_str ( bfr, 3, val );
printf ( "%s\n", bfr );
// lets use the opportunity to print some statistics too:
++size; // restore size to normal.
unsigned bits = mpz_popcount ( val );
gmp_printf ( "size: %i popcnt: %i\n\n", size, bits );
}
return 0;
}
int isBinaryPalandrome_lite ( mpz_t val, unsigned size) {
/* unsigned size = (val->_mp_size << 6) - __builtin_clzll( ((uint64_t*)val->_mp_d)[(val->_mp_size) - 1]);
if ( ! ( size & 1 ) ) return -1; */
--size; // now points to last.
const unsigned halfsize = size >> 1;
for ( int i = 1; i < halfsize; i++ ) { // yes, I intentionally started i at 1, got a problem with that, punk?
if ( mpz_tstbit_lite ( val, i ) != mpz_tstbit_lite ( val, size - i ) )
return halfsize - i;
}
#pragma omp critical (output)
{
gmp_printf ( "\n%Zd\n", val );
char bfr[1024];
mpz_get_str ( bfr, 2, val );
printf ( "%s\n", bfr );
mpz_get_str ( bfr, 3, val );
printf ( "%s\n", bfr );
// lets use the opportunity to print some statistics too:
++size; // restore size to normal.
unsigned bits = mpz_popcount ( val );
gmp_printf ( "size: %i popcnt: %i\n\n", size, bits );
}
return 0;
}
#endif