Tor  0.4.7.0-alpha-dev
addsub.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file addsub.c
8  *
9  * \brief Helpers for addition and subtraction.
10  *
11  * Currently limited to non-wrapping (saturating) addition.
12  **/
13 
14 #include "lib/intmath/addsub.h"
15 #include "lib/cc/compat_compiler.h"
16 
17 /* Helper: safely add two uint32_t's, capping at UINT32_MAX rather
18  * than overflow */
19 uint32_t
20 tor_add_u32_nowrap(uint32_t a, uint32_t b)
21 {
22  /* a+b > UINT32_MAX check, without overflow */
23  if (PREDICT_UNLIKELY(a > UINT32_MAX - b)) {
24  return UINT32_MAX;
25  } else {
26  return a+b;
27  }
28 }
Header for addsub.c.
Utility macros to handle different features and behavior in different compilers.