semigroup.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #ifndef SEMIGROUP_HPP
  2. #define SEMIGROUP_HPP
  3. #include <cstdint>
  4. #include <fstream>
  5. // We cant have those as C++ constant because of the #define used below in
  6. // remove_generator manual loop unrolling. I don't know if the unrolling is
  7. // doable using template metaprogamming. I didn't manage to figure out how.
  8. #ifndef MAX_GENUS
  9. #error "Please define the MAX_GENUS macro"
  10. #endif
  11. #define SIZE_BOUND (3*(MAX_GENUS-1))
  12. #define NBLOCKS ((SIZE_BOUND+15) >> 4)
  13. #define SIZE (NBLOCKS << 4)
  14. // const uint_fast8_t MAX_GENUS = 40;
  15. // const uint_fast8_t SIZE_BOUND = (3*(MAX_GENUS-1));
  16. // const uint_fast8_t NBLOCKS = ((SIZE_BOUND+15) >> 4);
  17. // const uint_fast8_t SIZE = (NBLOCKS << 4);
  18. #include <x86intrin.h>
  19. typedef uint8_t epi8 __attribute__ ((vector_size (16)));
  20. typedef uint8_t dec_numbers[SIZE] __attribute__ ((aligned (16)));
  21. typedef epi8 dec_blocks[NBLOCKS];
  22. typedef uint_fast64_t ind_t; // The type used for array indexes
  23. using namespace std;
  24. struct Semigroup
  25. {
  26. union {
  27. dec_numbers decs;
  28. dec_blocks blocks;
  29. };
  30. // Dont use char as they have to be promoted to 64 bits to do pointer arithmetic.
  31. ind_t conductor, min, genus,left_primitive,left,e,wilf;
  32. };
  33. void init_full_N(Semigroup &);
  34. void init(Semigroup&,char,char,char,char*);
  35. void print_Semigroup(const Semigroup &);
  36. void print_Semigroup_gen(const Semigroup&);
  37. void print_epi8(epi8);
  38. void output(const Semigroup& m,fstream& f);
  39. void record(const Semigroup& S,fstream& f);
  40. inline void copy_blocks( dec_blocks &__restrict__ dst,
  41. const dec_blocks &__restrict__ src);
  42. inline void remove_generator(Semigroup &__restrict__ dst,
  43. const Semigroup &__restrict__ src,
  44. ind_t gen,ind_t pos);
  45. inline Semigroup remove_generator(const Semigroup &src, ind_t gen,ind_t pos);
  46. // typedef enum { ALL, CHILDREN } generator_type;
  47. class ALL {};
  48. class CHILDREN {};
  49. // template <generator_type T> class generator_iter
  50. template <class T> class generator_iter{
  51. private:
  52. const Semigroup &m;
  53. unsigned int mask; // movemask_epi8 returns a 32 bits values
  54. ind_t iblock, gen, bound;
  55. public:
  56. generator_iter(const Semigroup &mon);
  57. bool move_next();
  58. uint8_t count();
  59. inline ind_t get_gen() const {return gen; };
  60. };
  61. ///////////////////////////////////////////////////////////////////////////
  62. ///////////////////////////////////////////////////////////////////////////
  63. //////////////// Implementation part here for inlining ////////////////
  64. ///////////////////////////////////////////////////////////////////////////
  65. ///////////////////////////////////////////////////////////////////////////
  66. /*
  67. Note: for some reason the code using gcc vector variables is 2-3% faster than
  68. the code using gcc intrinsic instructions.
  69. Here are the results of two runs:
  70. data_mmx = [9.757, 9.774, 9.757, 9.761, 9.811, 9.819, 9.765, 9.888, 9.774, 9.771]
  71. data_epi8 = [9.592, 9.535, 9.657, 9.468, 9.460, 9.679, 9.458, 9.461, 9.629, 9.474]
  72. */
  73. extern inline epi8 load_unaligned_epi8(const uint8_t *t)
  74. { return (epi8) _mm_loadu_si128((__m128i *) (t)); };
  75. extern inline epi8 shuffle_epi8(epi8 b, epi8 sh) // Require SSE 3
  76. { return (epi8) _mm_shuffle_epi8((__m128i) b, (__m128i) sh);}
  77. extern inline int movemask_epi8(epi8 b)
  78. { return _mm_movemask_epi8((__m128i) b);};
  79. const epi8 zero = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  80. const epi8 block1 = zero + 1;
  81. const uint8_t m1 = 255;
  82. const epi8 shift16[16] =
  83. { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15},
  84. {m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14},
  85. {m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13},
  86. {m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12},
  87. {m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11},
  88. {m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
  89. {m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  90. {m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7, 8},
  91. {m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6, 7},
  92. {m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5, 6},
  93. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4, 5},
  94. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3, 4},
  95. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2, 3},
  96. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1, 2},
  97. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0, 1},
  98. {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1, 0} };
  99. const epi8 mask16[16] =
  100. { {m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  101. { 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  102. { 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  103. { 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  104. { 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  105. { 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  106. { 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  107. { 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1,m1},
  108. { 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1,m1},
  109. { 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1,m1},
  110. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1,m1},
  111. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1,m1},
  112. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1,m1},
  113. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1,m1},
  114. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1,m1},
  115. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,m1} };
  116. template<> inline generator_iter<ALL>::generator_iter(const Semigroup &mon)
  117. : m(mon), bound((mon.conductor+mon.min+15) >> 4){
  118. epi8 block;
  119. iblock = 0;
  120. block = m.blocks[0];
  121. mask = movemask_epi8(block == block1);
  122. mask &= 0xFFFE; // 0 is not a generator
  123. gen = - 1;
  124. };
  125. template<> inline generator_iter<CHILDREN>::generator_iter(const Semigroup &mon)
  126. : m(mon), bound((mon.conductor+mon.min+15) >> 4){
  127. epi8 block;
  128. iblock = m.conductor >> 4;
  129. block = m.blocks[iblock] & mask16[m.conductor & 0xF];
  130. mask = movemask_epi8(block == block1);
  131. gen = (iblock << 4) - 1;
  132. };
  133. template <class T> inline uint8_t generator_iter<T>::count(){
  134. uint8_t nbr = _mm_popcnt_u32(mask); // popcnt returns a 8 bits value
  135. for (ind_t ib = iblock+1; ib < bound; ib++)
  136. nbr += _mm_popcnt_u32(movemask_epi8(m.blocks[ib] == block1));
  137. return nbr;
  138. };
  139. template <class T> inline bool generator_iter<T>::move_next(){
  140. while (!mask){
  141. iblock++;
  142. if (iblock > bound) return false;
  143. gen = (iblock << 4) - 1;
  144. mask = movemask_epi8(m.blocks[iblock] == block1);
  145. }
  146. unsigned char shift = __bsfd (mask) + 1; // Bit Scan Forward
  147. gen += shift;
  148. mask >>= shift;
  149. return true;
  150. };
  151. inline __attribute__((always_inline))
  152. void copy_blocks(dec_blocks &dst, dec_blocks const &src){
  153. for (ind_t i=0; i<NBLOCKS; i++) dst[i] = src[i];
  154. }
  155. #include <cassert>
  156. inline __attribute__((always_inline))
  157. void remove_generator(Semigroup &__restrict__ dst,
  158. const Semigroup &__restrict__ src,
  159. ind_t gen,
  160. ind_t pos){
  161. ind_t start_block, decal;
  162. epi8 block;
  163. assert(src.decs[gen] == 1);
  164. ind_t t=gen+1;
  165. dst.conductor = t;
  166. dst.genus = src.genus + 1;
  167. int delta;
  168. if(gen==src.min){
  169. dst.min=gen+1;
  170. delta=1;
  171. }
  172. else{
  173. dst.min=src.min;
  174. delta=(src.decs[gen+src.min]==2)?0:-1;
  175. }
  176. dst.e=src.e+delta;
  177. assert(dst.e==((gen==src.min)?src.e+1:((src.decs[gen+src.min]==2)?src.e:src.e-1)));
  178. ind_t k=gen-src.conductor;
  179. assert(dst.conductor==src.conductor+k+1);
  180. dst.left=src.left+k;
  181. dst.left_primitive=src.left_primitive+pos;
  182. //dst.wilf=dst.e*dst.left-dst.conductor;//src.wilf+delta*(src.left+k)-k-1;
  183. dst.wilf=src.wilf+delta*(src.left+k)+(src.e-1)*k-1;
  184. copy_blocks(dst.blocks, src.blocks);
  185. start_block = gen >> 4;
  186. decal = gen & 0xF;
  187. // Shift block by decal uchar
  188. block = shuffle_epi8(src.blocks[0], shift16[decal]);
  189. dst.blocks[start_block] -= ((block != zero) & block1);
  190. for (auto i=start_block+1; i<NBLOCKS; i++){
  191. // The following won't work due to some alignment problem (bug in GCC 4.7.1?)
  192. // block = *((epi8*)(src.decs + ((i-start_block)<<4) - decal));
  193. block = load_unaligned_epi8(src.decs + ((i-start_block)<<4) - decal);
  194. dst.blocks[i] -= ((block != zero) & block1);
  195. }
  196. assert(dst.decs[dst.conductor-1] == 0);
  197. }
  198. inline Semigroup
  199. remove_generator(const Semigroup &src, ind_t gen,ind_t pos){
  200. Semigroup dst;
  201. remove_generator(dst, src, gen,pos);
  202. return dst;
  203. }
  204. #endif