Skip to content

Commit 859711a

Browse files
committed
CLN: Apply clang form
Apply clang format to core source files
1 parent b4f38e6 commit 859711a

File tree

5 files changed

+507
-443
lines changed

5 files changed

+507
-443
lines changed

randomstate/aligned_malloc.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
#include "Python.h"
22
#include "numpy/npy_common.h"
33

4-
#define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */
4+
#define NPY_MEMALIGN 16 /* 16 for SSE2, 32 for AVX, 64 for Xeon Phi */
55

6-
static NPY_INLINE
7-
void *PyArray_realloc_aligned(void *p, size_t n)
6+
static NPY_INLINE void *PyArray_realloc_aligned(void *p, size_t n)
87
{
98
void *p1, **p2, *base;
10-
size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void*);
11-
if (NPY_UNLIKELY(p != NULL)) {
12-
base = *(((void**)p)-1);
13-
if (NPY_UNLIKELY((p1 = PyMem_Realloc(base,n+offs)) == NULL)) return NULL;
14-
if (NPY_LIKELY(p1 == base)) return p;
15-
p2 = (void**)(((Py_uintptr_t)(p1)+offs) & ~(NPY_MEMALIGN-1));
9+
size_t old_offs, offs = NPY_MEMALIGN - 1 + sizeof(void *);
10+
if (NPY_UNLIKELY(p != NULL))
11+
{
12+
base = *(((void **)p) - 1);
13+
if (NPY_UNLIKELY((p1 = PyMem_Realloc(base, n + offs)) == NULL))
14+
return NULL;
15+
if (NPY_LIKELY(p1 == base))
16+
return p;
17+
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
1618
old_offs = (size_t)((Py_uintptr_t)p - (Py_uintptr_t)base);
17-
memmove((void*)p2,((char*)p1)+old_offs,n);
18-
} else {
19-
if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL)) return NULL;
20-
p2 = (void**)(((Py_uintptr_t)(p1)+offs) & ~(NPY_MEMALIGN-1));
19+
memmove((void *)p2, ((char *)p1) + old_offs, n);
2120
}
22-
*(p2-1) = p1;
23-
return (void*)p2;
21+
else
22+
{
23+
if (NPY_UNLIKELY((p1 = PyMem_Malloc(n + offs)) == NULL))
24+
return NULL;
25+
p2 = (void **)(((Py_uintptr_t)(p1) + offs) & ~(NPY_MEMALIGN - 1));
26+
}
27+
*(p2 - 1) = p1;
28+
return (void *)p2;
2429
}
2530

26-
static NPY_INLINE
27-
void *PyArray_malloc_aligned(size_t n)
31+
static NPY_INLINE void *PyArray_malloc_aligned(size_t n)
2832
{
2933
return PyArray_realloc_aligned(NULL, n);
3034
}
3135

32-
static NPY_INLINE
33-
void *PyArray_calloc_aligned(size_t n, size_t s)
36+
static NPY_INLINE void *PyArray_calloc_aligned(size_t n, size_t s)
3437
{
3538
void *p;
36-
if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL,n*s)) == NULL)) return NULL;
37-
memset(p, 0, n*s);
39+
if (NPY_UNLIKELY((p = PyArray_realloc_aligned(NULL, n * s)) == NULL))
40+
return NULL;
41+
memset(p, 0, n * s);
3842
return p;
3943
}
4044

41-
static NPY_INLINE
42-
void PyArray_free_aligned(void *p)
45+
static NPY_INLINE void PyArray_free_aligned(void *p)
4346
{
44-
void *base = *(((void**)p)-1);
47+
void *base = *(((void **)p) - 1);
4548
PyMem_Free(base);
4649
}

