Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 54255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:11:29+00:00 2026-05-10T17:11:29+00:00

What is the best (cleanest, most efficient) way to write saturating addition in C?

  • 0

What is the best (cleanest, most efficient) way to write saturating addition in C?

The function or macro should add two unsigned inputs (need both 16- and 32-bit versions) and return all-bits-one (0xFFFF or 0xFFFFFFFF) if the sum overflows.

Target is x86 and ARM using gcc (4.1.2) and Visual Studio (for simulation only, so a fallback implementation is OK there).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:11:30+00:00Added an answer on May 10, 2026 at 5:11 pm

    You probably want portable C code here, which your compiler will turn into proper ARM assembly. ARM has conditional moves, and these can be conditional on overflow. The algorithm then becomes: add and conditionally set the destination to unsigned(-1), if overflow was detected.

    uint16_t add16(uint16_t a, uint16_t b) {   uint16_t c = a + b;   if (c < a)  /* Can only happen due to overflow */     c = -1;   return c; } 

    Note that this differs from the other algorithms in that it corrects overflow, instead of relying on another calculation to detect overflow.

    x86-64 clang 3.7 -O3 output for adds32: significantly better than any other answer:

    add     edi, esi mov     eax, -1 cmovae  eax, edi ret 

    ARMv7: gcc 4.8 -O3 -mcpu=cortex-a15 -fverbose-asm output for adds32:

    adds    r0, r0, r1      @ c, a, b it      cs movcs   r0, #-1         @ conditional-move bx      lr 

    16bit: still doesn’t use ARM’s unsigned-saturating add instruction (UADD16)

    add     r1, r1, r0        @ tmp114, a movw    r3, #65535      @ tmp116, uxth    r1, r1  @ c, tmp114 cmp     r0, r1    @ a, c ite     ls        @ movls   r0, r1        @,, c movhi   r0, r3        @,, tmp116 bx      lr  @ 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering what's the best, the cleanest and the most simply way to work
I am curious as to the best practices/most efficient way to structure my data.
The progress bar is stuck at 100%. Any suggestions for the best/cleanest way torwards
What's the cleanest way to add a prefix to every URL in CakePHP, like
What's the cleanest/best way in C# to convert something like 400AMP or 6M to
Casting an Iterator<Object> to a Set<String> What would be the cleanest/best practice way?
What is the cleanest way to pass the result of this recursive function back
I was wondering what do you think would be the best and cleanest way
When using the Ti.UI.iPad.SplitWindow what is the best(cleanest looking) way to update the detailView
What is the simplest/cleanest/easiest/best way to handle Null value of a Strongly Typed DataSet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.