| 1 |
#ifndef AWS_COMMON_CLOCK_INL |
| 2 |
#define AWS_COMMON_CLOCK_INL |
| 3 |
|
| 4 |
/** |
| 5 |
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 |
* SPDX-License-Identifier: Apache-2.0. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/common.h> |
| 11 |
#include <aws/common/math.h> |
| 12 |
|
| 13 |
AWS_EXTERN_C_BEGIN |
| 14 |
|
| 15 |
/** |
| 16 |
* Converts 'timestamp' from unit 'convert_from' to unit 'convert_to', if the units are the same then 'timestamp' is |
| 17 |
* returned. If 'remainder' is NOT NULL, it will be set to the remainder if convert_from is a more precise unit than |
| 18 |
* convert_to (but only if the old frequency is a multiple of the new one). If conversion would lead to integer |
| 19 |
* overflow, the timestamp returned will be the highest possible time that is representable, i.e. UINT64_MAX. |
| 20 |
*/ |
| 21 |
AWS_STATIC_IMPL uint64_t |
| 22 |
aws_timestamp_convert_u64(uint64_t ticks, uint64_t old_frequency, uint64_t new_frequency, uint64_t *remainder) { |
| 23 |
|
| 24 |
AWS_FATAL_ASSERT(old_frequency > 0 && new_frequency > 0); |
| 25 |
|
| 26 |
/* |
| 27 |
* The remainder, as defined in the contract of the original version of this function, only makes mathematical |
| 28 |
* sense when the old frequency is a positive multiple of the new frequency. The new convert function needs to be |
| 29 |
* backwards compatible with the old version's remainder while being a lot more accurate with its conversions |
| 30 |
* in order to handle extreme edge cases of large numbers. |
| 31 |
*/ |
| 32 |
if (remainder != NULL) { |
| 33 |
*remainder = 0; |
| 34 |
/* only calculate remainder when going from a higher to lower frequency */ |
| 35 |
if (new_frequency < old_frequency) { |
| 36 |
uint64_t frequency_remainder = old_frequency % new_frequency; |
| 37 |
/* only calculate remainder when the old frequency is evenly divisible by the new one */ |
| 38 |
if (frequency_remainder == 0) { |
| 39 |
uint64_t frequency_ratio = old_frequency / new_frequency; |
| 40 |
*remainder = ticks % frequency_ratio; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Now do the actual conversion. |
| 47 |
*/ |
| 48 |
uint64_t old_seconds_elapsed = ticks / old_frequency; |
| 49 |
uint64_t old_remainder = ticks - old_seconds_elapsed * old_frequency; |
| 50 |
|
| 51 |
uint64_t new_ticks_whole_part = aws_mul_u64_saturating(old_seconds_elapsed, new_frequency); |
| 52 |
|
| 53 |
/* |
| 54 |
* This could be done in one of three ways: |
| 55 |
* |
| 56 |
* (1) (old_remainder / old_frequency) * new_frequency - this would be completely wrong since we know that |
| 57 |
* (old_remainder / old_frequency) < 1 = 0 |
| 58 |
* |
| 59 |
* (2) old_remainder * (new_frequency / old_frequency) - this only gives a good solution when new_frequency is |
| 60 |
* a multiple of old_frequency |
| 61 |
* |
| 62 |
* (3) (old_remainder * new_frequency) / old_frequency - this is how we do it below, the primary concern is if |
| 63 |
* the initial multiplication can overflow. For that to be the case, we would need to be using old and new |
| 64 |
* frequencies in the billions. This does not appear to be the case in any current machine's hardware counters. |
| 65 |
* |
| 66 |
* Ignoring arbitrary frequencies, even a nanosecond to nanosecond conversion would not overflow either. |
| 67 |
* |
| 68 |
* If this did become an issue, we would potentially need to use intrinsics/platform support for 128 bit math. |
| 69 |
* |
| 70 |
* For review consideration: |
| 71 |
* (1) should we special case frequencies being a multiple of the other? |
| 72 |
* (2) should we special case frequencies being the same? A ns-to-ns conversion does the full math and |
| 73 |
* approaches overflow (but cannot actually do so). |
| 74 |
*/ |
| 75 |
uint64_t new_ticks_remainder_part = aws_mul_u64_saturating(old_remainder, new_frequency) / old_frequency; |
| 76 |
|
| 77 |
return aws_add_u64_saturating(new_ticks_whole_part, new_ticks_remainder_part); |
| 78 |
} |
| 79 |
|
| 80 |
AWS_STATIC_IMPL uint64_t aws_timestamp_convert( |
| 81 |
uint64_t timestamp, |
| 82 |
enum aws_timestamp_unit convert_from, |
| 83 |
enum aws_timestamp_unit convert_to, |
| 84 |
uint64_t *remainder) { |
| 85 |
|
| 86 |
return aws_timestamp_convert_u64(timestamp, convert_from, convert_to, remainder); |
| 87 |
} |
| 88 |
|
| 89 |
AWS_EXTERN_C_END |
| 90 |
|
| 91 |
#endif /* AWS_COMMON_CLOCK_INL */ |