Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable optimisation level -O3 for SAM QUAL+33 formatting. #1679

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions htslib/hts_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ DEALINGS IN THE SOFTWARE. */
#define HTS_NORETURN
#endif

// Enable optimisation level 3, especially for gcc. To be used
// where we want to force vectorisation in hot loops and the default -O2
// just doesn't cut it.
#if HTS_COMPILER_HAS(optimize) || HTS_GCC_AT_LEAST(4,4)
#define HTS_OPT3 __attribute__((optimize("O3")))
#else
#define HTS_OPT3
#endif

// GCC introduced warn_unused_result in 3.4 but added -Wno-unused-result later
#if HTS_COMPILER_HAS(__warn_unused_result__) || HTS_GCC_AT_LEAST(4,5)
#define HTS_RESULT_USED __attribute__ ((__warn_unused_result__))
Expand Down
15 changes: 11 additions & 4 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -4217,6 +4217,15 @@ int sam_read1(htsFile *fp, sam_hdr_t *h, bam1_t *b)
return pass_filter < 0 ? -2 : ret;
}

// With gcc, -O3 or -ftree-loop-vectorize is really key here as otherwise
// this code isn't vectorised and runs far slower than is necessary (even
// with the restrict keyword being used).
static inline void HTS_OPT3
add33(uint8_t *a, const uint8_t * b, int32_t len) {
uint32_t i;
for (i = 0; i < len; i++)
a[i] = b[i]+33;
}

static int sam_format1_append(const bam_hdr_t *h, const bam1_t *b, kstring_t *str)
{
Expand Down Expand Up @@ -4267,10 +4276,8 @@ static int sam_format1_append(const bam_hdr_t *h, const bam1_t *b, kstring_t *st
if (s[0] == 0xff) {
cp[i++] = '*';
} else {
// local copy of c->l_qseq to aid unrolling
uint32_t lqseq = c->l_qseq;
for (i = 0; i < lqseq; ++i)
cp[i]=s[i]+33;
add33((uint8_t *)cp, s, c->l_qseq); // cp[i] = s[i]+33;
i = c->l_qseq;
}
cp[i] = 0;
cp += i;
Expand Down