Skip to content

Instantly share code, notes, and snippets.

@lorsi96
Last active June 15, 2021 07:37
Show Gist options
  • Save lorsi96/e39aad70e93f7170dd0e8c6fe5ba0e35 to your computer and use it in GitHub Desktop.
Save lorsi96/e39aad70e93f7170dd0e8c6fe5ba0e35 to your computer and use it in GitHub Desktop.
C Saturating Signed and Unsigned Functions
/**
* Copyright 2021 Lucas Orsi (lorsi)
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#include <stdint.h>
#define DECL_ADD(XX) uint##XX##_t add##XX (uint##XX##_t a, uint##XX##_t b) {\
uint##XX##_t c = a + b; \
return c < a ? UINT##XX##_MAX : c;};
#define DECL_SUB(XX) uint##XX## _t sub##XX (uint##XX##_t a, uint##XX##_t b) {\
uint##XX##_t c = a - b; \
return c > a ? 0 : c;};
#define DECL_SADD(XX) int ##XX##_t sadd##XX (int ##XX##_t a, int ##XX##_t b) { \
int##XX##_t c = a + b; \
return ((a ^ b) & INT##XX##_MIN) == 0 ? (c ^ a) & INT##XX##_MIN ? (a < 0) ? INT##XX##_MIN : INT##XX##_MAX : c : c; \
}
#define DECL_SSUB(XX) int ##XX##_t ssub##XX (int##XX##_t a, int##XX##_t b) { \
int##XX## _t c = a - b; \
return ((a ^ b) & INT##XX##_MIN) == 0 ? (c ^ a) & INT##XX##_MIN ? (a < 0) ? INT##XX##_MIN : INT##XX##_MAX : c : c; \
}
DECL_ADD(8); // uint8_t add8(uint8_t, uint8_t);
DECL_ADD(16); // uint16_t add8(uint16_t, uint16_t);
DECL_ADD(32);
DECL_SUB(8);
DECL_SUB(16);
DECL_SUB(32);
DECL_SADD(8); // int8_t sadd8(int8_t, int8_t);
DECL_SADD(16);
DECL_SADD(32);
DECL_SSUB(8);
DECL_SSUB(16); // int16_t ssub16(int16_t, int16_t);
DECL_SSUB(32);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment