SlideShare a Scribd company logo
1 of 19
In the Name of
Performance
Sam Coleman
Who am I?
Who am I?
What is this not?
• Not about overall architecture
What is this not?
• Not about rescuing an entire codebase
What is this, then?
• Let’s look at something gross
• Let’s understand it
• Let’s never do it unless it needs to be done
• …“Performance”
Duff’s Device
• Live coding
• Please excuse the mess
Duff’s Device
• Why?
• Don’t do this.
• …unless you have to.
• memcpy available almost everywhere.
Bit Counting
• aka “population count”, aka “Hamming Weight”
But First, Bit Operations
• Bitwise: &, >>
• Masking: “covering up” bits we don’t care about
h g f e d c b a
& 0 0 1 1 1 1 0 0
0 0 f e d c 0 0
Bit Counting
• Live coding
Bit Counting
h g f e d c b a
- h g f e d c b  
= h (g ^ h) f (e ^ f) d (c ^ d) b (a ^ b)
• The left column retains its original value.
• The right column is “on” only if either d or c were on.
• So across both columns, possible results are “10”, “01”, or “00”.
• …you may recognize these as “2”, “1”, or “0”.
Bit Counting
+
h g f e d c b a
h g d c
Bit Counting
• Sum of two 2-bit numbers can require 3 bits
• Need to clean (mask) the input so we know which
bits of output are relevant
• Result is clean, 4-bit integers
+
h g f e d c b a
i h g e d c
Bit Counting
• Our “4-bit” inputs will only have up to 3 bits in use
(to store “4”) so the sum will take at most 4 bits
• Only need to mask the output to get a clean, 8-bit
result – saves an operation vs. masking both inputs
+
p o n m l k j i h g f e d c b a
p o n m h g f e
Bit Counting
- - - - p o n m - - - - l k j i - - - - h g f e - - - - d c b a
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
- - - - p o n m - - - - l k j i - - - - h g f e - - - - d c b a
- - - - l k j i - - - - h g f e - - - - d c b a
- - - - h g f e - - - - d c b a
- - - - d c b a
… >> 24
Bit Counting
• Why?
• Don’t do this.
• …unless you have to.
• __builtin_popcount available almost everywhere.
Concluding Remarks
• Old code can be a pain
• Embedded code can be a pain
• Progress can also be pain
• Boyscout rule: leave the campground cleaner than
you found it.
• Caveat: Don’t clean the campground without
understanding it!
?

More Related Content

Viewers also liked

HRIS Compensation and Benefit Analyst fix
HRIS Compensation and Benefit Analyst fixHRIS Compensation and Benefit Analyst fix
HRIS Compensation and Benefit Analyst fixLipi Fahrizal
 
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIAL
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIALTHE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIAL
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIALpaperpublications3
 
Alcatel-Lucent TN831
Alcatel-Lucent TN831Alcatel-Lucent TN831
Alcatel-Lucent TN831savomir
 
A filosofia da natureza
A filosofia da naturezaA filosofia da natureza
A filosofia da naturezaArmin Caldas
 
Basic principles in impression making 3
Basic principles in impression making  3Basic principles in impression making  3
Basic principles in impression making 3Prosth Ozone
 
Carrier Access 8616-2-TV-PR
Carrier Access 8616-2-TV-PRCarrier Access 8616-2-TV-PR
Carrier Access 8616-2-TV-PRsavomir
 
Escritos milenares
Escritos milenaresEscritos milenares
Escritos milenaresArmin Caldas
 
Codigo do trabalho-fiscalização da constitucionalidade
Codigo do trabalho-fiscalização da constitucionalidadeCodigo do trabalho-fiscalização da constitucionalidade
Codigo do trabalho-fiscalização da constitucionalidadeArmin Caldas
 

Viewers also liked (13)

Redes
RedesRedes
Redes
 
HRIS Compensation and Benefit Analyst fix
HRIS Compensation and Benefit Analyst fixHRIS Compensation and Benefit Analyst fix
HRIS Compensation and Benefit Analyst fix
 
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIAL
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIALTHE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIAL
THE ORIGIN OF THE POST OF THE INTERNATIONAL PUBLIC OFFICIAL
 
Alcatel-Lucent TN831
Alcatel-Lucent TN831Alcatel-Lucent TN831
Alcatel-Lucent TN831
 
