11.3Sjoerg/*	$NetBSD: atomic_and_16_cas.c,v 1.3 2014/06/23 21:53:45 joerg Exp $	*/
21.2Smartin
31.2Smartin/*-
41.2Smartin * Copyright (c) 2007 The NetBSD Foundation, Inc.
51.2Smartin * All rights reserved.
61.2Smartin *
71.2Smartin * This code is derived from software contributed to The NetBSD Foundation
81.2Smartin * by Jason R. Thorpe.
91.2Smartin *
101.2Smartin * Redistribution and use in source and binary forms, with or without
111.2Smartin * modification, are permitted provided that the following conditions
121.2Smartin * are met:
131.2Smartin * 1. Redistributions of source code must retain the above copyright
141.2Smartin *    notice, this list of conditions and the following disclaimer.
151.2Smartin * 2. Redistributions in binary form must reproduce the above copyright
161.2Smartin *    notice, this list of conditions and the following disclaimer in the
171.2Smartin *    documentation and/or other materials provided with the distribution.
181.2Smartin *
191.2Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.2Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.2Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.2Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.2Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.2Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.2Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.2Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.2Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.2Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.2Smartin * POSSIBILITY OF SUCH DAMAGE.
301.2Smartin */
311.2Smartin
321.2Smartin#include "atomic_op_namespace.h"
331.2Smartin
341.2Smartin#include <sys/atomic.h>
351.2Smartin
361.2Smartinuint16_t fetch_and_and_2(volatile uint16_t *, uint16_t, ...)
371.2Smartin    asm("__sync_fetch_and_and_2");
381.2Smartin
391.2Smartinuint16_t
401.2Smartinfetch_and_and_2(volatile uint16_t *addr, uint16_t val, ...)
411.2Smartin{
421.2Smartin	uint16_t old, new;
431.2Smartin
441.2Smartin	do {
451.2Smartin		old = *addr;
461.2Smartin		new = old & val;
471.2Smartin	} while (atomic_cas_16(addr, old, new) != old);
481.2Smartin	return old;
491.2Smartin}
501.3Sjoerg
511.3Sjoerg__strong_alias(__atomic_fetch_and_2,__sync_fetch_and_and_2)
52