11.3Sjoerg/*	$NetBSD: atomic_xor_16_cas.c,v 1.3 2014/06/23 21:53:45 joerg Exp $	*/
21.1Smartin
31.1Smartin/*-
41.1Smartin * Copyright (c) 2014 The NetBSD Foundation, Inc.
51.1Smartin * All rights reserved.
61.1Smartin *
71.1Smartin * This code is derived from software contributed to The NetBSD Foundation
81.1Smartin * by Jason R. Thorpe.
91.1Smartin *
101.1Smartin * Redistribution and use in source and binary forms, with or without
111.1Smartin * modification, are permitted provided that the following conditions
121.1Smartin * are met:
131.1Smartin * 1. Redistributions of source code must retain the above copyright
141.1Smartin *    notice, this list of conditions and the following disclaimer.
151.1Smartin * 2. Redistributions in binary form must reproduce the above copyright
161.1Smartin *    notice, this list of conditions and the following disclaimer in the
171.1Smartin *    documentation and/or other materials provided with the distribution.
181.1Smartin *
191.1Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smartin * POSSIBILITY OF SUCH DAMAGE.
301.1Smartin */
311.1Smartin
321.2Smartin#include "atomic_op_namespace.h"
331.2Smartin
341.1Smartin#include <sys/atomic.h>
351.1Smartin
361.1Smartinuint16_t fetch_and_xor_2(volatile uint16_t *, uint16_t, ...)
371.1Smartin    asm("__sync_fetch_and_xor_2");
381.1Smartinuint16_t xor_and_fetch_2(volatile uint16_t *, uint16_t, ...)
391.1Smartin    asm("__sync_xor_and_fetch_2");
401.1Smartin
411.1Smartinuint16_t
421.1Smartinfetch_and_xor_2(volatile uint16_t *addr, uint16_t val, ...)
431.1Smartin{
441.1Smartin	uint16_t old, new;
451.1Smartin
461.1Smartin	do {
471.1Smartin		old = *addr;
481.1Smartin		new = old ^ val;
491.1Smartin	} while (atomic_cas_16(addr, old, new) != old);
501.1Smartin	return old;
511.1Smartin}
521.1Smartin
531.1Smartinuint16_t
541.1Smartinxor_and_fetch_2(volatile uint16_t *addr, uint16_t val, ...)
551.1Smartin{
561.1Smartin	uint16_t old, new;
571.1Smartin
581.1Smartin	do {
591.1Smartin		old = *addr;
601.1Smartin		new = old ^ val;
611.1Smartin	} while (atomic_cas_16(addr, old, new) != old);
621.1Smartin	return new;
631.1Smartin}
641.3Sjoerg
651.3Sjoerg__strong_alias(__atomic_fetch_xor_2,__sync_fetch_and_xor_2)
66