logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

absu.h (846B)


  1. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  2. // SPDX-License-Identifier: CC0-1.0 OR WTFPL
  3. // unsigned by definition reclaims the sign bit of their associated
  4. // signed representation, meaning you can always represent the absolute
  5. // values in an unsigned, even in two's complement where keeping
  6. // a signed representation would cause an error.
  7. //
  8. // Sadly the abs() family of functions in <stdint.h> is misdesigned
  9. extern unsigned int
  10. absu(int a)
  11. {
  12. if(a >= 0)
  13. return (unsigned int)a;
  14. else
  15. return (unsigned int)-a;
  16. }
  17. extern long unsigned int
  18. labsu(long int a)
  19. {
  20. if(a >= 0)
  21. return (long unsigned int)a;
  22. else
  23. return (long unsigned int)-a;
  24. }
  25. extern long long unsigned int
  26. llabsu(long long int a)
  27. {
  28. if(a >= 0)
  29. return (long long unsigned int)a;
  30. else
  31. return (long long unsigned int)-a;
  32. }