1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 // The C implementation of this macro requires a C99 compound literal. In C++, 17 // avoid the compound literal in case -Wc99-extensions is enabled. 18 #ifdef __cplusplus 19 #define _PW_SYSTEM_CLOCK_DURATION(num_ticks) \ 20 (pw_chrono_SystemClock_Duration{.ticks = (num_ticks)}) 21 #else 22 #define _PW_SYSTEM_CLOCK_DURATION(num_ticks) \ 23 ((pw_chrono_SystemClock_Duration){.ticks = (num_ticks)}) 24 #endif // __cplusplus 25 26 // clang-format off 27 28 // ticks_ceil = ((count * clock_period_den + time_unit_num - 1) * time_unit_den) / 29 // (clock_period_num * time_unit_num) 30 #define _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL( \ 31 count, time_unit_seconds_numerator, time_unit_seconds_denominator) \ 32 _PW_SYSTEM_CLOCK_DURATION( \ 33 (((int64_t)(count) * PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR + time_unit_seconds_numerator - 1) * time_unit_seconds_denominator) / \ 34 (PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR * time_unit_seconds_numerator)) 35 36 // ticks_floor = (count * clock_period_den * time_unit_den) / 37 // (clock_period_num * time_unit_num) 38 #define _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR( \ 39 count, time_unit_seconds_numerator, time_unit_seconds_denominator) \ 40 _PW_SYSTEM_CLOCK_DURATION( \ 41 ((int64_t)(count) * PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR * time_unit_seconds_denominator) / \ 42 (PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR * time_unit_seconds_numerator)) 43 44 45 #define PW_SYSTEM_CLOCK_MS_CEIL(milliseconds) \ 46 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(milliseconds, 1000, 1) 47 #define PW_SYSTEM_CLOCK_S_CEIL(seconds) \ 48 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(seconds, 1, 1) 49 #define PW_SYSTEM_CLOCK_MIN_CEIL(minutes) \ 50 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(minutes, 1, 60) 51 #define PW_SYSTEM_CLOCK_H_CEIL(hours) \ 52 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_CEIL(hours, 1, 60 * 60) 53 54 #define PW_SYSTEM_CLOCK_MS_FLOOR(milliseconds) \ 55 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(milliseconds, 1000, 1) 56 #define PW_SYSTEM_CLOCK_S_FLOOR(seconds) \ 57 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(seconds, 1, 1) 58 #define PW_SYSTEM_CLOCK_MIN_FLOOR(minutes) \ 59 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(minutes, 1, 60) 60 #define PW_SYSTEM_CLOCK_H_FLOOR(hours) \ 61 _PW_SYSTEM_CLOCK_TIME_TO_DURATION_FLOOR(hours, 1, 60 * 60) 62 63 // clang-format on 64 65 #define PW_SYSTEM_CLOCK_MS(milliseconds) PW_SYSTEM_CLOCK_MS_CEIL(milliseconds) 66 #define PW_SYSTEM_CLOCK_S(seconds) PW_SYSTEM_CLOCK_S_CEIL(seconds) 67 #define PW_SYSTEM_CLOCK_MIN(minutes) PW_SYSTEM_CLOCK_MIN_CEIL(minutes) 68 #define PW_SYSTEM_CLOCK_H(hours) PW_SYSTEM_CLOCK_H_CEIL(hours) 69