1*0a6a1f1dSLionel Sambuc /*	$NetBSD: atomic_and_64_cas.c,v 1.10 2014/06/23 21:53:45 joerg Exp $	*/
2b6cbf720SGianluca Guida 
3b6cbf720SGianluca Guida /*-
4b6cbf720SGianluca Guida  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5b6cbf720SGianluca Guida  * All rights reserved.
6b6cbf720SGianluca Guida  *
7b6cbf720SGianluca Guida  * This code is derived from software contributed to The NetBSD Foundation
8b6cbf720SGianluca Guida  * by Jason R. Thorpe.
9b6cbf720SGianluca Guida  *
10b6cbf720SGianluca Guida  * Redistribution and use in source and binary forms, with or without
11b6cbf720SGianluca Guida  * modification, are permitted provided that the following conditions
12b6cbf720SGianluca Guida  * are met:
13b6cbf720SGianluca Guida  * 1. Redistributions of source code must retain the above copyright
14b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer.
15b6cbf720SGianluca Guida  * 2. Redistributions in binary form must reproduce the above copyright
16b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer in the
17b6cbf720SGianluca Guida  *    documentation and/or other materials provided with the distribution.
18b6cbf720SGianluca Guida  *
19b6cbf720SGianluca Guida  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b6cbf720SGianluca Guida  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b6cbf720SGianluca Guida  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b6cbf720SGianluca Guida  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b6cbf720SGianluca Guida  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b6cbf720SGianluca Guida  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b6cbf720SGianluca Guida  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b6cbf720SGianluca Guida  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b6cbf720SGianluca Guida  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b6cbf720SGianluca Guida  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b6cbf720SGianluca Guida  * POSSIBILITY OF SUCH DAMAGE.
30b6cbf720SGianluca Guida  */
31b6cbf720SGianluca Guida 
32b6cbf720SGianluca Guida #include "atomic_op_namespace.h"
33b6cbf720SGianluca Guida 
34b6cbf720SGianluca Guida #include <sys/atomic.h>
35b6cbf720SGianluca Guida 
36b6cbf720SGianluca Guida #ifdef __HAVE_ATOMIC64_OPS
37b6cbf720SGianluca Guida 
38*0a6a1f1dSLionel Sambuc uint64_t fetch_and_and_8(volatile uint64_t *, uint64_t, ...)
39*0a6a1f1dSLionel Sambuc     asm("__sync_fetch_and_and_8");
40*0a6a1f1dSLionel Sambuc 
41*0a6a1f1dSLionel Sambuc uint64_t
fetch_and_and_8(volatile uint64_t * addr,uint64_t val,...)42*0a6a1f1dSLionel Sambuc fetch_and_and_8(volatile uint64_t *addr, uint64_t val, ...)
43b6cbf720SGianluca Guida {
44b6cbf720SGianluca Guida 	uint64_t old, new;
45b6cbf720SGianluca Guida 
46b6cbf720SGianluca Guida 	do {
47b6cbf720SGianluca Guida 		old = *addr;
48b6cbf720SGianluca Guida 		new = old & val;
49b6cbf720SGianluca Guida 	} while (atomic_cas_64(addr, old, new) != old);
50*0a6a1f1dSLionel Sambuc 	return old;
51b6cbf720SGianluca Guida }
52b6cbf720SGianluca Guida 
53*0a6a1f1dSLionel Sambuc void
atomic_and_64(volatile uint64_t * addr,uint64_t val)54*0a6a1f1dSLionel Sambuc atomic_and_64(volatile uint64_t *addr, uint64_t val)
55*0a6a1f1dSLionel Sambuc {
56*0a6a1f1dSLionel Sambuc 	(void) fetch_and_and_8(addr, val);
57*0a6a1f1dSLionel Sambuc }
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc __strong_alias(__atomic_fetch_and_8,__sync_fetch_and_and_8)
60*0a6a1f1dSLionel Sambuc 
61b6cbf720SGianluca Guida #undef atomic_and_64
62b6cbf720SGianluca Guida atomic_op_alias(atomic_and_64,_atomic_and_64)
63*0a6a1f1dSLionel Sambuc 
64b6cbf720SGianluca Guida #if defined(_LP64)
65b6cbf720SGianluca Guida #undef atomic_and_ulong
66b6cbf720SGianluca Guida atomic_op_alias(atomic_and_ulong,_atomic_and_64)
67b6cbf720SGianluca Guida __strong_alias(_atomic_and_ulong,_atomic_and_64)
68b6cbf720SGianluca Guida #endif /* _LP64 */
69b6cbf720SGianluca Guida 
70b6cbf720SGianluca Guida #endif
71