A filosofia da natureza
A filosofia da naturezaA filosofia da natureza
A filosofia da natureza
 
Basic principles in impression making 3
Basic principles in impression making  3Basic principles in impression making  3
Basic principles in impression making 3
 
Manuel rojas
Manuel rojasManuel rojas
Manuel rojas
 
Carrier Access 8616-2-TV-PR
Carrier Access 8616-2-TV-PRCarrier Access 8616-2-TV-PR
Carrier Access 8616-2-TV-PR
 
Escritos milenares
Escritos milenaresEscritos milenares
Escritos milenares
 
Christian Esteban Proaño por Cotacachi
Christian Esteban Proaño por CotacachiChristian Esteban Proaño por Cotacachi
Christian Esteban Proaño por Cotacachi
 
14 lecture outline
14 lecture outline14 lecture outline
14 lecture outline
 
Resume Working Summary
Resume Working SummaryResume Working Summary
Resume Working Summary
 
Codigo do trabalho-fiscalização da constitucionalidade
Codigo do trabalho-fiscalização da constitucionalidadeCodigo do trabalho-fiscalização da constitucionalidade
Codigo do trabalho-fiscalização da constitucionalidade
 

Similar to In the Name of Performance

Cranking Floating Point Performance To 11 On The iPhone
Cranking Floating Point Performance To 11 On The iPhoneCranking Floating Point Performance To 11 On The iPhone
Cranking Floating Point Performance To 11 On The iPhoneNoel Llopis
 
Cranking Floating Point Performance Up To 11
Cranking Floating Point Performance Up To 11Cranking Floating Point Performance Up To 11
Cranking Floating Point Performance Up To 11John Wilker
 
C Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer CentreC Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer Centrejatin batra
 
Digital electronics logic design complete notes.pdf
Digital electronics logic design complete notes.pdfDigital electronics logic design complete notes.pdf
Digital electronics logic design complete notes.pdf022BELEHemant
 
Bitwise Operations(1).pdf
Bitwise Operations(1).pdfBitwise Operations(1).pdf
Bitwise Operations(1).pdfDalvinCalvin
 
Count-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdfCount-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdfStephen Lorello
 
#GDC15 Code Clinic
#GDC15 Code Clinic#GDC15 Code Clinic
#GDC15 Code ClinicMike Acton
 
lecture8_Cuong.ppt
lecture8_Cuong.pptlecture8_Cuong.ppt
lecture8_Cuong.pptHongV34104
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilPôle Systematic Paris-Region
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Romain Dorgueil
 
Cloud flare jgc bigo meetup rolling hashes
Cloud flare jgc   bigo meetup rolling hashesCloud flare jgc   bigo meetup rolling hashes
Cloud flare jgc bigo meetup rolling hashesCloudflare
 
Lenguaje python en I+D. Numpy, Sympy y Pandas
Lenguaje python en I+D. Numpy, Sympy y PandasLenguaje python en I+D. Numpy, Sympy y Pandas
Lenguaje python en I+D. Numpy, Sympy y PandasJosé Luis Muñoz Meza
 
C言語静的解析ツールと Ruby 1.9 trunk
C言語静的解析ツールと Ruby 1.9 trunkC言語静的解析ツールと Ruby 1.9 trunk
C言語静的解析ツールと Ruby 1.9 trunkikegami__
 
Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks Sperasoft
 
Code and memory optimization tricks
Code and memory optimization tricksCode and memory optimization tricks
Code and memory optimization tricksDevGAMM Conference
 

Similar to In the Name of Performance (20)

Cranking Floating Point Performance To 11 On The iPhone
Cranking Floating Point Performance To 11 On The iPhoneCranking Floating Point Performance To 11 On The iPhone
Cranking Floating Point Performance To 11 On The iPhone
 
Dynamic Binary Instrumentation
Dynamic Binary Instrumentation	Dynamic Binary Instrumentation
Dynamic Binary Instrumentation
 
Data type
Data typeData type
Data type
 
Cranking Floating Point Performance Up To 11
Cranking Floating Point Performance Up To 11Cranking Floating Point Performance Up To 11
Cranking Floating Point Performance Up To 11
 
C Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer CentreC Programming Training in Ambala ! Batra Computer Centre
C Programming Training in Ambala ! Batra Computer Centre
 
