1 /* $NetBSD: atomic_cas_by_cas32.c,v 1.4 2014/09/03 19:30:47 matt Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/inttypes.h>
34
35 #include "atomic_op_namespace.h"
36
37 uint32_t _atomic_cas_32(volatile uint32_t *addr, uint32_t old, uint32_t new);
38 uint16_t _atomic_cas_16(volatile uint16_t *addr, uint16_t old, uint16_t new);
39 uint8_t _atomic_cas_8(volatile uint8_t *addr, uint8_t old, uint8_t new);
40
41 /*
42 * This file provides emulation of 8 bit and 16 bit CAS operations based on
43 * 32 bit CAS.
44 */
45 uint16_t
_atomic_cas_16(volatile uint16_t * addr,uint16_t old,uint16_t new)46 _atomic_cas_16(volatile uint16_t *addr, uint16_t old, uint16_t new)
47 {
48 const uintptr_t base = (uintptr_t)addr & ~3;
49 const size_t off = (uintptr_t)addr - base;
50 volatile uint32_t * ptr = (volatile uint32_t *)base;
51 const size_t shift = off*8;
52 const uint32_t mask = 0x0ffff << shift;
53 const uint32_t old32_part = (uint32_t)old << shift;
54 const uint32_t new32_part = (uint32_t)new << shift;
55 uint32_t old32, new32;
56
57 do {
58 old32 = *ptr;
59 if ((old32 & mask) != old32_part)
60 return (uint16_t)((old32 & mask) >> shift);
61 new32 = (old32 & ~mask) | new32_part;
62 } while (_atomic_cas_32(ptr, old32, new32) != old32);
63
64 return old;
65 }
66
crt_alias(__sync_val_compare_and_swap_2,_atomic_cas_16)67 crt_alias(__sync_val_compare_and_swap_2,_atomic_cas_16)
68
69 uint8_t
70 _atomic_cas_8(volatile uint8_t *addr, uint8_t old, uint8_t new)
71 {
72 const uintptr_t base = (uintptr_t)addr & ~3;
73 const size_t off = (uintptr_t)addr - base;
74 volatile uint32_t * ptr = (volatile uint32_t *)base;
75 const size_t shift = off*8;
76 const uint32_t mask = 0x0ff << shift;
77 const uint32_t old32_part = (uint32_t)old << shift;
78 const uint32_t new32_part = (uint32_t)new << shift;
79 uint32_t old32, new32;
80
81 do {
82 old32 = *ptr;
83 if ((old32 & mask) != old32_part)
84 return (uint8_t)((old32 & mask) >> shift);
85 new32 = (old32 & ~mask) | new32_part;
86 } while (_atomic_cas_32(ptr, old32, new32) != old32);
87
88 return old;
89 }
90
91 crt_alias(__sync_val_compare_and_swap_1,_atomic_cas_8)
92