spinn_common  7.4.2
Support code for SpiNNaker applications.
utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 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 
36 #ifndef __UTILS_H__
37 #define __UTILS_H__
38 
39 #include <stdint.h>
40 
45 static inline uint32_t __hi(uint64_t x) {
46  return (uint32_t) (x >> 32);
47 }
48 
53 static inline uint32_t __lo(uint64_t x) {
54  return __hi(x << 32);
55 }
56 
61 static inline uint64_t round64(uint64_t x) {
62  uint64_t r = (uint64_t) __hi(x);
63 
64  if (__lo(x) >= INT32_MAX) {
65  r++;
66  }
67  return r;
68 }
69 
76 static inline uint64_t scale64(
77  uint64_t x,
78  uint32_t y)
79 {
80  uint64_t r = round64((uint64_t) __lo(x) * (uint64_t) y);
81 
82  r += (uint64_t) __hi(x) * (uint64_t) y;
83  return r;
84 }
85 
92 static inline uint32_t scale32(
93  uint32_t x,
94  uint32_t y)
95 {
96  return (uint32_t) round64((uint64_t) x * (uint64_t) y);
97 }
98 
102 static inline uint32_t log_next_power_of_2(uint32_t v) {
103  return 32 - __builtin_clz(v);
104 }
105 
109 static inline uint32_t ilog_2(uint32_t v) {
110  return 31 - __builtin_clz(v);
111 }
112 
116 static inline uint32_t next_power_of_2(uint32_t v) {
117  return 1 << log_next_power_of_2(v);
118 }
119 
123 static inline bool is_power_of_2(uint32_t v) {
124  return (v & (v - 1)) == 0;
125 }
126 
127 #endif /*__UTILS_H__*/
static uint32_t next_power_of_2(uint32_t v)
Returns the next highest power of 2 of a value.
Definition: utils.h:116
static uint64_t round64(uint64_t x)
The function treats the 64-bit number as if it were a 32-bit integer and a 32-bit fraction,...
Definition: utils.h:61
static uint32_t log_next_power_of_2(uint32_t v)
Returns the log of the next highest power of 2 of a value.
Definition: utils.h:102
static bool is_power_of_2(uint32_t v)
Returns True if the value is a power of 2.
Definition: utils.h:123
static uint32_t scale32(uint32_t x, uint32_t y)
The function scales the 32-bit number x, treating y as if it were an unsigned long fract,...
Definition: utils.h:92
static uint32_t ilog_2(uint32_t v)
Returns the integer part of the log to base 2 of a number.
Definition: utils.h:109
static uint64_t scale64(uint64_t x, uint32_t y)
The function scales the 64-bit number x, treating y as if it were an unsigned long fract,...
Definition: utils.h:76
static uint32_t __hi(uint64_t x)
This function returns the most significant 32-bit word of a 64-bit unsigned integer.
Definition: utils.h:45
static uint32_t __lo(uint64_t x)
This function returns the least significant 32-bit word of a 64-bit unsigned integer.
Definition: utils.h:53