Skip to content

maxron84/Branchless-Programming-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Branchless Programming Examples

📚 Educational Repository — Learn branchless programming techniques through practical examples across multiple languages.

Demonstrates branchless programming techniques using bit manipulation to avoid conditional branches. Each example is fully commented to explain the underlying concepts.

Techniques Shown

  • Sign detection - Extract sign bit via right shift
  • Branchless minimum - Use sign bit masking to select smaller value

Files

File Language Description
program.c C Basic examples
program.js JavaScript Basic examples
program.py Python Basic examples
program.sh Bash Basic examples
program.sql MariaDB SQL Basic examples
advanced/calculator.py Python Function table lookup
advanced/sorting.py Python Sorting networks, clamp, abs
advanced/image_processing.py Python Pixel ops: threshold, blend, contrast
advanced/state_machine.py Python Table-driven state transitions

Why Branchless?

Modern CPUs use branch prediction to speculatively execute instructions before knowing which path a conditional will take. When predictions fail, the pipeline must be flushed and restarted—costing 10-20+ cycles.

Benefits:

  • No misprediction penalties - Arithmetic always executes the same way
  • Better pipelining - Instructions flow without stalls
  • SIMD-friendly - Enables vectorization across multiple data points
  • Constant-time execution - Useful in cryptography to prevent timing attacks

Trade-offs:

  • Code is harder to read and maintain
  • May compute unnecessary values
  • Not always faster—profile before optimizing

About

Learn branchless programming techniques through practical examples across multiple languages.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published