xref: /netbsd-src/common/lib/libc/atomic/atomic_and_8_cas.c (revision 504e8e687f4f6c5bcfd35885ec51e14ae6b54f44)
1*504e8e68Sjoerg /*	$NetBSD: atomic_and_8_cas.c,v 1.3 2014/06/23 21:53:45 joerg Exp $	*/
2e39d980fSmartin 
3e39d980fSmartin /*-
4e39d980fSmartin  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5e39d980fSmartin  * All rights reserved.
6e39d980fSmartin  *
7e39d980fSmartin  * This code is derived from software contributed to The NetBSD Foundation
8e39d980fSmartin  * by Jason R. Thorpe.
9e39d980fSmartin  *
10e39d980fSmartin  * Redistribution and use in source and binary forms, with or without
11e39d980fSmartin  * modification, are permitted provided that the following conditions
12e39d980fSmartin  * are met:
13e39d980fSmartin  * 1. Redistributions of source code must retain the above copyright
14e39d980fSmartin  *    notice, this list of conditions and the following disclaimer.
15e39d980fSmartin  * 2. Redistributions in binary form must reproduce the above copyright
16e39d980fSmartin  *    notice, this list of conditions and the following disclaimer in the
17e39d980fSmartin  *    documentation and/or other materials provided with the distribution.
18e39d980fSmartin  *
19e39d980fSmartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20e39d980fSmartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21e39d980fSmartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22e39d980fSmartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23e39d980fSmartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24e39d980fSmartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25e39d980fSmartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e39d980fSmartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27e39d980fSmartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28e39d980fSmartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29e39d980fSmartin  * POSSIBILITY OF SUCH DAMAGE.
30e39d980fSmartin  */
31e39d980fSmartin 
32e39d980fSmartin #include "atomic_op_namespace.h"
33e39d980fSmartin 
34e39d980fSmartin #include <sys/atomic.h>
35e39d980fSmartin 
36e39d980fSmartin uint8_t fetch_and_and_1(volatile uint8_t *, uint8_t, ...)
37e39d980fSmartin     asm("__sync_fetch_and_and_1");
38e39d980fSmartin 
39e39d980fSmartin uint8_t
fetch_and_and_1(volatile uint8_t * addr,uint8_t val,...)40e39d980fSmartin fetch_and_and_1(volatile uint8_t *addr, uint8_t val, ...)
41e39d980fSmartin {
42e39d980fSmartin 	uint8_t old, new;
43e39d980fSmartin 
44e39d980fSmartin 	do {
45e39d980fSmartin 		old = *addr;
46e39d980fSmartin 		new = old & val;
47e39d980fSmartin 	} while (atomic_cas_8(addr, old, new) != old);
48e39d980fSmartin 	return old;
49e39d980fSmartin }
50*504e8e68Sjoerg 
51*504e8e68Sjoerg __strong_alias(__atomic_fetch_and_1,__sync_fetch_and_and_1)
52