LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/lib/os - div64.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 46 0
Test Date: 2026-11-06 14:43:19 Functions: 0.0 % 4 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 22 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * SPDX-FileCopyrightText: Copyright (c) 2026 BayLibre SAS
       3                 :             :  * SPDX-License-Identifier: Apache-2.0
       4                 :             :  */
       5                 :             : 
       6                 :             : #include <zephyr/sys/__assert.h>
       7                 :             : #include <zephyr/sys/div64.h>
       8                 :             : 
       9                 :             : /* Nothing here is used when the header divides plainly. */
      10                 :             : #if !defined(Z_DIV64_PLAIN_RECIP)
      11                 :             : 
      12                 :             : /* n >> s for s from 0 to 31, without libgcc's 64-bit shift */
      13                 :           0 : static ALWAYS_INLINE uint64_t div64_shr(uint64_t n, uint32_t s)
      14                 :             : {
      15                 :           0 :         uint32_t lo = (uint32_t)n;
      16                 :           0 :         uint32_t hi = (uint32_t)(n >> 32);
      17                 :             : 
      18                 :           0 :         lo = (lo >> s) | ((hi << 1) << (31U - s));
      19                 :           0 :         hi >>= s;
      20                 :             : 
      21                 :           0 :         return ((uint64_t)hi << 32) | lo;
      22                 :             : }
      23                 :             : 
      24                 :           0 : uint64_t z_div64_u32_recip(uint64_t n, uint64_t m, uint32_t flags)
      25                 :             : {
      26         [ #  # ]:           0 :         if ((flags & Z_DIV64_FLAG_SHIFT) == 0U) {
      27                 :           0 :                 n = z_div64_mulhi(n, m, (flags & Z_DIV64_FLAG_BIAS) != 0U, false);
      28                 :             :         }
      29                 :             : 
      30                 :           0 :         return div64_shr(n, flags & Z_DIV64_SHIFT_MASK);
      31                 :             : }
      32                 :             : 
      33                 :           0 : uint64_t z_div64_shared_div(uint64_t n, const struct sys_div64_recip *r)
      34                 :             : {
      35                 :           0 :         return z_div64_u32_recip(n, r->m, r->flags);
      36                 :             : }
      37                 :             : 
      38                 :             : #define DEFINE_SHARED(name, v)                                                                     \
      39                 :             :         const struct sys_div64_recip z_div64_recip_##name =                                        \
      40                 :             :                 SYS_DIV64_RECIP_INIT(((v) != 0U) ? (v) : 1U);
      41                 :             : 
      42                 :             : Z_DIV64_SHARED_LIST(DEFINE_SHARED)
      43                 :             : 
      44                 :             : /* The derivation of Z_DIV64_M() and Z_DIV64_FLAGS(), for a run-time divisor. */
      45                 :           0 : void sys_div64_recip_init(struct sys_div64_recip *r, uint32_t d)
      46                 :             : {
      47                 :           0 :         uint32_t k = Z_DIV64_LOG2(d);
      48                 :           0 :         uint32_t p = 1U << k;
      49                 :           0 :         uint32_t s, dn, dh, dl, rem;
      50                 :           0 :         uint64_t m = 0U;
      51                 :           0 :         uint32_t zeros;
      52                 :           0 :         bool bias;
      53                 :             : 
      54         [ #  # ]:           0 :         __ASSERT(d != 0U, "division by zero");
      55                 :             : 
      56                 :           0 :         r->div = d;
      57                 :             : 
      58         [ #  # ]:           0 :         if (IS_POWER_OF_TWO(d)) {
      59                 :           0 :                 r->m = 0U;
      60                 :           0 :                 r->flags = Z_DIV64_ILOG2(d) | Z_DIV64_FLAG_SHIFT;
      61                 :           0 :                 return;
      62                 :             :         }
      63                 :             : 
      64                 :             :         /*
      65                 :             :          * m = 2^(64 + k) / d = 2^(64 + k + s) / dn, with dn = d << s normalized,
      66                 :             :          * in four 16-bit digits, as Knuth's algorithm D. rem starts as the top 32
      67                 :             :          * bits of the dividend. A digit estimated from dn's top half is checked
      68                 :             :          * against its bottom half, which makes it exact.
      69                 :             :          */
      70                 :           0 :         s = 31U - k;
      71                 :           0 :         dn = d << s;
      72                 :           0 :         dh = dn >> 16;
      73                 :           0 :         dl = dn & 0xffffU;
      74                 :           0 :         rem = 1U << 31;
      75         [ #  # ]:           0 :         for (uint32_t i = 0U; i < 4U; i++) {
      76                 :           0 :                 uint32_t q = rem / dh;
      77                 :           0 :                 uint32_t rh = rem - (q * dh);
      78                 :             : 
      79   [ #  #  #  # ]:           0 :                 while ((q > 0xffffU) || ((q * dl) > (rh << 16))) {
      80                 :           0 :                         q--;
      81                 :           0 :                         rh += dh;
      82         [ #  # ]:           0 :                         if (rh > 0xffffU) {
      83                 :             :                                 break;
      84                 :             :                         }
      85                 :             :                 }
      86                 :           0 :                 rem = (rem << 16) - (q * dn);
      87                 :           0 :                 m = (m << 16) | q;
      88                 :             :         }
      89                 :           0 :         rem >>= s;
      90                 :             : 
      91   [ #  #  #  # ]:           0 :         bias = (rem != 0U) && (rem < (d - p));
      92         [ #  # ]:           0 :         if ((rem != 0U) && !bias) {
      93                 :           0 :                 m++;
      94                 :             :         }
      95                 :             : 
      96                 :           0 :         zeros = __builtin_ctz((uint32_t)m | p);
      97                 :           0 :         r->m = div64_shr(m, zeros);
      98         [ #  # ]:           0 :         r->flags = (k - zeros) | (bias ? Z_DIV64_FLAG_BIAS : 0U);
      99                 :             : }
     100                 :             : 
     101                 :             : #endif /* !Z_DIV64_PLAIN_RECIP */
        

Generated by: LCOV version 2.0-1