AtomicMath.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <atomic.h>
  2. #include <limits.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include "pal.h"
  6. #include "pal_debug.h"
  7. int main(int argc, char** argv, char** envp) {
  8. int64_t my_int = 0;
  9. struct atomic_int a_int;
  10. atomic_set(&a_int, 0);
  11. /* Check that INT_MIN and INT_MAX wrap around consistently
  12. * with atomic values.
  13. *
  14. * Check atomic_sub specifically.
  15. */
  16. my_int -= INT_MIN;
  17. atomic_sub(INT_MIN, &a_int);
  18. if (my_int == atomic_read(&a_int))
  19. pal_printf("Subtract INT_MIN: Both values match %ld\n", my_int);
  20. else
  21. pal_printf("Subtract INT_MIN: Values do not match %ld, %ld\n", my_int, atomic_read(&a_int));
  22. atomic_set(&a_int, 0);
  23. my_int = 0;
  24. my_int -= INT_MAX;
  25. atomic_sub(INT_MAX, &a_int);
  26. if (my_int == atomic_read(&a_int))
  27. pal_printf("Subtract INT_MAX: Both values match %ld\n", my_int);
  28. else
  29. pal_printf("Subtract INT_MAX: Values do not match %ld, %ld\n", my_int, atomic_read(&a_int));
  30. /* Check that 64-bit signed values also wrap properly. */
  31. atomic_set(&a_int, 0);
  32. my_int = 0;
  33. my_int -= LLONG_MIN;
  34. atomic_sub(LLONG_MIN, &a_int);
  35. if (my_int == atomic_read(&a_int))
  36. pal_printf("Subtract LLONG_MIN: Both values match %ld\n", my_int);
  37. else
  38. pal_printf("Subtract LLONG_MIN: Values do not match %ld, %ld\n", my_int,
  39. atomic_read(&a_int));
  40. atomic_set(&a_int, 0);
  41. my_int = 0;
  42. my_int -= LLONG_MAX;
  43. atomic_sub(LLONG_MAX, &a_int);
  44. if (my_int == atomic_read(&a_int))
  45. pal_printf("Subtract LLONG_MAX: Both values match %ld\n", my_int);
  46. else
  47. pal_printf("Subtract LLONG_MAX: Values do not match %ld, %ld\n", my_int,
  48. atomic_read(&a_int));
  49. return 0;
  50. }