sPyNNaker neural_modelling 7.3.1
Loading...
Searching...
No Matches
common_kernel.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 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
21#include "common_kernel.h"
22#include <stdlib.h>
23
29uint16_t uidiv(uint32_t dividend, uint16_t divider, uint16_t *remainder) {
30 if (dividend == 0 || dividend < divider) {
31 *remainder = (uint16_t) dividend;
32 return 0;
33 }
34
35 // Assumes that the dividend is less than 1<<31
36 div_t results = div((int) dividend, (int) (uint32_t) divider);
37 *remainder = (uint16_t) results.rem;
38 return (uint16_t) results.quot;
39}
40
41void post_in_pre_world(uint16_t in_row, uint16_t in_col,
42 uint16_t start_row, uint16_t start_col,
43 uint16_t step_row, uint16_t step_col,
44 uint16_t *out_row, uint16_t *out_col) {
45 *out_row = start_row + in_row * step_row;
46 *out_col = start_col + in_col * step_col;
47}
48
49void pre_in_post_world(uint16_t in_row, uint16_t in_col, uint16_t start_row,
50 uint16_t start_col, uint16_t step_row, uint16_t step_col,
51 int16_t *out_row, int16_t *out_col) {
52 int16_t d = (int16_t) (in_row - start_row - 1);
53 uint16_t r;
54 if (d == 0) {
55 *out_row = 1;
56 } else if (d < 0) {
57 d = (int16_t) uidiv((uint16_t) (-d), step_row, &r);
58 if (r == 0) {
59 *out_row = -d + 1;
60 } else {
61 *out_row = -d; // Note: e.g. ((-1) // 4) is not the same as (- (1 // 4))
62 }
63 } else {
64 d = (int16_t) uidiv((uint16_t) d, step_row, &r);
65 *out_row = d + 1;
66 }
67
68 d = (int16_t) (in_col - start_col - 1);
69 if (d == 0) {
70 *out_col = 1;
71 } else if (d < 0) {
72 d = (int16_t) uidiv((uint16_t) (-d), step_col, &r);
73 if (r == 0) {
74 *out_col = -d + 1;
75 } else {
76 *out_col = -d; // Note: e.g. ((-1) // 4) is not the same as (- (1 // 4))
77 }
78 } else {
79 d = (int16_t) uidiv((uint16_t) d, step_col, &r);
80 *out_col = d + 1;
81 }
82}
void pre_in_post_world(uint16_t in_row, uint16_t in_col, uint16_t start_row, uint16_t start_col, uint16_t step_row, uint16_t step_col, int16_t *out_row, int16_t *out_col)
Get the pre's coordinates in the post's coordinate system.
uint16_t uidiv(uint32_t dividend, uint16_t divider, uint16_t *remainder)
Unsigned integer division.
void post_in_pre_world(uint16_t in_row, uint16_t in_col, uint16_t start_row, uint16_t start_col, uint16_t step_row, uint16_t step_col, uint16_t *out_row, uint16_t *out_col)
Get the post's coordinates in the pre's coordinate system.
Common functions for kernel generation.