#include "config.h"
#include <stdint.h>
#include <stdlib.h>
#include "parasail.h"
#include "parasail/memory.h"
#define NEG_INF_32 (INT32_MIN/2)
#define MAX(a,b) ((a)>(b)?(a):(b))
#ifdef PARASAIL_TABLE
#define ENAME parasail_sw_table
#else
#ifdef PARASAIL_ROWCOL
#define ENAME parasail_sw_rowcol
#else
#define ENAME parasail_sw
#endif
#endif
parasail_result_t* ENAME(
const char * const restrict _s1, const int _s1Len,
const char * const restrict _s2, const int s2Len,
const int open, const int gap, const parasail_matrix_t *matrix)
{
parasail_result_t *result = NULL;
int * restrict s1 = NULL;
int * restrict s2 = NULL;
int * restrict H = NULL;
int * restrict F = NULL;
int s1Len = 0;
int i = 0;
int j = 0;
int score = 0;
int end_query = 0;
int end_ref = 0;
PARASAIL_CHECK_NULL(_s2);
PARASAIL_CHECK_GT0(s2Len);
PARASAIL_CHECK_GE0(open);
PARASAIL_CHECK_GE0(gap);
PARASAIL_CHECK_NULL(matrix);
if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
PARASAIL_CHECK_NULL(_s1);
PARASAIL_CHECK_GT0(_s1Len);
}
s1Len = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? _s1Len : matrix->length;
i = 0;
j = 0;
score = NEG_INF_32;
end_query = s1Len;
end_ref = s2Len;
#ifdef PARASAIL_TABLE
result = parasail_result_new_table1(s1Len, s2Len);
#else
#ifdef PARASAIL_ROWCOL
result = parasail_result_new_rowcol1(s1Len, s2Len);
#else
result = parasail_result_new();
#endif
#endif
if (!result) return NULL;
result->flag |= PARASAIL_FLAG_SW | PARASAIL_FLAG_NOVEC
| PARASAIL_FLAG_BITS_INT | PARASAIL_FLAG_LANES_1;
#ifdef PARASAIL_TABLE
result->flag |= PARASAIL_FLAG_TABLE;
#endif
#ifdef PARASAIL_ROWCOL
result->flag |= PARASAIL_FLAG_ROWCOL;
#endif
s2 = parasail_memalign_int(16, s2Len);
H = parasail_memalign_int(16, s2Len+1);
F = parasail_memalign_int(16, s2Len+1);
if (!s2) return NULL;
if (!H) return NULL;
if (!F) return NULL;
if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
s1 = parasail_memalign_int(16, s1Len);
if (!s1) return NULL;
for (i=0; i<s1Len; ++i) {
s1[i] = matrix->mapper[(unsigned char)_s1[i]];
}
}
for (j=0; j<s2Len; ++j) {
s2[j] = matrix->mapper[(unsigned char)_s2[j]];
}
H[0] = 0;
F[0] = NEG_INF_32;
for (j=1; j<=s2Len; ++j) {
H[j] = 0;
F[j] = NEG_INF_32;
}
for (i=1; i<=s1Len; ++i) {
const int * const restrict matrow =
matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ?
&matrix->matrix[matrix->size*s1[i-1]] :
&matrix->matrix[matrix->size*(i-1)];
int NH = H[0];
int WH = 0;
int E = NEG_INF_32;
H[0] = WH;
for (j=1; j<=s2Len; ++j) {
int H_dag;
int E_opn;
int E_ext;
int F_opn;
int F_ext;
int NWH = NH;
NH = H[j];
F_opn = NH - open;
F_ext = F[j] - gap;
F[j] = MAX(F_opn, F_ext);
E_opn = WH - open;
E_ext = E - gap;
E = MAX(E_opn, E_ext);
H_dag = NWH + matrow[s2[j-1]];
WH = MAX(H_dag, 0);
WH = MAX(WH, E);
WH = MAX(WH, F[j]);
H[j] = WH;
if (WH > score) {
end_query = i-1;
end_ref = j-1;
}
else if (score == WH && j-1 < end_ref) {
end_query = i-1;
end_ref = j-1;
}
score = MAX(score,WH);
#ifdef PARASAIL_TABLE
result->tables->score_table[1LL*(i-1)*s2Len + (j-1)] = WH;
#endif
}
#ifdef PARASAIL_ROWCOL
result->rowcols->score_col[i-1] = WH;
#endif
}
#ifdef PARASAIL_ROWCOL
for (j=1; j<=s2Len; ++j) {
result->rowcols->score_row[j-1] = H[j];
}
#endif
result->score = score;
result->end_query = end_query;
result->end_ref = end_ref;
parasail_free(F);
parasail_free(H);
parasail_free(s2);
if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
parasail_free(s1);
}
return result;
}