Digital electronics logic design complete notes.pdf
Digital electronics logic design complete notes.pdfDigital electronics logic design complete notes.pdf
Digital electronics logic design complete notes.pdf
 
Bitwise Operations(1).pdf
Bitwise Operations(1).pdfBitwise Operations(1).pdf
Bitwise Operations(1).pdf
 
Count-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdfCount-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdf
 
#GDC15 Code Clinic
#GDC15 Code Clinic#GDC15 Code Clinic
#GDC15 Code Clinic
 
lecture8_Cuong.ppt
lecture8_Cuong.pptlecture8_Cuong.ppt
lecture8_Cuong.ppt
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
 
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017Simple ETL in python 3.5+ with Bonobo - PyParis 2017
Simple ETL in python 3.5+ with Bonobo - PyParis 2017
 
There and Back Again
There and Back AgainThere and Back Again
There and Back Again
 
Cloud flare jgc bigo meetup rolling hashes
Cloud flare jgc   bigo meetup rolling hashesCloud flare jgc   bigo meetup rolling hashes
Cloud flare jgc bigo meetup rolling hashes
 
Lenguaje python en I+D. Numpy, Sympy y Pandas
Lenguaje python en I+D. Numpy, Sympy y PandasLenguaje python en I+D. Numpy, Sympy y Pandas
Lenguaje python en I+D. Numpy, Sympy y Pandas
 
C言語静的解析ツールと Ruby 1.9 trunk
C言語静的解析ツールと Ruby 1.9 trunkC言語静的解析ツールと Ruby 1.9 trunk
C言語静的解析ツールと Ruby 1.9 trunk
 
Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks Code and Memory Optimisation Tricks
Code and Memory Optimisation Tricks
 
Code and memory optimization tricks
Code and memory optimization tricksCode and memory optimization tricks
Code and memory optimization tricks
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

In the Name of Performance

  • 1. In the Name of Performance Sam Coleman
  • 4. What is this not? • Not about overall architecture
  • 5. What is this not? • Not about rescuing an entire codebase
  • 6. What is this, then? • Let’s look at something gross • Let’s understand it • Let’s never do it unless it needs to be done • …“Performance”
  • 7. Duff’s Device • Live coding • Please excuse the mess
  • 8. Duff’s Device • Why? • Don’t do this. • …unless you have to. • memcpy available almost everywhere.
  • 9. Bit Counting • aka “population count”, aka “Hamming Weight”
  • 10. But First, Bit Operations • Bitwise: &, >> • Masking: “covering up” bits we don’t care about h g f e d c b a & 0 0 1 1 1 1 0 0 0 0 f e d c 0 0
  • 12. Bit Counting h g f e d c b a - h g f e d c b = h (g ^ h) f (e ^ f) d (c ^ d) b (a ^ b) • The left column retains its original value. • The right column is “on” only if either d or c were on. • So across both columns, possible results are “10”, “01”, or “00”. • …you may recognize these as “2”, “1”, or “0”.
  • 13. Bit Counting + h g f e d c b a h g d c
  • 14. Bit Counting • Sum of two 2-bit numbers can require 3 bits • Need to clean (mask) the input so we know which bits of output are relevant • Result is clean, 4-bit integers + h g f e d c b a i h g e d c
  • 15. Bit Counting • Our “4-bit” inputs will only have up to 3 bits in use (to store “4”) so the sum will take at most 4 bits • Only need to mask the output to get a clean, 8-bit result – saves an operation vs. masking both inputs + p o n m l k j i h g f e d c b a p o n m h g f e
  • 16. Bit Counting - - - - p o n m - - - - l k j i - - - - h g f e - - - - d c b a 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 - - - - p o n m - - - - l k j i - - - - h g f e - - - - d c b a - - - - l k j i - - - - h g f e - - - - d c b a - - - - h g f e - - - - d c b a - - - - d c b a … >> 24
  • 17. Bit Counting • Why? • Don’t do this. • …unless you have to. • __builtin_popcount available almost everywhere.
  • 18. Concluding Remarks • Old code can be a pain • Embedded code can be a pain • Progress can also be pain • Boyscout rule: leave the campground cleaner than you found it. • Caveat: Don’t clean the campground without understanding it!
  • 19. ?