-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlibtmscore_cpu.cpp
2708 lines (2523 loc) · 95.5 KB
/
libtmscore_cpu.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Ling-Hong Hung Aug 2013
//routines for CPU RMSD/TMscore - no OpenCL
#include "lite.h"
#include "error_handlers.h"
#ifdef OPENMP
#include <omp.h>
#endif
#include <iostream>
#define PI 3.1415926535897932
#define SQRT3 1.732050807568877
#define COORDS_BUFFER_SIZE 768
extern double gtimer1,gtimer2;
using namespace std;
//subroutines for all v all TMscore
//CPU version of RMSD/TMScore using hybrid Kabsch/quaternion
float rmsd_cpu(int nat,float *coords1,float *coords2,float *rmatrix);
float rmsd_cpu(int nat,float *coords1,float *coords2);
float tmscore_rmsd_cpu(int nat,float *coords1,float *coords2,float bR[3][3], float bt[3],float *rmsd);
//SSE/AVX accelerated RMSD/TMScore
#ifdef SSE2
//sse2
void print_m128(__m128 p);
//rmsd
//The SSE/AVX routines use pre-shuffled coordinates for the SOA ie. split into separate x, y and z streams and padded with zeros to fit boundaries
//This is normally less or equally efficient than just transforming on the fly but for all vs all matrices
//In addition for RMSD the coordinates are all pre-centered which eliminates a lot of the complexity
//this needs only to be done once for the entire ensemble of coordinates so costs little for RMSD
//Using AVX there are enough registers to do also the centering efficiently in place
//For TMScore new centers need to be calculated for each subset of atoms used to seed the comparison so this useful for the first subset only
int shuffle_coords4_sse (int nstructs,int pdb_size, float *coords, float *shuffled_coords,float *ssqs);
int shuffle_coords4_sse (int nstructs,int pdb_size, float *coords, float *shuffled_coords,float *ssqs, float *centroids);
float center_coords4_sse(int nat, float *coords);
//TMscore
void shuffle_tmscore_coords_soa_sse(int nstructs, int nat, float *coords,float **x,float **y,float **z);
void split_coords_sse(int nat, float *coords, float *x, float *y,float *z);
//main TMscore routine
float tmscore_cpu_soa_sse2(int nat, float *x1, float *y1,float *z1,float *x2,float *y2,float *z2,float bR[3][3], float bt[3],float *rmsd);
//score fun routines - applies matrix and determines next set of atoms for RMSD in next iteration
int score_fun_soa_sse(int nat, float d0, float d, float *r,float *x1, float *y1, float *z1, float *x2, float *y2, float *z2, int *ialign,int *nalign,float *tm_score);
//Kabsch-quatXXX organizes the coordinates to a contiguous cache friendly form
//for SSE there are not enough registers to calculate the centers and sum of squares in the same pass as the covariances
float kabsch_quat_soa_sse2(int nat, int *map, float *x1, float *y1,float *z1, float *x2,float *y2, float *z2,float *r); //given a subset of coords - finds optimal rotation and transformation to apply to whole set of coords
//coords_sum_ssqXXX are used by Kabsch-quat routines to determine center and sum of squares separately for each subset of atoms
//AVX code uses both halves of the 256 bit registers to be able to do both calculations in one pass
float coords_sum_ssq_xyz_sse2(int nat, float *x, float *y,float *z,float center[3]);
//calculates optimal matrix for starting subsets of matched atoms
float rmsd_sse2_matrix_xyz (int nat,float *c1x,float *c1y, float *c1z,float *c2x,float *c2y, float *c2z,float center1[3], float center2[3],double ssq,float u[3][3]);
//simple sse accelerated rotation multiplication
void R34v4_sse2 (float r[16],float *x,float *Rx);
//LG_score_ Levitt-Gerstein metric scoring routines - i.e. calculate and apply rotation matrix and return LG score
//This is the main routine in the TMscore algorithm
//The SOA form is faster and is easily extended to 256-wide AVX - However the AOS form can almost be as fast for SSE depending on HADD performance
float LG_score_soa_sse (float r[16],int nat, float *x1,float *y1,float *z1, float *x2, float *y2, float *z2, float *d,float invd0d0);
#endif
#ifdef SSE3
//SSE3 versions - only slightly faster using HADD
float rmsd_sse3 (int nat,float *coords1,float *coords2,double ssq,float *rmatrix);
float rmsd_sse3 (int nat,float *coords1,float *coords2,double ssq,float u[3][3]); //returns rotation matrix
float tmscore_cpu_soa_sse3(int nat, float *x1, float *y1,float *z1,float *x2,float *y2,float *z2,float bR[3][3], float bt[3],float *rmsd);
float kabsch_quat_soa_sse3(int nat, int *map, float *x1, float *y1,float *z1, float *x2,float *y2, float *z2,float *r,float *coords_buffer);
float coords_sum_ssq_xyz_sse3(int nat, float *x, float *y,float *z,float center[3]);
void R34v4_sse3 (float r[16],float *x,float *Rx);
float rmsd_sse3_matrix_xyz (int nat,float *c1x,float *c1y, float *c1z,float *c2x,float *c2y, float *c2z,float center1[3], float center2[3],double ssq,float u[3][3]); //uses precalculated ssq and split coords for TMScore coords subsets
#endif
#ifdef AVX
//AVX versions - much larger changes - use of half-registers - hadds are trickier
//speed gain offset by - emulated AVX instructions - cache pressure and loading of wide data
//also previously mino non-vectorized code becomes limiting factor - see about 5-20% increase on Bulldozers and laptop i7 overall
void print_m256(__m256 p);
//use memalign to allocate non-local arrays __attribute__ does not work for heap storage
int shuffle_coords8_avx (int nstructs,int pdb_size, float *coords, float *shuffled_coords,float *ssqs);
int shuffle_coords8_avx (int nstructs,int pdb_size, float *coords, float *shuffled_coords,float *ssqs,float *centroids);
float shuffle_center_coords8_avx(int nat, float *coords, float *shuffled_coords,float centroid[3]);
//rmsd using precentered coords
float rmsd_avx (int nat,float *coords1,float *coords2,double ssq,float u[3][3]);
float rmsd_avx (int nat,float *coords1,float *coords2,double ssq);
//rmsd using uncentered coords using wide registers to do centering in same pass as calculation of covariacnes
//used for TMScore but can be modified for normal usage
float rmsd_uncentered_avx (int nat,float *c1x,float *c1y, float *c1z,float *c2x,float *c2y, float *c2z,float *rm);
//earlier version using precalculated ssq sse
float rmsd_avx_matrix_xyz (int nat,float *c1x,float *c1y, float *c1z,float *c2x,float *c2y, float *c2z,float center1[3], float center2[3],double ssq,float u[3][3]);
void shuffle_tmscore_coords_soa_avx(int nstructs, int nat, float *coords,float **x,float **y,float **z);
void split_coords_avx(int nat, float *coords, float *x, float *y,float *z);
float tmscore_cpu_soa_avx(int nat, float *x1, float *y1,float *z1,float *x2,float *y2,float *z2,float bR[3][3], float bt[3],float *rmsd);
float kabsch_quat_soa_avx(int nat, int *map, float *x1, float *y1,float *z1, float *x2,float *y2, float *z2,float *r,float *coords_buffer);
float coords_sum_ssq_avx(int nat, float *x, float *y,float *z,float center[3]);
int score_fun_soa_avx(int nat, float d0, float d, float *r,float *x1, float *y1, float *z1, float *x2, float *y2, float *z2, int *ialign,int *nalign,float *tm_score);
float LG_score_soa_avx (float r[16],int nat, float *x1,float *y1,float *z1, float *x2, float *y2, float *z2, float *d,float invd0d0);
#endif
//scalar routines
void center_all_coords(int nstructs,int nat,float *coords,float *centered_coords);
//iterative TMscore routine
int score_fun_dcoords(int nat, float d0, float d, double R[3][3], double t[3],double *coords1, double *coords2,double *acoords,int *ialign,int *nalign,float *tm_score);
//eigenvector routine from quaternion method
template <class T> void rmatrix (T ev,T r[3][3],T u[3][3]);
template <class T> void rmatrix (T ev,T r[9],T u[3][3]);
//optimised Kabsch routine to be used with eigenvector matrix calculation
double rmsd_svd(int nat,double *my_coords,double u[3][3],double t[3],bool rmsd_flag);
void R34v4(float *r,float *x, float *Rx);
template <class T> void dump_matrix(T u[3][3]);
template <class T> void dump_vector(T u[3]);
//scalar
void center_all_coords(int nstructs,int nat,float *coords,float *centered_coords){
for (int p=0;p<nstructs;p++){
float sums[3]={0.0f,0.0f,0.0f};
float invnat=1.0f/(float) nat;
float const *pcoords=&(coords[p*nat*3]);
float *cpcoords=&(centered_coords[p*nat*3]);
for(int i=0;i<nat;i++){
sums[0]+=pcoords[3*i];
sums[1]+=pcoords[3*i+1];
sums[2]+=pcoords[3*i+2];
}
for(int i=0;i<3;i++){
sums[i]*=invnat;
}
for(int i=0;i<nat;i++){
cpcoords[3*i]=pcoords[3*i]-sums[0];
cpcoords[3*i+1]=pcoords[3*i+1]-sums[1];
cpcoords[3*i+2]=pcoords[3*i+2]-sums[2];
}
}
}
template <class T> void rmatrix (T ev,T r[3][3],T u[3][3]){
//calculate rotation matrix
T a00=(r[0][0]+r[1][1]+r[2][2]);
T a01=(r[1][2]-r[2][1]);
T a02=(r[2][0]-r[0][2]);
T a03=(r[0][1]-r[1][0]);
T a11=(r[0][0]-r[1][1]-r[2][2]);
T a12=(r[0][1]+r[1][0]);
T a13=(r[2][0]+r[0][2]);
T a22=(-r[0][0]+r[1][1]-r[2][2]);
T a23=(r[1][2]+r[2][1]);
T a33=(-r[0][0]-r[1][1]+r[2][2]);
//from Theobald
a00-=ev;a11-=ev;a22-=ev;a33-=ev;
T a2233_3223 = a22 * a33 - a23 * a23;
T a1233_3123 = a12 * a33-a13*a23;
T a1223_3122 = a12 * a23 - a13 * a22;
T a0232_3022 = a02 * a23-a03*a22;
T a0233_3023 = a02 * a33 - a03 * a23;
T a0231_3021 = a02 * a13-a03*a12;
T q[4]={a11*a2233_3223-a12*a1233_3123+a13*a1223_3122, -a01*a2233_3223+a12*a0233_3023-a13*a0232_3022,a01*a1233_3123-a11*a0233_3023+a13*a0231_3021,-a01*a1223_3122+a11*a0232_3022-a12*a0231_3021};
T invlen2q=1.0f/(q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3]);
T aj=q[0]*q[0]*invlen2q;
T xj=q[1]*q[1]*invlen2q;
T yj=q[2]*q[2]*invlen2q;
T zj=q[3]*q[3]*invlen2q;
T xy = q[1] * q[2]*invlen2q;
T az = q[0] * q[3]*invlen2q;
T zx = q[3] * q[1]*invlen2q;
T ay = q[0] * q[2]*invlen2q;
T yz = q[2] * q[3]*invlen2q;
T ax = q[0] * q[1]*invlen2q;
u[0][0]= aj + xj - yj - zj; u[0][1]= 2.0f * (xy + az); u[0][2]= 2.0f * (zx - ay);
u[1][0]= 2.0f * (xy - az); u[1][1]=aj - xj + yj - zj; u[1][2]= 2.0f * (yz + ax);
u[2][0]= 2.0f * (zx + ay), u[2][1]= 2.0f * (yz - ax); u[2][2]= aj - xj - yj + zj;
}
template <class T> void rmatrix (T ev,T r[9],T u[3][3]){
//calculate rotation matrix
T a00=(r[0]+r[4]+r[8]);
T a01=(r[5]-r[7]);
T a02=(r[6]-r[2]);
T a03=(r[1]-r[3]);
T a11=(r[0]-r[4]-r[8]);
T a12=(r[1]+r[3]);
T a13=(r[6]+r[2]);
T a22=(-r[0]+r[4]-r[8]);
T a23=(r[5]+r[7]);
T a33=(-r[0]-r[4]+r[8]);
//from Theobald
a00-=ev;a11-=ev;a22-=ev;a33-=ev;
T a2233_3223 = a22 * a33 - a23 * a23;
T a1233_3123 = a12 * a33-a13*a23;
T a1223_3122 = a12 * a23 - a13 * a22;
T a0232_3022 = a02 * a23-a03*a22;
T a0233_3023 = a02 * a33 - a03 * a23;
T a0231_3021 = a02 * a13-a03*a12;
T q[4]={a11*a2233_3223-a12*a1233_3123+a13*a1223_3122, -a01*a2233_3223+a12*a0233_3023-a13*a0232_3022,a01*a1233_3123-a11*a0233_3023+a13*a0231_3021,-a01*a1223_3122+a11*a0232_3022-a12*a0231_3021};
T len2q=q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3];
if(!len2q){
//return the identity matrix
u[0][0]= 1.0f; u[0][1]= 0.0f; u[0][2]= 0.0f;
u[1][0]= 0.0f; u[1][1]= 1.0f; u[1][2]= 0.0f;
u[2][0]= 0.0f; u[2][1]= 0.0f; u[2][2]= 1.0f;
}
else{
T invlen2q=1.0/len2q;
T aj=q[0]*q[0]*invlen2q;
T xj=q[1]*q[1]*invlen2q;
T yj=q[2]*q[2]*invlen2q;
T zj=q[3]*q[3]*invlen2q;
T xy = q[1] * q[2]*invlen2q;
T az = q[0] * q[3]*invlen2q;
T zx = q[3] * q[1]*invlen2q;
T ay = q[0] * q[2]*invlen2q;
T yz = q[2] * q[3]*invlen2q;
T ax = q[0] * q[1]*invlen2q;
u[0][0]= aj + xj - yj - zj; u[0][1]= 2.0 * (xy + az); u[0][2]= 2.0 * (zx - ay);
u[1][0]= 2.0 * (xy - az); u[1][1]=aj - xj + yj - zj; u[1][2]= 2.0 * (yz + ax);
u[2][0]= 2.0 * (zx + ay); u[2][1]= 2.0 * (yz - ax); u[2][2]= aj - xj - yj + zj;
}
}
void rmatrix_d(double ev,double r[3][3],double u[3][3]){
//calculate rotation matrix
double a00=(r[0][0]+r[1][1]+r[2][2]);
double a01=(r[1][2]-r[2][1]);
double a02=(r[2][0]-r[0][2]);
double a03=(r[0][1]-r[1][0]);
double a11=(r[0][0]-r[1][1]-r[2][2]);
double a12=(r[0][1]+r[1][0]);
double a13=(r[2][0]+r[0][2]);
double a22=(-r[0][0]+r[1][1]-r[2][2]);
double a23=(r[1][2]+r[2][1]);
double a33=(-r[0][0]-r[1][1]+r[2][2]);
//from Theobald
a00-=ev;a11-=ev;a22-=ev;a33-=ev;
double a2233_3223 = a22 * a33 - a23 * a23;
double a1233_3123 = a12 * a33-a13*a23;
double a1223_3122 = a12 * a23 - a13 * a22;
double a0232_3022 = a02 * a23-a03*a22;
double a0233_3023 = a02 * a33 - a03 * a23;
double a0231_3021 = a02 * a13-a03*a12;
double q[4]={a11*a2233_3223-a12*a1233_3123+a13*a1223_3122, -a01*a2233_3223+a12*a0233_3023-a13*a0232_3022,a01*a1233_3123-a11*a0233_3023+a13*a0231_3021,-a01*a1223_3122+a11*a0232_3022-a12*a0231_3021};
double len2q=q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3];
if(!len2q){
//return the identity matrix
u[0][0]= 1.0f; u[0][1]= 0.0f; u[0][2]= 0.0f;
u[1][0]= 0.0f; u[1][1]= 1.0f; u[1][2]= 0.0f;
u[2][0]= 0.0f; u[2][1]= 0.0f; u[2][2]= 1.0f;
}
else{
double invlen2q=1.0/len2q;
double aj=q[0]*q[0]*invlen2q;
double xj=q[1]*q[1]*invlen2q;
double yj=q[2]*q[2]*invlen2q;
double zj=q[3]*q[3]*invlen2q;
double xy = q[1] * q[2]*invlen2q;
double az = q[0] * q[3]*invlen2q;
double zx = q[3] * q[1]*invlen2q;
double ay = q[0] * q[2]*invlen2q;
double yz = q[2] * q[3]*invlen2q;
double ax = q[0] * q[1]*invlen2q;
u[0][0]= aj + xj - yj - zj; u[0][1]= 2.0 * (xy + az); u[0][2]= 2.0 * (zx - ay);
u[1][0]= 2.0 * (xy - az); u[1][1]=aj - xj + yj - zj; u[1][2]= 2.0 * (yz + ax);
u[2][0]= 2.0 * (zx + ay); u[2][1]= 2.0 * (yz - ax); u[2][2]= aj - xj - yj + zj;
}
}
int score_fun_dcoords(int nat, float d0, float d, double R[3][3], double t[3],double *coords1, double *coords2,double *acoords,int *ialign,int *nalign,float *tm_score){
//ialign points to atom number
int k,ncut=0,nchange=0,my_nalign=*nalign;
double invd0d0=1.0/(double)(d0*d0);
double d2,dist;
double *my_dist=0,my_score=0;
if(!(my_dist=(double*)malloc(nat*sizeof(double))))exit(FALSE);
//keep nmin smallest distances && distances < dtmp
for(k=0;k<nat;k++){
double u[3];
int m=3*k;
u[0]=t[0]+R[0][0]*coords1[m]+R[1][0]*coords1[m+1]+R[2][0]*coords1[m+2]-coords2[m];
u[1]=t[1]+R[0][1]*coords1[m]+R[1][1]*coords1[m+1]+R[2][1]*coords1[m+2]-coords2[m+1];
u[2]=t[2]+R[0][2]*coords1[m]+R[1][2]*coords1[m+1]+R[2][2]*coords1[m+2]-coords2[m+2];
dist=u[0]*u[0]+u[1]*u[1]+u[2]*u[2];
my_score+=1.0/(1.0+dist*invd0d0);
my_dist[k]=dist;
}
//adjust d until there are at least 3 the same
while(ncut <3)
{
d2=d*d;
ncut=0;
for(k=0;k<nat;k++)
if(my_dist[k]<d2) ncut++;
d+=.5;
}
ncut=0;
for(k=0;k<nat;k++)
if(my_dist[k]<d2)
{
if(ncut < my_nalign && ialign[ncut] == k)ncut++;
else
{
nchange=1;
ialign[ncut++]=k;
}
}
if(my_dist)free(my_dist);
*tm_score=my_score/(double)nat;
if(!nchange)return(0);
int m=0;
for(k=0;k<ncut;k++)
{
int n=ialign[k];
acoords[m++]=coords1[3*n];
acoords[m++]=coords1[3*n+1];
acoords[m++]=coords1[3*n+2];
acoords[m++]=coords2[3*n];
acoords[m++]=coords2[3*n+1];
acoords[m++]=coords2[3*n+2];
}
*nalign=ncut;
return(1);
}
float rmsd_cpu(int nat,float *coords1,float *coords2){
double e0=0,d,rr[6], ss[6], e[3],r[3][3],rms=0;
double spur, det, cof, h, g, cth, sth, sqrth, p, sigma;
float s1x=0,s1y=0,s1z=0,s2x=0,s2y=0,s2z=0,ssq=0;
float sxx=0,sxy=0,sxz=0,syx=0,syy=0,syz=0,szx=0,szy=0,szz=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
r[i][j]=0;
for (int i=0;i<nat;i++){
int m=3*i;
float c1x=coords1[m];
float c1y=coords1[m+1];
float c1z=coords1[m+2];
float c2x=coords2[m];
float c2y=coords2[m+1];
float c2z=coords2[m+2];
s1x+=c1x;s1y+=c1y;s1z+=c1z;s2x+=c2x;s2y+=c2y;s2z+=c2z;
sxx+=c1x*c2x; sxy+=c1x*c2y; sxz+=c1x*c2z; syx+=c1y*c2x; syy+=c1y*c2y; syz+=c1y*c2z;szx+=c1z*c2x; szy+=c1z*c2y; szz+=c1z*c2z;
ssq+=c1x*c1x+c1y*c1y+c1z*c1z+c2x*c2x+c2y*c2y+c2z*c2z;
}
float invfnat=1.0/(float) nat;
r[0][0]=sxx-s1x*s2x*invfnat;
r[0][1]=sxy-s1x*s2y*invfnat;
r[0][2]=sxz-s1x*s2z*invfnat;
r[1][0]=syx-s1y*s2x*invfnat;
r[1][1]=syy-s1y*s2y*invfnat;
r[1][2]=syz-s1y*s2z*invfnat;
r[2][0]=szx-s1z*s2x*invfnat;
r[2][1]=szy-s1z*s2y*invfnat;
r[2][2]=szz-s1z*s2z*invfnat;
det= r[0][0] * ( (r[1][1]*r[2][2]) - (r[1][2]*r[2][1]) )- r[0][1] * ( (r[1][0]*r[2][2]) - (r[1][2]*r[2][0]) ) + r[0][2] * ( (r[1][0]*r[2][1]) - (r[1][1]*r[2][0]) );
sigma=det;
//for symmetric matrix
//lower triangular matrix rr
{
int m=0;
for(int i=0;i<3;i++)
for(int j=0;j<=i;j++)
rr[m++]= r[i][0]*r[j][0]+ r[i][1]*r[j][1]+ r[i][2]*r[j][2];
}
spur=(rr[0]+rr[2]+rr[5]) / 3.0; //average of diagonal sum
cof=(((((rr[2]*rr[5] - rr[4]*rr[4]) + rr[0]*rr[5])- rr[3]*rr[3]) + rr[0]*rr[2]) - rr[1]*rr[1]) / 3.0;
for(int i=0;i<3;i++)
e[i]=spur;
h=( spur > 0 )? spur*spur-cof : -1;
if(h>0)
{
det*=det;
g = (spur*cof - det)/2.0 - spur*h;
sqrth = sqrt(h);
d = h*h*h - g*g;
d= ( d<0 ) ? atan2(0,-g) / 3.0 : atan2(sqrt(d),-g)/3.0;
cth = sqrth * cos(d);
sth = sqrth*SQRT3*sin(d);
e[0] = (spur + cth) + cth;
e[1] = (spur - cth) + sth;
e[2] = (spur - cth) - sth;
}
for(int i=0;i<3;i++)
e[i]=(e[i] < 0) ? 0 : sqrt(e[i]);
d=e[2];
if(sigma < 0) d=-d;
d+=e[1] + e[0];
//translation for 1 to 2;
//calculate R vectors - d is the ev;
double xm=s1x*s1x+s1y*s1y+s1z*s1z+s2x*s2x+s2y*s2y+s2z*s2z;
double r2=(ssq-xm*invfnat-d-d)*invfnat;
rms=(r2>0.0) ? sqrt(r2) : 0.0;
return(rms);
}
double rmsd_svd(int nat,double *dcoords,double u[3][3], double t[3],bool rmsd_flag){
const double inv3=1.0/3.0;
const double dnat=(double)nat;
const double invdnat=1.0/(double)nat;
double drms=0,r[9]={0,0,0,0,0,0,0,0,0};
double s1x=0,s1y=0,s1z=0,s2x=0,s2y=0,s2z=0,ssq=0;
int m=0;
for (int i=0;i<nat;i++){
double c1x=dcoords[m++];
double c1y=dcoords[m++];
double c1z=dcoords[m++];
double c2x=dcoords[m++];
double c2y=dcoords[m++];
double c2z=dcoords[m++];
r[0]+=c1x*c2x; r[1]+=c1x*c2y; r[2]+=c1x*c2z; r[3]+=c1y*c2x; r[4]+=c1y*c2y; r[5]+=c1y*c2z; r[6]+=c1z*c2x; r[7]+=c1z*c2y; r[8]+=c1z*c2z;
s1x+=c1x;s1y+=c1y;s1z+=c1z;s2x+=c2x;s2y+=c2y;s2z+=c2z;
if(rmsd_flag) ssq+=c1x*c1x+c1y*c1y+c1z*c1z+c2x*c2x+c2y*c2y+c2z*c2z;
}
r[0]-=s1x*s2x*invdnat;
r[1]-=s1x*s2y*invdnat;
r[2]-=s1x*s2z*invdnat;
r[3]-=s1y*s2x*invdnat;
r[4]-=s1y*s2y*invdnat;
r[5]-=s1y*s2z*invdnat;
r[6]-=s1z*s2x*invdnat;
r[7]-=s1z*s2y*invdnat;
r[8]-=s1z*s2z*invdnat;
double det= r[0] * ( (r[4]*r[8]) - (r[5]*r[7]) )- r[1] * ( (r[3]*r[8]) - (r[5]*r[6]) ) + r[2] * ( (r[3]*r[7]) - (r[4]*r[6]) );
//for symmetric matrix
//lower triangular matrix rr
double detsq=det*det;
//lower triangular matrix rr
double rr[6]={r[0]*r[0]+ r[1]*r[1]+ r[2]*r[2],
r[3]*r[0]+ r[4]*r[1]+ r[5]*r[2],
r[3]*r[3]+ r[4]*r[4]+ r[5]*r[5],
r[6]*r[0]+ r[7]*r[1]+ r[8]*r[2],
r[6]*r[3]+ r[7]*r[4]+ r[8]*r[5],
r[6]*r[6]+ r[7]*r[7]+ r[8]*r[8]};
double spur=((double)(rr[0]+rr[2]+rr[5]))*inv3;
double cof=((double)(rr[2]*rr[5] - rr[4]*rr[4] + rr[0]*rr[5]- rr[3]*rr[3] + rr[0]*rr[2] - rr[1]*rr[1])) *inv3;
double e[3] ={spur,spur,spur};
double h=( spur > 0 )? spur*spur-cof : -1.0;
if(h>0)
{
double g = (spur*cof - detsq)*0.5 - spur*h;
double sqrth = sqrt(h);
double d1 = h*h*h - g*g;
d1= ( d1<0 ) ? atan2(0,-g)*inv3 : atan2(sqrt(d1),-g)*inv3;
double cth = sqrth * cos(d1);
double sth = sqrth*SQRT3*sin(d1);
e[0]+= cth+cth;
e[1]+= -cth+sth;
e[2]+= -cth-sth;
}
e[0]=(e[0] < 0) ? 0 : sqrt(e[0]);
e[1]=(e[1] < 0) ? 0 : sqrt(e[1]);
e[2]=(e[2] < 0) ? 0 : sqrt(e[2]);
double d=(det<0)? e[0] + e[1] -e[2] : e[0] + e[1]+e[2];
if(rmsd_flag){
double xm=s1x*s1x+s1y*s1y*+s1z*s1z+s2x*s2x+s2y*s2y+s2z*s2z;
drms=(ssq-xm*dnat-d-d)*invdnat;
drms=(drms>1e-8)?sqrt(drms) : 0.0f;
}
if(rmsd_flag){
double xm=s1x*s1x+s1y*s1y+s1z*s1z+s2x*s2x+s2y*s2y+s2z*s2z;
double r2=(ssq-xm*invdnat-d-d)*invdnat;
drms=(r2>1.0e-8) ? sqrt(r2) : 0.0;
}
if(u && t){
rmatrix(d,r,u);
t[0] =s2x*invdnat - (u[0][0]*s1x*invdnat + u[1][0]*s1y*invdnat + u[2][0]*s1z*invdnat);
t[1]= s2y*invdnat - (u[0][1]*s1x*invdnat + u[1][1]*s1y*invdnat + u[2][1]*s1z*invdnat);
t[2]= s2z*invdnat - (u[0][2]*s1x*invdnat + u[1][2]*s1y*invdnat + u[2][2]*s1z*invdnat);
}
return(drms);
}
float tmscore_rmsd_cpu(int nat,float *coords1,float *coords2, float bR[3][3], float bt[3], float *rmsd){
int nalign;
float max_score=-1,rms;
double R[3][3],t[3];
int ialign[nat];
double acoords[6*nat],dcoords1[3*nat],dcoords2[3*nat];
for(int i=0;i<nat*3;i++){
dcoords1[i]=coords1[i];
dcoords2[i]=coords2[i];
}
//d0
float d0=1.24*pow((nat-15),(1.0/3.0))-1.8;
if(d0< 0.5)d0=0.5;
//d0_search ----->
float d,d0_search=d0;
if(d0_search > 8)d0_search=8;
if(d0_search <4.5)d0_search=4.5;
//iterative parameters ----->
int n_it=20; //maximum number of iterations
int n_init_max=6; //maximum number of L_init
int n_init=0;
int L_ini_min=4;
int L_ini[6];
if(nat < 4) L_ini_min=nat;
int len=nat;
int divisor=1;
while(len > L_ini_min && n_init <5)
{
L_ini[n_init++]=len;
divisor*=2;
len=nat/divisor;
}
L_ini[n_init++]=4;
if (L_ini[n_init-1] > L_ini_min)L_ini[n_init++]=L_ini_min;;
// find the maximum score starting from local structures superposition
float score; //TM-score
for (int seed=0;seed<n_init;seed++)
{
//find the initial rotation matrix using the initial seed residues
int L_init=L_ini[seed];
for(int istart=0;istart<=nat-L_init;istart++)
{
int nchanges=1;
int nalign=L_init;
{
int m=0;
int n=0;
for(int i=0;i<nat;i++)
{
if(i>=istart && i<istart+L_init)
{
ialign[n++]=i;
int p=3*i;
acoords[m++]=dcoords1[p];
acoords[m++]=dcoords1[p+1];
acoords[m++]=dcoords1[p+2];
acoords[m++]=dcoords2[p];
acoords[m++]=dcoords2[p+1];
acoords[m++]=dcoords2[p+2];
}
}
}
if(!seed && rmsd)
*rmsd=(float)rmsd_svd(nalign,acoords,R, t,1);
else
rmsd_svd(nalign,acoords,R, t,0);
score_fun_dcoords(nat, d0, d0_search-1,R,t,dcoords1,dcoords2,acoords,ialign,&nalign,&score);
d=d0_search+1;
if(score > max_score)
{
max_score=score;
if(bR && bt){
for(int j=0;j<3;j++)
{
bt[j]=t[j];
for(int k=0;k<3;k++)
bR[j][k]=R[j][k];
}
}
}
//extend search from seed
for (int iter=0;iter<n_it && nchanges;iter++)
{
rmsd_svd(nalign,acoords,R, t,0);
nchanges=score_fun_dcoords(nat, d0,d,R,t,dcoords1,dcoords2,acoords,ialign,&nalign,&score);
if(score > max_score){
max_score=score;
if(bR && bt){
for(int j=0;j<3;j++){
bt[j]=t[j];
for(int k=0;k<3;k++)
bR[j][k]=R[j][k];
}
}
}
}
}
}
return(max_score);
}
#ifdef SSE2
//sse2
void print_m128(__m128 p){
float t[4];
_mm_storeu_ps(t,p);
for (int i=0;i<4;i++)
fprintf(stderr,"%10.3f ",t[i]);
fprintf(stderr,"\n");
}
float shuffle_center_coords4_sse(int nat, float *coords, float *shuffled_coords,float centroid[3]){ //returns ssq - does not use aligned coords - but outputs aligned coords
float invnat=1.0f/(float)nat;
float sums[4] __attribute__ ((aligned (16)));
float ssq __attribute__ ((aligned (16)));
int nat4=(nat%4)? nat/4+1 : nat/4;
int padded_nat=nat4*4;
int i=0;
int lower_nat4=(nat/4)*4;
//first pass reads and calculates sums
{
__m128 sum0 = _mm_setzero_ps();
__m128 sum1 = _mm_setzero_ps();
__m128 sum2 = _mm_setzero_ps();
__m128 ssq0 = _mm_setzero_ps();
for(;i<lower_nat4*3;i+=12){
__m128 p0 = _mm_loadu_ps(&(coords[i])); // x0y0z0x1
sum0= _mm_add_ps(sum0,p0);
ssq0= _mm_add_ps(ssq0,_mm_mul_ps(p0,p0));
__m128 p1 = _mm_loadu_ps(&(coords[i+4])); // y1z1x2y2
sum1 = _mm_add_ps(sum1,p1);
ssq0 = _mm_add_ps(ssq0,_mm_mul_ps(p1,p1));
__m128 p2 = _mm_loadu_ps(&(coords[i+8])); // z2x3y3z3
sum2= _mm_add_ps(sum2,p2);
ssq0= _mm_add_ps(ssq0,_mm_mul_ps(p2,p2));
}
__m128 t = _mm_shuffle_ps(sum0, sum1, _MM_SHUFFLE(0, 1, 0, 3));
sum0=_mm_add_ps(sum0,_mm_shuffle_ps(t, t, _MM_SHUFFLE(0, 2, 3, 0)));
sum0=_mm_add_ps(sum0,_mm_shuffle_ps(sum1, sum2, _MM_SHUFFLE(0, 0, 3, 2)));
sum0=_mm_add_ps(sum0,_mm_shuffle_ps(sum2, sum2, _MM_SHUFFLE(0, 3, 2, 1)));
_mm_store_ps(sums,sum0);
t=_mm_add_ps(ssq0,_mm_movehl_ps(ssq0,ssq0));
ssq0=_mm_add_ss(t, _mm_shuffle_ps(t, t, 1));
_mm_store_ss(&ssq,ssq0);
}
//finish sums in scalar
//subtract and write out
for(;i<nat*3;i+=3){
sums[0]+=coords[i];
ssq+=coords[i]*coords[i];
sums[1]+=coords[i+1];
ssq+=coords[i+1]*coords[i+1];
sums[2]+=coords[i+2];
ssq+=coords[i+2]*coords[i+2];
}
float invfnat=1.0f/(float)nat;
ssq-=sums[0]*sums[0]*invfnat+sums[1]*sums[1]*invfnat+sums[2]*sums[2]*invfnat;
sums[0]*=invfnat;sums[1]*=invfnat;sums[2]*=invfnat;
//correct ssq for centered coords
//subtract from coords
i=0;
//subtract from coords
__m128 s0 =_mm_load1_ps(sums);
__m128 s1 =_mm_load1_ps(&(sums[1]));
__m128 s2 =_mm_load1_ps(&(sums[2]));
for(;i<lower_nat4*3;i+=12){
__m128 x0y0z0x1 = _mm_loadu_ps(&(coords[i]));
__m128 y1z1x2y2 = _mm_loadu_ps(&(coords[i+4]));
__m128 z2x3y3z3 = _mm_loadu_ps(&(coords[i+8]));
__m128 x2y2x3y3 = _mm_shuffle_ps(y1z1x2y2,z2x3y3z3,_MM_SHUFFLE( 2,1,3,2));
__m128 y0z0y1z1 = _mm_shuffle_ps(x0y0z0x1,y1z1x2y2,_MM_SHUFFLE( 1,0,2,1));
_mm_store_ps (&(shuffled_coords[i]),_mm_sub_ps(_mm_shuffle_ps(x0y0z0x1,x2y2x3y3,_MM_SHUFFLE( 2,0,3,0)),s0));
_mm_store_ps (&(shuffled_coords[i+4]),_mm_sub_ps(_mm_shuffle_ps(y0z0y1z1,x2y2x3y3,_MM_SHUFFLE( 3,1,2,0)),s1));
_mm_store_ps (&(shuffled_coords[i+8]),_mm_sub_ps(_mm_shuffle_ps(y0z0y1z1,z2x3y3z3,_MM_SHUFFLE( 3,0,3,1)),s2));
}
if(nat%4){
int k=i;
for(;i<nat*3;i+=3){
shuffled_coords[k] =coords[i]-sums[0];
shuffled_coords[k+4] =coords[i+1]-sums[1];
shuffled_coords[k+8] =coords[i+2]-sums[2];
k++;
}
}
if(centroid){
centroid[0]=sums[0];
centroid[1]=sums[1];
centroid[2]=sums[2];
}
return(ssq);
}
float center_coords4_sse(int nat, float *coords){//rearranged 4x 4y 4z - must be aligned - uses sse2
float invnat=1.0f/(float)nat;
float out[4] __attribute__ ((aligned (16)));
int nat4=(nat%4)? nat/4+1 : nat/4;
int padded_nat=nat4*4;
int i=0;
__m128 sumx = _mm_setzero_ps();
{
__m128 sumy = _mm_setzero_ps();
__m128 sumz = _mm_setzero_ps();
__m128 ssx = _mm_setzero_ps();
for(;i<padded_nat*3;i+=12){
//load the 4 sets of coords from molecule 2 and then load x,y,z of molecule 1
__m128 mx=_mm_load_ps(&(coords[i]));
sumx=_mm_add_ps(sumx,mx);
ssx=_mm_add_ps(ssx,_mm_mul_ps(mx,mx));
__m128 my=_mm_load_ps(&(coords[i+4]));
sumy=_mm_add_ps(sumy,my);
ssx=_mm_add_ps(ssx,_mm_mul_ps(my,my));
__m128 mz=_mm_load_ps(&(coords[i+8]));
sumz=_mm_add_ps(sumz,mz);
ssx=_mm_add_ps(ssx,_mm_mul_ps(mz,mz));
}
__m128 t1 = _mm_add_ps(_mm_unpacklo_ps(ssx,sumy), _mm_unpackhi_ps(ssx,sumy));
__m128 t2 = _mm_add_ps(_mm_unpacklo_ps(sumx,sumz),_mm_unpackhi_ps(sumx,sumz));
sumx=_mm_add_ps(_mm_unpacklo_ps(t1,t2),_mm_unpackhi_ps(t1,t2));//(ssq,sumx,sumy,sumz)
}
{
__m128 mnat = _mm_set_ss(invnat);
mnat = _mm_shuffle_ps(mnat,mnat,0x0000);
sumx=_mm_mul_ps(mnat,sumx);
_mm_store_ps(out,sumx);;
}
__m128 sumy=_mm_shuffle_ps(sumx,sumx,0x00AA);
__m128 sumz=_mm_shuffle_ps(sumx,sumx,0x00ff);
sumx=_mm_shuffle_ps(sumx,sumx,0x0055);
int pad=padded_nat*3-nat%4*3;
for(i=0;i<pad;i+=12){
__m128 mx=_mm_load_ps(&(coords[i]));
mx=_mm_sub_ps(mx,sumx);
__m128 my=_mm_load_ps(&(coords[i+4]));
my=_mm_sub_ps(my,sumy);
__m128 mz=_mm_load_ps(&(coords[i+8]));
mz=_mm_sub_ps(mz,sumz);
_mm_store_ps(&(coords[i]),mx);
_mm_store_ps(&(coords[i+4]),my);
_mm_store_ps(&(coords[i+8]),mz);
}
//do the last set of 12 in scalar
int j=i/3;
int m=0;
for(;j<nat;j++){
coords[i+m] -=out[1];
coords[i+m+4] -=out[2];
coords[i+m+8] -=out[3];
m++;
}
return(out[0]/invnat-out[1]*out[1]-out[2]*out[2]-out[3]*out[3]);
}
void shuffle_tmscore_coords_soa_sse(int nstructs, int nat, float *coords,float **x,float **y,float **z){
//need to ensure alignment 16 nat must be multiple of 4
int anat= (nat%4) ? (nat/4)*4+4 : nat;
*x=(float*)memalign(16,anat*nstructs*sizeof(float));
*y=(float*)memalign(16,anat*nstructs*sizeof(float));
*z=(float*)memalign(16,anat*nstructs*sizeof(float));
for(int p=0;p<nstructs;p++){
float *const c=&(coords[p*nat*3]);
float *const mx=&((*x)[p*anat]);
float *const my=&((*y)[p*anat]);
float *const mz=&((*z)[p*anat]);
split_coords_sse(nat,c,mx,my,mz);
if(nat != anat){
for(int i=nat;i<anat;i++){
mx[i]=0.0f;
my[i]=0.0f;
mz[i]=0.0f;
}
}
}
}
float tmscore_cpu_soa_sse2(int nat,float *x1, float *y1,float *z1,float *x2,float *y2,float *z2,float bR[3][3], float bt[3],float *rmsd){
int nalign=0,best_nalign=0;
int *ialign=new int[nat];
int *best_align=new int[nat];
float max_score=-1,rms;
float r[16] __attribute__ ((aligned (16)));
//d0
float d0=1.24*pow((nat-15),(1.0/3.0))-1.8;
if(d0< 0.5)d0=0.5;
//d0_search ----->
float d,d0_search=d0;
if(d0_search > 8)d0_search=8;
if(d0_search <4.5)d0_search=4.5;
//iterative parameters ----->
int n_it=20; //maximum number of iterations
int n_init_max=6; //maximum number of L_init
int n_init=0;
int L_ini_min=4;
int L_ini[6];
if(nat < 4) L_ini_min=nat;
int len=nat;
int divisor=1;
while(len > L_ini_min && n_init <5){
L_ini[n_init++]=len;
divisor*=2;
len=nat/divisor;
}
L_ini[n_init++]=4;
if (L_ini[n_init-1] > L_ini_min)L_ini[n_init++]=L_ini_min;;
// find the maximum score starting from local structures superposition
float score; //TM-score
for (int seed=0;seed<n_init;seed++)
{
//find the initial rotation matrix using the initial seed residues
int L_init=L_ini[seed];
for(int istart=0;istart<=nat-L_init;istart++)
{
int nchanges=1;
int nalign=L_init;
{
int m=0;
int n=0;
for(int i=0;i<nat;i++){
if(i>=istart && i<istart+L_init){
ialign[n++]=i;
}
}
}
if(rmsd && !seed)
*rmsd=kabsch_quat_soa_sse2(nalign,ialign,x1,y1,z1,x2,y2,z2,r);
else
kabsch_quat_soa_sse2(nalign,ialign,x1,y1,z1,x2,y2,z2,r);
score_fun_soa_sse(nat, d0, d0_search-1,r,x1,y1,z1,x2,y2,z2,ialign,&nalign,&score);
d=d0_search+1.0f;
if(score > max_score){
max_score=score;
memmove(best_align,ialign,nalign*sizeof(int));
best_nalign=nalign;
}
//extend search from seed
for (int iter=0;iter<n_it && nchanges;iter++){
kabsch_quat_soa_sse2(nalign,ialign,x1,y1,z1,x2,y2,z2,r);
nchanges=score_fun_soa_sse(nat, d0, d,r,x1,y1,z1,x2,y2,z2,ialign,&nalign,&score);
if(score > max_score){
max_score=score;
memmove(best_align,ialign,nalign*sizeof(int));
best_nalign=nalign;
}
}
}
}
//for best frame re-calculate matrix with double precision
double R[3][3],t[3];
double *acoords=new double [best_nalign*6];
for(int k=0;k<best_nalign;k++){
int i=best_align[k];
acoords[6*k] =x1[i];
acoords[6*k+1]=y1[i];
acoords[6*k+2]=z1[i];
acoords[6*k+3]=x2[i];
acoords[6*k+4]=y2[i];
acoords[6*k+5]=z2[i];
}
rmsd_svd(best_nalign,acoords,R, t,0);
if(bR){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
bR[i][j]=R[i][j];
}
if(bt){
for(int i=0;i<3;i++)
bt[i]=t[i];
}
double invd0d0=1.0/(double)(d0*d0);
double dist;
float invdnat=1.0/(double)nat;
double my_score=0;
for(int k=0;k<nat;k++){
double u[3];
double v[3]={x1[k],y1[k],z1[k]};
double w[3]={x2[k],y2[k],z2[k]};
u[0]=t[0]+R[0][0]*v[0]+R[1][0]*v[1]+R[2][0]*v[2]-w[0];
u[1]=t[1]+R[0][1]*v[0]+R[1][1]*v[1]+R[2][1]*v[2]-w[1];
u[2]=t[2]+R[0][2]*v[0]+R[1][2]*v[1]+R[2][2]*v[2]-w[2];
dist=u[0]*u[0]+u[1]*u[1]+u[2]*u[2];
my_score+=1.0/(1.0+dist*invd0d0);
}
delete [] best_align;
delete [] ialign;
delete [] acoords;
return(my_score*invdnat);
}
int score_fun_soa_sse(int nat, float d0, float d, float *r,float *x1, float *y1, float *z1, float *x2, float *y2, float *z2, int *ialign,int *nalign,float *tm_score){
//ialign points to atom number
int k,ncut=0,nchange=0,my_nalign=*nalign,upper_nat= (nat%4)?(nat/4)*4+4 : nat;
float d2=d*d,invfnat=1.0f/(float)nat;
float invd0d0=1.0f/(d0*d0);
float* dist=(float*)memalign(16,upper_nat*sizeof(float));
//keep nmin smallest distances && distances < dtmp
float my_score=LG_score_soa_sse(r,nat,x1,y1,z1,x2,y2,z2,dist,invd0d0);
for(int k=0;k<nat;k++){
if(dist[k]<d2) ncut++;
}
//adjust d until there are at least 3 the same - rare use another routine for this
while(ncut <3){
d2=d*d;
ncut=0;
for(k=0;k<nat;k++)
if(dist[k]<d2) ncut++;
d+=0.5f;
}
ncut=0;
for(k=0;k<nat;k++){
if(dist[k]<d2){
if(ncut < my_nalign && ialign[ncut] == k)ncut++;
else{
nchange=1;
ialign[ncut++]=k;
}
}
}
free (dist);
*tm_score=my_score*invfnat;
if(!nchange)return(0);
*nalign=ncut;
return(1);
}
void split_coords_sse(int nat, float *coords, float *x, float *y,float *z){ //returns ssq - use aligned coords - do once
int i=0,k=0;
int lower_nat4=(nat/4)*4;
for(;i<lower_nat4*3;i+=12){
__m128 x0y0z0x1 = _mm_loadu_ps(&(coords[i]));
__m128 y1z1x2y2 = _mm_loadu_ps(&(coords[i+4]));
__m128 z2x3y3z3 = _mm_loadu_ps(&(coords[i+8]));
__m128 x2y2x3y3 = _mm_shuffle_ps(y1z1x2y2,z2x3y3z3,_MM_SHUFFLE( 2,1,3,2));
__m128 y0z0y1z1 = _mm_shuffle_ps(x0y0z0x1,y1z1x2y2,_MM_SHUFFLE( 1,0,2,1));
_mm_storeu_ps (&x[k],_mm_shuffle_ps(x0y0z0x1,x2y2x3y3,_MM_SHUFFLE( 2,0,3,0)));
_mm_storeu_ps (&y[k],_mm_shuffle_ps(y0z0y1z1,x2y2x3y3,_MM_SHUFFLE( 3,1,2,0)));
_mm_storeu_ps (&z[k],_mm_shuffle_ps(y0z0y1z1,z2x3y3z3,_MM_SHUFFLE( 3,0,3,1)));
k+=4;
}
//do the last set in scalar
i=lower_nat4;
for(;i<nat;i++){
int p=i*3;
x[k] =coords[p];
y[k] =coords[p+1];
z[k] =coords[p+2];
k++;;
}
}
float kabsch_quat_soa_sse2(int nat, int *map, float *x1, float *y1,float *z1, float *x2,float *y2, float *z2,float *r){
int nat4=(nat%4) ? nat/4+1 : nat/4;
float center1[3],center2[3];
float u[3][3];
float *mem = (float*)memalign(16,6*nat4*4*sizeof(float));
memset(mem,0,nat4*24*sizeof(float));
float* c1x= mem;
float* c1y= &mem[nat4*4];
float* c1z= &mem[nat4*8];
float* c2x= &mem[nat4*12];
float* c2y= &mem[nat4*16];
float* c2z= &mem[nat4*20];
for(int i=0;i<nat;i++){
int n=map[i];
c1x[i]=x1[n];
c1y[i]=y1[n];
c1z[i]=z1[n];
c2x[i]=x2[n];
c2y[i]=y2[n];
c2z[i]=z2[n];
}
float ssq=coords_sum_ssq_xyz_sse2(nat,c1x,c1y,c1z,center1);
ssq+=coords_sum_ssq_xyz_sse2(nat,c2x,c2y,c2z,center2);
float rms=rmsd_sse2_matrix_xyz(nat,c1x,c1y,c1z,c2x,c2y,c2z,center1,center2,(double)ssq,u);
float rr[16] __attribute__ ((aligned (16)))=