AtomicMath.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. #include <atomic.h>
  6. #include <string.h>
  7. #include <limits.h>
  8. int main (int argc, char ** argv, char ** envp)
  9. {
  10. int64_t my_int = 0;
  11. struct atomic_int a_int;
  12. atomic_set(&a_int, 0);
  13. /* Check that INT_MIN and INT_MAX wrap around consistently
  14. * with atomic values.
  15. *
  16. * Check atomic_sub specifically.
  17. */
  18. my_int -= INT_MIN;
  19. atomic_sub(INT_MIN, &a_int);
  20. if (my_int == atomic_read(&a_int))
  21. pal_printf("Subtract INT_MIN: Both values match %lld\n", my_int);
  22. else
  23. pal_printf("Subtract INT_MIN: Values do not match %lld, %lld\n", my_int, atomic_read(&a_int));
  24. atomic_set(&a_int, 0);
  25. my_int = 0;
  26. my_int -= INT_MAX;
  27. atomic_sub(INT_MAX, &a_int);
  28. if (my_int == atomic_read(&a_int))
  29. pal_printf("Subtract INT_MAX: Both values match %lld\n", my_int);
  30. else
  31. pal_printf("Subtract INT_MAX: Values do not match %lld, %lld\n", my_int, atomic_read(&a_int));
  32. /* Check that 64-bit signed values also wrap properly. */
  33. atomic_set(&a_int, 0);
  34. my_int = 0;
  35. my_int -= LLONG_MIN;
  36. atomic_sub(LLONG_MIN, &a_int);
  37. if (my_int == atomic_read(&a_int))
  38. pal_printf("Subtract LLONG_MIN: Both values match %lld\n", my_int);
  39. else
  40. pal_printf("Subtract LLONG_MIN: Values do not match %lld, %lld\n", my_int, atomic_read(&a_int));
  41. atomic_set(&a_int, 0);
  42. my_int = 0;
  43. my_int -= LLONG_MAX;
  44. atomic_sub(LLONG_MAX, &a_int);
  45. if (my_int == atomic_read(&a_int))
  46. pal_printf("Subtract LLONG_MAX: Both values match %lld\n", my_int);
  47. else
  48. pal_printf("Subtract LLONG_MAX: Values do not match %lld, %lld\n", my_int, atomic_read(&a_int));
  49. return 0;
  50. }