sPyNNaker neural_modelling  7.4.2
stoc_exp_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 The University of Manchester
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
19 
20 static inline uint32_t stoc_exp_ceil_accum(UREAL value) {
21  uint32_t bits = bitsuk(value);
22  uint32_t integer = bits >> 16;
23  uint32_t fraction = bits & 0xFFFF;
24  if (fraction > 0) {
25  return integer + 1;
26  }
27  return integer;
28 }
29 
34 static const uint32_t MIN_TAU = 0x10B55;
35 
37 static inline uint32_t get_probability(UREAL tau, REAL p) {
38 
39  if (p >= 0) {
40 
41  // If tau is already more than 1, it will never get smaller here,
42  // so just immediately return a probability of "1"
43  if (tau >= 1.0k) {
44  return 0xFFFFFFFF;
45  }
46 
47  // The amount of left shift that will result in a tau > 1 (where tau
48  // is 16-bits integer, 16-bits fractional, and < 1.0k, so we expect
49  // at least 16 leading zeros, thus this can only be >= 0). Note a
50  // a 1 at bit 16 means clz = 16, but we can shift 1 place before >= 1
51  // so we subtract 15 from clz to get the right number.
52  uint32_t over_left_shift = __builtin_clz(bitsuk(tau)) - 15;
53 
54  // If tau is going to be shifted by this amount
55  if (p >= over_left_shift) {
56  return 0xFFFFFFFF;
57  }
58 
59  // Shift left by integer part to perform power of 2
60  uint64_t accumulator = ((uint64_t) bitsuk(tau)) << (bitsk(p) >> 15);
61  uint32_t fract_bits = bitsk(p) & 0x7FFF;
62 
63  // Multiply in fractional powers for each non-zero fractional bits
64  for (uint32_t i = 0; i < 15; i++) {
65  uint32_t bit = (fract_bits >> (14 - i)) & 0x1;
66  if (bit) {
67  // Do a U1616 * U1616 multiply here, which is safe in 64-bits
68  accumulator = (accumulator * fract_powers_2[i]) >> 16;
69 
70  // If we are >= 1, return now as won't get smaller
71  if (accumulator >= bitsuk(1.0ulk)) {
72  return 0xFFFFFFFF;
73  }
74  }
75  }
76 
77  // Multiply accumulated fraction (must be <= 1 here) by 0xFFFFFFFF
78  // to get final answer
79  return (uint32_t) ((accumulator * 0xFFFFFFFFL) >> 16);
80  } else {
81  // If tau is too big, we will never make it small enough with negative
82  // powers, so just return probability of 1
83  if (bitsuk(tau) > MIN_TAU) {
84  return 0xFFFFFFFF;
85  }
86 
87  // Negative left shift = positive right shift; have to multiply here
88  // as accum negating seems to fail!
89  REAL val = p * REAL_CONST(-1);
90 
91  // The amount of right shift that will make the MSB of tau disappear,
92  // and so the value will be 0. The most number of leading zeros is 32,
93  // so this is always >= 0. If we have a bit in position 31 (a very big
94  // tau), this clz = 0 so this is 32, which means we can shift by 32
95  uint32_t over_right_shift = 32 - __builtin_clz(bitsuk(tau));
96 
97  // If p <= 0, tau can only get smaller through multiplication with
98  // fractional powers, so there is no point in doing the calculation if
99  // it will already be shifted out of range of an accum
100  if (val >= over_right_shift) {
101  return 0;
102  }
103 
104  // Shift right by integer value to perform negative power of 2
105  uint64_t accumulator = ((uint64_t) bitsuk(tau)) >> (bitsk(val) >> 15);
106  uint32_t fract_bits = bitsk(val) & 0x7FFF;
107 
108  // Multiply in fractional powers for each non-zero fractional bits
109  for (uint32_t i = 0; i < 15; i++) {
110  uint32_t bit = (fract_bits >> (14 - i)) & 0x1;
111  if (bit) {
112  // Do a U1616 * U1616 multiply here
113  accumulator = (accumulator * fract_powers_half[i]) >> 16;
114 
115  // If we have reached a value of 0, return
116  if (accumulator == 0) {
117  return 0;
118  }
119  }
120  }
121 
122  // Multiply accumulated fraction (must be <= 1 here) by 0xFFFFFFFF
123  // to get final answer
124  return (uint32_t) ((accumulator * 0xFFFFFFFFL) >> 16);
125  }
126 }
#define REAL_CONST(x)
Define a constant of type REAL.
Definition: maths-util.h:104
unsigned accum UREAL
Type used for "unsigned real" numbers.
Definition: maths-util.h:94
accum REAL
Type used for "real" numbers.
Definition: maths-util.h:91
static const uint32_t fract_powers_2[]
Definition: maths-util.h:265
static const uint32_t fract_powers_half[]
Definition: maths-util.h:274
static const uint32_t MIN_TAU
static uint32_t get_probability(UREAL tau, REAL p)
Calculates the probability as a uint32_t from 0 to 0xFFFFFFFF (which is 1)