semigroup.hpp 7.8 KB

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