AtomicMath.c 1.6 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. {
  9. int64_t my_int = 0;
  10. struct atomic_int a_int;
  11. atomic_set(&a_int, 0);
  12. /* Check that INT_MIN and INT_MAX wrap around consistently
  13. * with atomic values.
  14. *
  15. * Check atomic_sub specifically.
  16. */
  17. my_int -= INT_MIN;
  18. atomic_sub(INT_MIN, &a_int);
  19. if (my_int == atomic_read(&a_int))
  20. pal_printf("Subtract INT_MIN: Both values match %ld\n", my_int);
  21. else
  22. pal_printf("Subtract INT_MIN: Values do not match %ld, %ld\n", my_int, atomic_read(&a_int));
  23. atomic_set(&a_int, 0);
  24. my_int = 0;
  25. my_int -= INT_MAX;
  26. atomic_sub(INT_MAX, &a_int);
  27. if (my_int == atomic_read(&a_int))
  28. pal_printf("Subtract INT_MAX: Both values match %ld\n", my_int);
  29. else
  30. pal_printf("Subtract INT_MAX: Values do not match %ld, %ld\n", my_int, atomic_read(&a_int));
  31. /* Check that 64-bit signed values also wrap properly. */
  32. atomic_set(&a_int, 0);
  33. my_int = 0;
  34. my_int -= LLONG_MIN;
  35. atomic_sub(LLONG_MIN, &a_int);
  36. if (my_int == atomic_read(&a_int))
  37. pal_printf("Subtract LLONG_MIN: Both values match %ld\n", my_int);
  38. else
  39. pal_printf("Subtract LLONG_MIN: Values do not match %ld, %ld\n", my_int, 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, atomic_read(&a_int));
  48. return 0;
  49. }