Merge trunk version 207649 into gupc branch.
[official-gcc.git] / libgupc / smp / upc_addr.c
blob31872a238e393881269785140ecb3f557dc75e0f
1 /* Copyright (C) 2001-2014 Free Software Foundation, Inc.
2 This file is part of the UPC runtime Library.
3 Written by Gary Funck <gary@intrepid.com>
4 and Nenad Vukicevic <nenad@intrepid.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "upc_config.h"
28 #include "upc_sysdep.h"
29 #include "upc_defs.h"
30 #include "upc_sup.h"
32 void *
33 __cvtaddr (upc_shared_ptr_t p)
35 upc_info_p u = __upc_info;
36 void *addr;
37 if (!u)
38 __upc_fatal ("UPC runtime not initialized");
39 if (GUPCR_PTS_IS_NULL (p))
40 return (void *) 0;
41 addr = __upc_sptr_to_addr (p);
42 return addr;
45 void *
46 __getaddr (upc_shared_ptr_t p)
48 upc_info_p u = __upc_info;
49 void *addr;
50 if (!u)
51 __upc_fatal ("UPC runtime not initialized");
52 if (GUPCR_PTS_IS_NULL (p))
53 return (void *) 0;
54 if ((int)GUPCR_PTS_THREAD(p) != MYTHREAD)
55 __upc_fatal ("Invalid conversion of shared address to local pointer;\nthread does not have affinity to shared address");
56 addr = __upc_sptr_to_addr (p);
57 return addr;
60 size_t
61 upc_threadof (upc_shared_ptr_t p)
63 upc_info_p u = __upc_info;
64 if (!u)
65 __upc_fatal ("UPC runtime not initialized");
66 if ((int)GUPCR_PTS_THREAD(p) >= THREADS)
67 __upc_fatal ("Thread number in shared address is out of range");
68 return GUPCR_PTS_THREAD (p);
71 size_t
72 upc_phaseof (upc_shared_ptr_t p)
74 upc_info_p u = __upc_info;
75 if (!u)
76 __upc_fatal ("UPC runtime not initialized");
77 if ((int)GUPCR_PTS_THREAD(p) >= THREADS)
78 __upc_fatal ("Thread number in shared address is out of range");
79 return GUPCR_PTS_PHASE (p);
82 upc_shared_ptr_t
83 upc_resetphase (upc_shared_ptr_t p)
85 upc_shared_ptr_t result;
86 result = p;
87 GUPCR_PTS_SET_PHASE (result, 0);
88 return result;
91 size_t
92 upc_addrfield (upc_shared_ptr_t p)
94 upc_info_p u = __upc_info;
95 if (!u)
96 __upc_fatal ("UPC runtime not initialized");
97 if ((int)GUPCR_PTS_THREAD(p) >= THREADS)
98 __upc_fatal ("Thread number in shared address is out of range");
99 return (size_t) GUPCR_PTS_VADDR (p);
102 size_t
103 upc_affinitysize (size_t totalsize, size_t nbytes, size_t threadid)
105 size_t result;
106 if (nbytes == 0 || totalsize == 0 || nbytes >= totalsize)
107 result = (size_t) (threadid == 0 ? totalsize : 0);
108 else
110 size_t const nblocks = (totalsize / nbytes);
111 size_t const cutoff = (nblocks % THREADS);
112 if (threadid < cutoff)
113 result = (size_t) ((nblocks + THREADS - 1) / THREADS) * nbytes;
114 else if (threadid > cutoff)
115 result = (size_t) (nblocks / THREADS) * nbytes;
116 else /* threadid == cutoff */
117 result = (size_t) ((nblocks / THREADS) * nbytes)
118 + totalsize - nblocks * nbytes;
120 return result;