1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.concurrency.atomic.AtomicBiInteger;
13 
14 /**
15  * An AtomicLong with additional methods to treat it as two hi/lo integers.
16  */
17 public class AtomicBiInteger  { // AtomicLong
18 
19    /**
20      * Gets a hi value from the given encoded value.
21      *
22      * @param encoded the encoded value
23      * @return the hi value
24      */
25     public static int getHi(long encoded) {
26         return cast(int) ((encoded >> 32) & 0xFFFF_FFFFL);
27     }
28 
29     /**
30      * Gets a lo value from the given encoded value.
31      *
32      * @param encoded the encoded value
33      * @return the lo value
34      */
35     public static int getLo(long encoded) {
36         return cast(int) (encoded & 0xFFFF_FFFFL);
37     }
38 
39        /**
40      * Encodes hi and lo values into a long.
41      *
42      * @param hi the hi value
43      * @param lo the lo value
44      * @return the encoded value
45      */
46     public static long encode(int hi, int lo) {
47         long h = (cast(long) hi) & 0xFFFF_FFFFL;
48         long l = (cast(long) lo) & 0xFFFF_FFFFL;
49         return (h << 32) + l;
50     }
51 
52     /**
53      * Sets the hi value into the given encoded value.
54      *
55      * @param encoded the encoded value
56      * @param hi      the hi value
57      * @return the new encoded value
58      */
59     public static long encodeHi(long encoded, int hi) {
60         long h = (cast(long) hi) & 0xFFFF_FFFFL;
61         long l = encoded & 0xFFFF_FFFFL;
62         return (h << 32) + l;
63     }
64 
65     /**
66      * Sets the lo value into the given encoded value.
67      *
68      * @param encoded the encoded value
69      * @param lo      the lo value
70      * @return the new encoded value
71      */
72     public static long encodeLo(long encoded, int lo) {
73         long h = (encoded >> 32) & 0xFFFF_FFFFL;
74         long l = (cast(long) lo) & 0xFFFF_FFFFL;
75         return (h << 32) + l;
76     }
77 
78     /**
79      * Atomically adds the given deltas to the current hi and lo values.
80      *
81      * @param deltaHi the delta to apply to the hi value
82      * @param deltaLo the delta to apply to the lo value
83      */
84     public static long encode(long encoded, int deltaHi, int deltaLo) {
85         // while (true) {
86                 return encode(getHi(encoded) + deltaHi, getLo(encoded) + deltaLo);
87         // }
88     }
89 
90 }