From 4badf2fcbf4e1fa8906bd2b28350b54371223fe7 Mon Sep 17 00:00:00 2001 From: Heng Li Date: Tue, 8 Aug 2017 11:29:46 -0400 Subject: [PATCH] avoid wasted memory allocation for backtrack --- ksw2_extd2_sse.c | 5 ++++- ksw2_extz2_sse.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ksw2_extd2_sse.c b/ksw2_extd2_sse.c index e0f5337..c7f5843 100644 --- a/ksw2_extd2_sse.c +++ b/ksw2_extd2_sse.c @@ -1,5 +1,6 @@ #include #include +#include #include "ksw2.h" #ifdef __SSE2__ @@ -66,7 +67,8 @@ void ksw_extd2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uin if (w < 0) w = tlen > qlen? tlen : qlen; wl = wr = w; tlen_ = (tlen + 15) / 16; - n_col_ = ((w + 1 < tlen? (w + 1 < qlen? w + 1 : qlen): tlen) + 15) / 16 + 1; + n_col_ = qlen < tlen? qlen : tlen; + n_col_ = ((n_col_ < w + 1? n_col_ : w + 1) + 15) / 16 + 1; qlen_ = (qlen + 15) / 16; for (t = 1, max_sc = mat[0], min_sc = mat[1]; t < m * m; ++t) { max_sc = max_sc > mat[t]? max_sc : mat[t]; @@ -161,6 +163,7 @@ void ksw_extd2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uin x21_ = _mm_cvtsi32_si128((uint8_t)x21); v1_ = _mm_cvtsi32_si128((uint8_t)v1); st_ = st / 16, en_ = en / 16; + assert(en_ - st_ + 1 <= n_col_); if (!with_cigar) { // score only for (t = st_; t <= en_; ++t) { __m128i z, a, b, a2, b2, xt1, x2t1, vt1, ut, tmp; diff --git a/ksw2_extz2_sse.c b/ksw2_extz2_sse.c index d665c6f..04669b9 100644 --- a/ksw2_extz2_sse.c +++ b/ksw2_extz2_sse.c @@ -1,4 +1,5 @@ #include +#include #include "ksw2.h" #ifdef __SSE2__ @@ -58,7 +59,8 @@ void ksw_extz2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uin if (w < 0) w = tlen > qlen? tlen : qlen; wl = wr = w; tlen_ = (tlen + 15) / 16; - n_col_ = ((w + 1 < tlen? (w + 1 < qlen? w + 1 : qlen): tlen) + 15) / 16 + 1; + n_col_ = qlen < tlen? qlen : tlen; + n_col_ = ((n_col_ < w + 1? n_col_ : w + 1) + 15) / 16 + 1; qlen_ = (qlen + 15) / 16; for (t = 1, max_sc = mat[0], min_sc = mat[1]; t < m * m; ++t) { max_sc = max_sc > mat[t]? max_sc : mat[t]; @@ -130,6 +132,7 @@ void ksw_extz2_sse(void *km, int qlen, const uint8_t *query, int tlen, const uin x1_ = _mm_cvtsi32_si128(x1); v1_ = _mm_cvtsi32_si128(v1); st_ = st / 16, en_ = en / 16; + assert(en_ - st_ + 1 <= n_col_); if (!with_cigar) { // score only for (t = st_; t <= en_; ++t) { __m128i z, a, b, xt1, vt1, ut, tmp;