AtomicMath.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. #include <atomic.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. int main (int argc, char ** argv, char ** envp)
  7. {
  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, atomic_read(&a_int));
  39. atomic_set(&a_int, 0);
  40. my_int = 0;
  41. my_int -= LLONG_MAX;
  42. atomic_sub(LLONG_MAX, &a_int);
  43. if (my_int == atomic_read(&a_int))
  44. pal_printf("Subtract LLONG_MAX: Both values match %ld\n", my_int);
  45. else
  46. pal_printf("Subtract LLONG_MAX: Values do not match %ld, %ld\n", my_int, atomic_read(&a_int));
  47. return 0;
  48. }