xref: /netbsd-src/common/lib/libc/atomic/atomic_nand_8_cas.c (revision 3bbf8c07ee158914ceea1a2c61bc4b343b69b25e)
1*3bbf8c07Sisaki /*	$NetBSD: atomic_nand_8_cas.c,v 1.3 2019/03/01 06:14:52 isaki Exp $	*/
2e39d980fSmartin 
3e39d980fSmartin /*-
4e39d980fSmartin  * Copyright (c) 2014 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 
32de9cd3f9Smartin #include "atomic_op_namespace.h"
33de9cd3f9Smartin 
34e39d980fSmartin #include <sys/atomic.h>
35e39d980fSmartin 
36e39d980fSmartin uint8_t fetch_and_nand_1(volatile uint8_t *, uint8_t, ...)
37e39d980fSmartin     asm("__sync_fetch_and_nand_1");
38e39d980fSmartin uint8_t nand_and_fetch_1(volatile uint8_t *, uint8_t, ...)
39e39d980fSmartin     asm("__sync_nand_and_fetch_1");
40e39d980fSmartin 
41e39d980fSmartin uint8_t
fetch_and_nand_1(volatile uint8_t * addr,uint8_t val,...)42e39d980fSmartin fetch_and_nand_1(volatile uint8_t *addr, uint8_t val, ...)
43e39d980fSmartin {
44e39d980fSmartin 	uint8_t old, new;
45e39d980fSmartin 
46e39d980fSmartin 	do {
47e39d980fSmartin 		old = *addr;
48e39d980fSmartin 		new = ~(old & val);
49e39d980fSmartin 	} while (atomic_cas_8(addr, old, new) != old);
50e39d980fSmartin 	return old;
51e39d980fSmartin }
52e39d980fSmartin 
53e39d980fSmartin uint8_t
nand_and_fetch_1(volatile uint8_t * addr,uint8_t val,...)54e39d980fSmartin nand_and_fetch_1(volatile uint8_t *addr, uint8_t val, ...)
55e39d980fSmartin {
56e39d980fSmartin 	uint8_t old, new;
57e39d980fSmartin 
58e39d980fSmartin 	do {
59e39d980fSmartin 		old = *addr;
60e39d980fSmartin 		new = ~(old & val);
61e39d980fSmartin 	} while (atomic_cas_8(addr, old, new) != old);
62e39d980fSmartin 	return new;
63e39d980fSmartin }
64*3bbf8c07Sisaki 
65*3bbf8c07Sisaki __strong_alias(__atomic_fetch_nand_1,__sync_fetch_and_nand_1)
66