randomstate/bounded_integers.pxi.in

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
2121
mask |= mask >> 16
2222
mask |= mask >> 32
2323
return mask
24-
2524
{{
2625
py:
2726
bc_ctypes = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000ULL'),
@@ -32,11 +31,8 @@ bc_ctypes = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000U
3231
('int16', 'uint16', 'uint32', 'NPY_INT32', 1, 16, '-0x8000LL', '0x8000LL' ),
3332
('int8', 'uint8', 'uint16', 'NPY_INT16', 3, 8, '-0x80LL', '0x80LL' ),
3433
)}}
35-
3634
{{for nptype, utype, nptype_up, npctype, remaining, bitshift, lb, ub in bc_ctypes}}
37-
3835
{{ py: otype = nptype + '_' if nptype == 'bool' else nptype }}
39-
4036
cdef object _rand_{{nptype}}(object low, object high, object size, aug_state *state, object lock):
4137
"""
4238
_rand_{{nptype}}(low, high, size, *state, lock)
@@ -50,27 +46,27 @@ cdef object _rand_{{nptype}}(object low, object high, object size, aug_state *st
5046

5147
Parameters
5248
----------
53-
low : int
49+
low : int or array-like
5450
Lowest (signed) integer to be drawn from the distribution (unless
5551
``high=None``, in which case this parameter is the *highest* such
5652
integer).
57-
high : int
53+
high : int or array-like
5854
If provided, the largest (signed) integer to be drawn from the
5955
distribution (see above for behavior if ``high=None``).
6056
size : int or tuple of ints
6157
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
6258
``m * n * k`` samples are drawn. Default is None, in which case a
6359
single value is returned.
64-
rngstate : encapsulated pointer to rk_state
65-
The specific type depends on the python version. In Python 2 it is
66-
a PyCObject, in Python 3 a PyCapsule object.
60+
state : augmented random state
61+
State to use in the core random number generators
62+
lock : threading.Lock
63+
Lock to prevent multiple using a single RandomState simultaneously
6764

6865
Returns
6966
-------
7067
out : python scalar or ndarray of np.{{nptype}}
7168
`size`-shaped array of random integers from the appropriate
7269
distribution, or a single such random int if `size` not provided.
73-
7470
"""
7571
cdef {{utype}}_t rng, last_rng, off, val, mask, out_val
7672
cdef uint32_t buf
@@ -150,45 +146,47 @@ cdef object _rand_{{nptype}}(object low, object high, object size, aug_state *st
150146

151147
return out_arr
152148
{{endfor}}
153-
154-
155149
{{
156150
py:
157151
big_bc_ctypes = (('uint64', 'uint64', 'NPY_UINT64', '0x0ULL', '0xFFFFFFFFFFFFFFFFULL'),
158152
('int64', 'uint64', 'NPY_INT64', '-0x8000000000000000LL', '0x7FFFFFFFFFFFFFFFLL' )
159153
)}}
160-
161154
{{for nptype, utype, npctype, lb, ub in big_bc_ctypes}}
162-
163155
{{ py: otype = nptype}}
164-
165156
cdef object _rand_{{nptype}}(object low, object high, object size, aug_state *state, object lock):
166157
"""
167-
Generate bounded random {{nptype}} values
158+
_rand_{{nptype}}(low, high, size, *state, lock)
159+
160+
Return random np.{{nptype}} integers between `low` and `high`, inclusive.
161+
162+
Return random integers from the "discrete uniform" distribution in the
163+
closed interval [`low`, `high`). If `high` is None (the default),
164+
then results are from [0, `low`). On entry the arguments are presumed
165+
to have been validated for size and order for the np.{{nptype}} type.
168166

169167
Parameters
170168
----------
171169
low : int or array-like
172-
Array containing the lowest (signed) integers to be drawn from the
173-
distribution.
170+
Lowest (signed) integer to be drawn from the distribution (unless
171+
``high=None``, in which case this parameter is the *highest* such
172+
integer).
174173
high : int or array-like
175-
Array containing the the open interval bound for the distribution.
174+
If provided, the largest (signed) integer to be drawn from the
175+
distribution (see above for behavior if ``high=None``).
176176
size : int or tuple of ints
177177
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
178-
``m * n * k`` samples are drawn. Default is None, in which case
179-
the output shape is determined by the broadcast shapes of low and
180-
high
178+
``m * n * k`` samples are drawn. Default is None, in which case a
179+
single value is returned.
181180
state : augmented random state
182181
State to use in the core random number generators
183182
lock : threading.Lock
184183
Lock to prevent multiple using a single RandomState simultaneously
185184

186185
Returns
187186
-------
188-
out : ndarray of np.{{nptype}}
189-
array of random integers from the appropriate distribution where the
190-
size is determined by size if provided or the broadcast shape of low
191-
and high
187+
out : python scalar or ndarray of np.{{nptype}}
188+
`size`-shaped array of random integers from the appropriate
189+
distribution, or a single such random int if `size` not provided.
192190
"""
193191
cdef np.ndarray low_arr, high_arr, out_arr, highm1_arr
194192
cdef np.npy_intp i, cnt
@@ -279,5 +277,4 @@ cdef object _rand_{{nptype}}(object low, object high, object size, aug_state *st
279277
np.PyArray_MultiIter_NEXT(it)
280278

281279
return out_arr
282-
283280
{{endfor}}

0 commit comments

Comments
 (0)