sPyNNaker neural_modelling 7.3.1
Loading...
Searching...
No Matches
maths.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 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#ifndef MATHS_H
20#define MATHS_H
21
22// Standard includes
24#include <debug.h>
25#include <spin1_api.h>
26
27//---------------------------------------
28// Macros
29//---------------------------------------
34#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
39#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
40
44typedef struct int16_lut {
45 uint16_t size;
46 uint16_t shift;
47 int16_t values[];
48} int16_lut;
49
50//---------------------------------------
51// Plasticity maths function inline implementation
52//---------------------------------------
57static inline int16_lut *maths_copy_int16_lut(address_t *address) {
58 int16_lut *sdram_lut = (int16_lut *) *address;
59 uint32_t size = sizeof(int16_lut) + (sdram_lut->size * sizeof(int16_t));
60 int16_lut *lut = spin1_malloc(size);
61 if (lut == NULL) {
62 log_error("Not enough space to allocate LUT. Try reducing the timestep,"
63 " the number of neurons per core, or the tau value; size = %u", size);
65 }
66 spin1_memcpy(lut, sdram_lut, size);
67
68 // Pad to number of words (+ 1 for size / shift header)
69 const uint32_t num_words = (lut->size / 2) + (((lut->size & 1) != 0) ? 1 : 0);
70 *address += num_words + 1;
71
72 return lut;
73}
74
79static inline int32_t maths_lut_exponential_decay(
80 uint32_t time, const int16_lut *lut) {
81 // Calculate lut index
82 uint32_t lut_index = time >> lut->shift;
83
84 // Return value from LUT
85 return (lut_index < lut->size) ? lut->values[lut_index] : 0;
86}
87
92static inline int32_t maths_clamp_pot(int32_t x, uint32_t shift) {
93 uint32_t y = x >> shift;
94 if (y) {
95 x = ~y >> (32 - shift);
96 }
97
98 return x;
99}
100
101//---------------------------------------
109static inline int32_t maths_mul_16x16(int16_t x, int16_t y) {
110 return x * y;
111}
112
113//---------------------------------------
119static inline int32_t maths_fixed_mul16(
120 int32_t a, int32_t b, const int32_t fixed_point_position) {
121 // Multiply lower 16-bits of a and b together
122 return __smulbb(a, b) >> fixed_point_position;
123}
124
125//---------------------------------------
131static inline int32_t maths_fixed_mul32(
132 int32_t a, int32_t b, const int32_t fixed_point_position) {
133 int32_t mul = a * b;
134
135 // Shift down and return
136 return (mul >> fixed_point_position);
137}
138
139#endif // MATHS_H
uint32_t time
The current timer tick value.
Definition c_main.c:94
uint32_t * address_t
void log_error(const char *message,...)
int16_t values[]
Table of actual values.
Definition maths.h:47
static int32_t maths_clamp_pot(int32_t x, uint32_t shift)
Clamp to fit in number of bits.
Definition maths.h:92
static int16_lut * maths_copy_int16_lut(address_t *address)
Copy a Lookup Table from SDRAM to DTCM, updating the address.
Definition maths.h:57
static int32_t maths_mul_16x16(int16_t x, int16_t y)
multiply two 16-bit numbers to get a 32-bit number.
Definition maths.h:109
static int32_t maths_fixed_mul16(int32_t a, int32_t b, const int32_t fixed_point_position)
multiply two 16-bit fixed point numbers (encoded in int32_t)
Definition maths.h:119
static int32_t maths_lut_exponential_decay(uint32_t time, const int16_lut *lut)
Get value from lookup table.
Definition maths.h:79
static int32_t maths_fixed_mul32(int32_t a, int32_t b, const int32_t fixed_point_position)
multiply two 32-bit fixed point numbers (encoded in int32_t)
Definition maths.h:131
uint16_t shift
Mapping from time to table index.
Definition maths.h:46
uint16_t size
Number of entries in table.
Definition maths.h:45
Lookup Table of 16-bit integers.
Definition maths.h:44
Data type definitions for SpiNNaker Neuron-modelling.
RTE_SWERR
void rt_error(uint code,...)
#define NULL
void spin1_memcpy(void *dst, void const *src, uint len)