sPyNNaker neural_modelling  7.4.2
type_writers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 
22 #include <common-typedefs.h>
23 #include <debug.h>
24 
25 typedef enum type {
26  S1615,
27  UINT32,
28  INT32,
29  U032
30 } type;
31 
32 typedef void (*type_writer_func_t)(void *, accum);
33 
34 typedef struct type_info {
35  type type_id;
36  uint32_t size;
37  type_writer_func_t writer;
38 } type_info;
39 
40 static void write_s1615(void *address, accum value) {
41  accum *values = (accum *) address;
42  values[0] = value;
43 }
44 
45 static void write_uint32(void *address, accum value) {
46  uint32_t *values = (uint32_t *) address;
47  values[0] = (uint32_t) value;
48 }
49 
50 static void write_int32(void *address, accum value) {
51  int32_t *values = (int32_t *) address;
52  values[0] = (int32_t) value;
53 }
54 
55 static void write_u032(void *address, accum value) {
56  unsigned long fract *values = (unsigned long fract *) address;
57  values[0] = (unsigned long fract) value;
58 }
59 
60 static type_info type_writers[] = {
61  {S1615, sizeof(accum), write_s1615},
62  {UINT32, sizeof(uint32_t), write_uint32},
63  {INT32, sizeof(int32_t), write_int32},
64  {U032, sizeof(unsigned long fract), write_u032}
65 };
66 
67 static type_info *get_type_writer(type t) {
68  if (t >= sizeof(type_writers) / sizeof(*type_writers)) {
69  // Bogus index is bad! And otherwise hard to debug!
70  log_error("type id=%u is outside sane range", t);
71  rt_error(RTE_SWERR);
72  }
73  return &type_writers[t];
74 }