diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-19 10:45:37 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-19 10:45:37 +0100 |
| commit | 15ceee3a7c72d174305fff9b4fe9886cb3f7204d (patch) | |
| tree | fc2dc7280ccb581f49d8b123e5d7ea32673293a3 /cube.c | |
| parent | 13275a4bb8696e730a4fffdd05b8e1d046e489c6 (diff) | |
| download | nissy-core-15ceee3a7c72d174305fff9b4fe9886cb3f7204d.tar.gz nissy-core-15ceee3a7c72d174305fff9b4fe9886cb3f7204d.zip | |
Removed inverse_fast, probably not going to use it in tight loops
Diffstat (limited to 'cube.c')
| -rw-r--r-- | cube.c | 95 |
1 files changed, 24 insertions, 71 deletions
| @@ -134,6 +134,12 @@ Section: constants, strings and other stuff | |||
| 134 | #define _eflip 0x10U | 134 | #define _eflip 0x10U |
| 135 | #define _error 0xFFU | 135 | #define _error 0xFFU |
| 136 | 136 | ||
| 137 | _static cube_t zero = { .corner = {0}, .edge = {0} }; | ||
| 138 | _static cube_t solved = { | ||
| 139 | .corner = {0, 1, 2, 3, 4, 5, 6, 7}, | ||
| 140 | .edge = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} | ||
| 141 | }; | ||
| 142 | |||
| 137 | #define zero_fast fastcube( \ | 143 | #define zero_fast fastcube( \ |
| 138 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | 144 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
| 139 | #define solved_fast fastcube( \ | 145 | #define solved_fast fastcube( \ |
| @@ -507,7 +513,6 @@ _static_inline bool equal_fast(cube_fast_t, cube_fast_t); | |||
| 507 | _static_inline bool issolved_fast(cube_fast_t); | 513 | _static_inline bool issolved_fast(cube_fast_t); |
| 508 | _static_inline cube_fast_t invertco_fast(cube_fast_t); | 514 | _static_inline cube_fast_t invertco_fast(cube_fast_t); |
| 509 | _static_inline cube_fast_t cleanaftershuffle(cube_fast_t); | 515 | _static_inline cube_fast_t cleanaftershuffle(cube_fast_t); |
| 510 | _static_inline cube_fast_t inverse_fast(cube_fast_t); | ||
| 511 | _static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t); | 516 | _static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t); |
| 512 | _static_inline int64_t coord_fast_eo(cube_fast_t); | 517 | _static_inline int64_t coord_fast_eo(cube_fast_t); |
| 513 | 518 | ||
| @@ -617,45 +622,6 @@ cleanaftershuffle(cube_fast_t c) | |||
| 617 | } | 622 | } |
| 618 | 623 | ||
| 619 | _static_inline cube_fast_t | 624 | _static_inline cube_fast_t |
| 620 | inverse_fast(cube_fast_t c) | ||
| 621 | { | ||
| 622 | /* Method taken from Andrew Skalski's vcube[1]. The addition sequence | ||
| 623 | * was generated using [2]. | ||
| 624 | * [1] https://github.com/Voltara/vcube | ||
| 625 | * [2] http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html | ||
| 626 | */ | ||
| 627 | cube_fast_t v3, vi, vo, vp, ret; | ||
| 628 | |||
| 629 | v3 = _mm256_shuffle_epi8(c, c); | ||
| 630 | v3 = _mm256_shuffle_epi8(v3, c); | ||
| 631 | vi = _mm256_shuffle_epi8(v3, v3); | ||
| 632 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 633 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 634 | vi = _mm256_shuffle_epi8(vi, v3); | ||
| 635 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 636 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 637 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 638 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 639 | vi = _mm256_shuffle_epi8(vi, c); | ||
| 640 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 641 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 642 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 643 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 644 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 645 | vi = _mm256_shuffle_epi8(vi, v3); | ||
| 646 | vi = _mm256_shuffle_epi8(vi, vi); | ||
| 647 | vi = _mm256_shuffle_epi8(vi, c); | ||
| 648 | |||
| 649 | vo = _mm256_and_si256(c, _mm256_or_si256(_eo_avx2, _co2_avx2)); | ||
| 650 | vo = _mm256_shuffle_epi8(vo, vi); | ||
| 651 | vp = _mm256_andnot_si256(_mm256_or_si256(_eo_avx2, _co2_avx2), vi); | ||
| 652 | ret = _mm256_or_si256(vp, vo); | ||
| 653 | ret = cleanaftershuffle(ret); | ||
| 654 | |||
| 655 | return invertco_fast(ret); | ||
| 656 | } | ||
| 657 | |||
| 658 | _static_inline cube_fast_t | ||
| 659 | compose_fast(cube_fast_t c1, cube_fast_t c2) | 625 | compose_fast(cube_fast_t c1, cube_fast_t c2) |
| 660 | { | 626 | { |
| 661 | cube_fast_t ret; | 627 | cube_fast_t ret; |
| @@ -716,7 +682,6 @@ _static cube_t fasttocube(cube_fast_t); | |||
| 716 | _static_inline bool equal_fast(cube_fast_t, cube_fast_t); | 682 | _static_inline bool equal_fast(cube_fast_t, cube_fast_t); |
| 717 | _static_inline bool issolved_fast(cube_fast_t); | 683 | _static_inline bool issolved_fast(cube_fast_t); |
| 718 | _static_inline cube_fast_t invertco_fast(cube_fast_t); | 684 | _static_inline cube_fast_t invertco_fast(cube_fast_t); |
| 719 | _static_inline cube_fast_t inverse_fast(cube_fast_t); | ||
| 720 | _static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t); | 685 | _static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t); |
| 721 | _static_inline int64_t coord_fast_eo(cube_fast_t); | 686 | _static_inline int64_t coord_fast_eo(cube_fast_t); |
| 722 | 687 | ||
| @@ -812,29 +777,6 @@ invertco_fast(cube_fast_t c) | |||
| 812 | } | 777 | } |
| 813 | 778 | ||
| 814 | _static_inline cube_fast_t | 779 | _static_inline cube_fast_t |
| 815 | inverse_fast(cube_fast_t cube) | ||
| 816 | { | ||
| 817 | cube_fast_t ret; | ||
| 818 | uint8_t i, piece, orien; | ||
| 819 | |||
| 820 | ret = zero_fast; | ||
| 821 | |||
| 822 | for (i = 0; i < 12; i++) { | ||
| 823 | piece = cube.edge[i]; | ||
| 824 | orien = piece & _eobit; | ||
| 825 | ret.edge[piece & _pbits] = i | orien; | ||
| 826 | } | ||
| 827 | |||
| 828 | for (i = 0; i < 8; i++) { | ||
| 829 | piece = cube.corner[i]; | ||
| 830 | orien = ((piece << 1) | (piece >> 1)) & _cobits2; | ||
| 831 | ret.corner[piece & _pbits] = i | orien; | ||
| 832 | } | ||
| 833 | |||
| 834 | return ret; | ||
| 835 | } | ||
| 836 | |||
| 837 | _static_inline cube_fast_t | ||
| 838 | compose_fast(cube_fast_t c1, cube_fast_t c2) | 780 | compose_fast(cube_fast_t c1, cube_fast_t c2) |
| 839 | { | 781 | { |
| 840 | cube_fast_t ret; | 782 | cube_fast_t ret; |
| @@ -895,12 +837,6 @@ previous sections, while some other operate directly on the cube. | |||
| 895 | invertco_fast(compose_fast(compose_fast(_trans_cube_ ## T, c), \ | 837 | invertco_fast(compose_fast(compose_fast(_trans_cube_ ## T, c), \ |
| 896 | _trans_cube_ ## T ## _inverse)) | 838 | _trans_cube_ ## T ## _inverse)) |
| 897 | 839 | ||
| 898 | _static cube_t zero = { .corner = {0}, .edge = {0} }; | ||
| 899 | _static cube_t solved = { | ||
| 900 | .corner = {0, 1, 2, 3, 4, 5, 6, 7}, | ||
| 901 | .edge = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} | ||
| 902 | }; | ||
| 903 | |||
| 904 | cube_t solvedcube(void); | 840 | cube_t solvedcube(void); |
| 905 | bool isconsistent(cube_t); | 841 | bool isconsistent(cube_t); |
| 906 | bool issolvable(cube_t); | 842 | bool issolvable(cube_t); |
| @@ -1076,10 +1012,27 @@ compose(cube_t c1, cube_t c2) | |||
| 1076 | cube_t | 1012 | cube_t |
| 1077 | inverse(cube_t cube) | 1013 | inverse(cube_t cube) |
| 1078 | { | 1014 | { |
| 1015 | cube_t ret; | ||
| 1016 | uint8_t i, piece, orien; | ||
| 1017 | |||
| 1079 | DBG_ASSERT(isconsistent(cube), zero, | 1018 | DBG_ASSERT(isconsistent(cube), zero, |
| 1080 | "inverse error: inconsistent cube\n"); | 1019 | "inverse error: inconsistent cube\n"); |
| 1081 | 1020 | ||
| 1082 | return fasttocube(inverse_fast(cubetofast(cube))); | 1021 | ret = zero_fast; |
| 1022 | |||
| 1023 | for (i = 0; i < 12; i++) { | ||
| 1024 | piece = cube.edge[i]; | ||
| 1025 | orien = piece & _eobit; | ||
| 1026 | ret.edge[piece & _pbits] = i | orien; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | for (i = 0; i < 8; i++) { | ||
| 1030 | piece = cube.corner[i]; | ||
| 1031 | orien = ((piece << 1) | (piece >> 1)) & _cobits2; | ||
| 1032 | ret.corner[piece & _pbits] = i | orien; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | return ret; | ||
| 1083 | } | 1036 | } |
| 1084 | 1037 | ||
| 1085 | cube_t | 1038 | cube_t |
