<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb" -->

---
title: Eliminating branches in C++ loops | daily.dev
description: A deep technical walkthrough on removing branches from performance-critical C++ loops, using ASCII lowercase classification as the running example. Covers...
canonical: https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Eliminating branches in C++ loops | daily.dev
og:description: A deep technical walkthrough on removing branches from performance-critical C++ loops, using ASCII lowercase classification as the running example. Covers...
og:url: https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb
og:image: https://api.daily.dev/og/posts/vX3JCKkCb.png
og:image:alt: Eliminating branches in C++ loops
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Eliminating branches in C++ loops

**[Yagiz Nizipli](https://daily.dev/sources/yagiz)** · 18 min read · 0 upvotes · 0 comments

## Summary

A deep technical walkthrough on removing branches from performance-critical C++ loops, using ASCII lowercase classification as the running example. Covers replacing early-return branches with bitwise AND/OR accumulation, a wraparound subtract-and-compare trick for single character ranges, 256-byte lookup tables for unions of ranges (like hex digits), SWAR (SIMD within a register) for 64-bit-at-a-time processing, and ARM NEON intrinsics for 16-byte vectorized comparisons. Benchmarks on an Intel Xeon show branchy early-return at 4.0 GB/s versus SSE2 wraparound at 56.0 GB/s and AVX2 at 63.9 GB/s. The same techniques are then applied to UTF-16 validity checking, including a NEON approach exploiting interleaved loads to check 32 bytes of input via a single 16-byte register, reaching roughly 8x speedup over scalar code on Apple M2.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.yagiz.co/eliminating-branches-in-cpp-loops>

## Questions this post answers

### How can I check if a byte is an ASCII lowercase letter using only one comparison instead of two range checks?

Subtract 'a' from the byte cast to unsigned char, then compare the result against 25 using less-than-or-equal. Because unsigned subtraction wraps modulo 256, bytes below 'a' underflow into the high end of the range and fail the same test as bytes above 'z', so a single subtract-and-compare replaces two comparisons and works well with autovectorization.

_Developers optimizing string parsing hot loops can find these branchless C++ patterns curated on daily.dev._

### Why is a 256-byte lookup table slower than a branchless loop for classifying ASCII lowercase characters in C++?

On an Intel Xeon with GCC 13.3 using -O3 -march=native, a 256-byte table scanning a 1 MiB all-lowercase buffer ran at 3.1 GB/s, slower than even the naive branchy early-return loop at 4.0 GB/s, because the table lookup is bound by load latency. A wraparound subtract-and-compare autovectorized to 14.2 GB/s, and SSE2 wraparound reached 56.0 GB/s, so tables only win for unions of ranges like hex digits, not single intervals like a-z.

_Anyone benchmarking classification strategies in C++ can track these performance trade-offs via daily.dev._

### How much faster is a fast ARM NEON UTF-16 validation approach compared to regular scalar C code?

The fast ARM NEON implementation runs about 8 times faster than regular scalar C code, reaching roughly 13 GB/s versus 1.7 GB/s on an Apple M2 using LLVM 16, when benchmarked on a 10-million space-character string. It works by loading interleaved bytes so the most significant byte register alone can detect surrogates, letting one 16-byte NEON register validate 32 bytes of UTF-16 input.

_Engineers validating UTF-16 at scale can follow NEON-based performance techniques like this through daily.dev._

## Similar posts on daily.dev

- [Don’t stop early: Case-folding source code at memory speed](https://daily.dev/posts/don-t-stop-early-case-folding-source-code-at-memory-speed-0kmr0gljo) · GitHub Blog · 3 upvotes · 0 comments
- [Branches in branch-free code](https://daily.dev/posts/branches-in-branch-free-code-trw8zoxp3) · Lobsters · 0 upvotes · 0 comments
- [Switch Lowering in GCC](https://daily.dev/posts/switch-lowering-in-gcc-cg4bme6jx) · Hacker News · 1 upvotes · 0 comments
- [Eight bytes in one register](https://daily.dev/posts/eight-bytes-in-one-register-mloumcpw2) · Yagiz Nizipli · 2 upvotes · 0 comments
- [C\+\+ Performance Improvements in MSVC Build Tools v14.51](https://daily.dev/posts/c-performance-improvements-in-msvc-build-tools-v14-51-tagncfkv0) · DevBlogs · 1 upvotes · 0 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#c++](https://daily.dev/tags/c++)

[View this post on daily.dev](https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Eliminating branches in C++ loops","url":"https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb"},"datePublished":"2026-09-13T19:57:45.252Z","dateModified":"2026-09-13T20:01:57.162Z","description":"A deep technical walkthrough on removing branches from performance-critical C++ loops, using ASCII lowercase classification as the running example. Covers...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/f42d0e16335fd49975fa01c0ec3781e3?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/f42d0e16335fd49975fa01c0ec3781e3?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Yagiz Nizipli","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Yagiz Nizipli","logo":"https://media.daily.dev/image/upload/s--9Cscmaqk--/f_auto,q_auto/v1789329427/logos/yagiz?_a=BAMAMicg0","url":"https://daily.dev/sources/yagiz"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,c++","timeRequired":"PT18M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Yagiz Nizipli","item":"https://daily.dev/sources/yagiz"},{"@type":"ListItem","position":3,"name":"Eliminating branches in C++ loops"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/eliminating-branches-in-c-loops-vx3jckkcb#faq","mainEntity":[{"@type":"Question","name":"How can I check if a byte is an ASCII lowercase letter using only one comparison instead of two range checks?","acceptedAnswer":{"@type":"Answer","text":"Subtract 'a' from the byte cast to unsigned char, then compare the result against 25 using less-than-or-equal. Because unsigned subtraction wraps modulo 256, bytes below 'a' underflow into the high end of the range and fail the same test as bytes above 'z', so a single subtract-and-compare replaces two comparisons and works well with autovectorization. Developers optimizing string parsing hot loops can find these branchless C++ patterns curated on daily.dev."}},{"@type":"Question","name":"Why is a 256-byte lookup table slower than a branchless loop for classifying ASCII lowercase characters in C++?","acceptedAnswer":{"@type":"Answer","text":"On an Intel Xeon with GCC 13.3 using -O3 -march=native, a 256-byte table scanning a 1 MiB all-lowercase buffer ran at 3.1 GB/s, slower than even the naive branchy early-return loop at 4.0 GB/s, because the table lookup is bound by load latency. A wraparound subtract-and-compare autovectorized to 14.2 GB/s, and SSE2 wraparound reached 56.0 GB/s, so tables only win for unions of ranges like hex digits, not single intervals like a-z. Anyone benchmarking classification strategies in C++ can track these performance trade-offs via daily.dev."}},{"@type":"Question","name":"How much faster is a fast ARM NEON UTF-16 validation approach compared to regular scalar C code?","acceptedAnswer":{"@type":"Answer","text":"The fast ARM NEON implementation runs about 8 times faster than regular scalar C code, reaching roughly 13 GB/s versus 1.7 GB/s on an Apple M2 using LLVM 16, when benchmarked on a 10-million space-character string. It works by loading interleaved bytes so the most significant byte register alone can detect surrogates, letting one 16-byte NEON register validate 32 bytes of UTF-16 input. Engineers validating UTF-16 at scale can follow NEON-based performance techniques like this through daily.dev."}}]}
```

