View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import javax.annotation.concurrent.NotThreadSafe;
35  import javax.annotation.concurrent.ThreadSafe;
36  
37  /**
38   * @author zoly
39   */
40  public final class IntMath {
41  
42      private IntMath() { }
43  
44      public static int closestPowerOf2(final int number) {
45          return number == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(number - 1);
46      }
47  
48      public static int closestPowerOf2Number(final int number) {
49         return number == 0 ? 0 : 1 << (32 - Integer.numberOfLeadingZeros(number - 1));
50      }
51  
52      /**
53       * A very fast Pseudo random generator.
54       * use of this random is appropriate when you need the fastest random that you plan to use in a single
55       * thread.
56       * If you need a thread-safe random, please use JDK ThreadLocalRandom, which will be your best option.
57       */
58      @NotThreadSafe
59      public static final class XorShift32 implements IntSequence {
60          // XorShift128 PRNG with a 2^32-1 period.
61          private int x;
62  
63          public XorShift32() {
64            x = (int) System.currentTimeMillis();
65            while (x == 0) {
66              x = (int) System.currentTimeMillis();
67            }
68          }
69  
70          @Override
71          public int nextInt() {
72              x ^= (x << 6);
73              x ^= (x >>> 21);
74              return x ^ (x << 7);
75          }
76      }
77  
78      /**
79       * @deprecated please use JDK java.util.concurrent.ThreadLocalRandom instead.
80       * The JDK implementation uses local fields in the Thread class instead of a classic ThreadLocal,
81       * which makes it faster...
82       */
83      @ThreadSafe
84      @Deprecated
85      public static final class XorShift32ThreadSafe implements IntSequence {
86  
87        private final ThreadLocal<XorShift32> rnd = new ThreadLocalRandom();
88  
89        public static final class Singleton {
90  
91          public static final XorShift32ThreadSafe INSTANCE = new XorShift32ThreadSafe();
92  
93        }
94  
95        @Override
96        public int nextInt() {
97          return rnd.get().nextInt();
98        }
99  
100       private static class ThreadLocalRandom extends ThreadLocal<XorShift32> {
101 
102         @Override
103         protected XorShift32 initialValue() {
104           return new XorShift32();
105         }
106       }
107     }
108 
109 
110     @NotThreadSafe
111     public static final class XorShift128 implements IntSequence {
112         // XorShift128 PRNG with a 2^128-1 period.
113         private int x = System.identityHashCode(this);
114         private int y = -938745813;
115         private int z = 452465366;
116         private int w = 1343246171;
117 
118         @Override
119         public int nextInt() {
120             int t = x ^ (x << 15);
121             x = y; y = z; z = w;
122             w = (w ^ (w >>> 21)) ^ (t ^ (t >>> 4));
123             return w;
124         }
125     }
126 
127 
128 }