gload.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2012-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Atomic Library (libatomic).
  4. Libatomic is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libatomic_i.h"
  20. /* If we natively support the load, and if we're unconcerned with extra
  21. barriers (e.g. fully in-order cpu for which barriers are a nop), then
  22. go ahead and expand the operation inline. */
  23. #if !defined(WANT_SPECIALCASE_RELAXED) && !defined(__OPTIMIZE_SIZE__)
  24. # define EXACT_INLINE(N, DEST, SRC, DONE) \
  25. if (C2(HAVE_ATOMIC_LDST_,N)) \
  26. { \
  27. DEST = __atomic_load_n (SRC, __ATOMIC_SEQ_CST); \
  28. DONE; \
  29. }
  30. #else
  31. # define EXACT_INLINE(N, DEST, SRC, DONE)
  32. #endif
  33. #define EXACT_(N, DEST, SRC, DONE) \
  34. do { \
  35. EXACT_INLINE (N, DEST, SRC, DONE); \
  36. DEST = C3(local_,load_,N) (SRC, smodel); \
  37. DONE; \
  38. } while (0)
  39. #define EXACT(N) \
  40. do { \
  41. if (!C2(HAVE_INT,N)) break; \
  42. if ((uintptr_t)mptr & (N - 1)) break; \
  43. EXACT_(N, *PTR(N,rptr), PTR(N,mptr), return); \
  44. } while (0)
  45. #define LARGER(N) \
  46. do { \
  47. if (!C2(HAVE_INT,N)) break; \
  48. r = (uintptr_t)mptr & (N - 1); \
  49. a = (uintptr_t)mptr & -N; \
  50. if (r + n <= N) \
  51. EXACT_ (N, u.C2(i,N), PTR(N,a), goto Lfinish); \
  52. } while (0)
  53. void
  54. libat_load (size_t n, void *mptr, void *rptr, int smodel)
  55. {
  56. union max_size_u u;
  57. uintptr_t r, a;
  58. switch (n)
  59. {
  60. case 0: return;
  61. case 1: EXACT(1); goto L4;
  62. case 2: EXACT(2); goto L4;
  63. case 4: EXACT(4); goto L8;
  64. case 8: EXACT(8); goto L16;
  65. case 16: EXACT(16); break;
  66. case 3: L4: LARGER(4); /* FALLTHRU */
  67. case 5 ... 7: L8: LARGER(8); /* FALLTHRU */
  68. case 9 ... 15: L16: LARGER(16); break;
  69. Lfinish:
  70. memcpy (rptr, u.b + r, n);
  71. return;
  72. }
  73. pre_seq_barrier (smodel);
  74. libat_lock_n (mptr, n);
  75. memcpy (rptr, mptr, n);
  76. libat_unlock_n (mptr, n);
  77. post_seq_barrier (smodel);
  78. }
  79. EXPORT_ALIAS (load);