blob: cb2fd91ad74b0eb44419117c62f57a904d23364e [file] [log] [blame]
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +01001/*
2 * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010018#pragma once
19#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010020extern "C" {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010021#endif
Max20c7c462017-12-22 14:20:05 +010022#include <osmocom/gsm/gsm_utils.h>
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010023#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010024}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010025#endif
26
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010027#include <time.h>
Max20c7c462017-12-22 14:20:05 +010028
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010029static inline int msecs_to_frames(int msecs) {
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010030 return (msecs * (1024 * 1000 / 4615)) / 1024;
31}
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010032
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010033static inline uint32_t next_fn(uint32_t fn, uint32_t offset)
Max9dabfa22017-05-16 16:10:45 +020034{
35 return (fn + offset) % GSM_MAX_FN;
36}
37
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010038static inline void csecs_to_timespec(unsigned csecs, struct timespec *ts) {
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010039 ts->tv_sec = csecs / 100;
40 ts->tv_nsec = (csecs % 100) * 10000000;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010041}
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010042
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010043#ifdef __cplusplus
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010044template <typename T>
45inline unsigned int pcu_bitcount(T x)
46{
47 unsigned int count = 0;
48 for (count = 0; x; count += 1)
49 x &= x - 1;
50
51 return count;
52}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010053#endif
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010054
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010055static inline uint8_t pcu_lsb(uint8_t x)
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010056{
57 return x & -x;
58}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010059
60/* Used to store a C++ class in a llist used by C code */
61struct llist_item {
62 struct llist_head list; /* item used by llist */
63 void *entry;
64};