"Fossies" - the Fresh Open Source Software Archive

Member "dateutils-0.4.6/contrib/leaps.c" (19 Mar 2019, 3755 Bytes) of package /linux/privat/dateutils-0.4.6.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "leaps.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.4.5_vs_0.4.6.

    1 /*** leaps.c -- materialised leap seconds
    2  *
    3  * Copyright (C) 2012-2019 Sebastian Freundt
    4  *
    5  * Author:  Sebastian Freundt <freundt@ga-group.nl>
    6  *
    7  * This file is part of dateutils.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  *
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  *
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * 3. Neither the name of the author nor the names of any contributors
   21  *    may be used to endorse or promote products derived from this
   22  *    software without specific prior written permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
   25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   27  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
   34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  *
   36  ***/
   37 /* implementation part of leaps.h */
   38 #if !defined INCLUDED_leaps_c_
   39 #define INCLUDED_leaps_c_
   40 
   41 #if defined HAVE_CONFIG_H
   42 # include "config.h"
   43 #endif  /* HAVE_CONFIG_H */
   44 #include <stdint.h>
   45 #include <limits.h>
   46 #include "nifty.h"
   47 #include "leaps.h"
   48 
   49 typedef ssize_t sidx_t;
   50 
   51 #if !defined DEFUN
   52 # define DEFUN
   53 #endif  /* !DEFUN */
   54 
   55 
   56 /* this can be called roughly 100m/sec */
   57 #define DEF_FIND_BEFORE(N, X)                   \
   58 static zidx_t                           \
   59 find_before_##N(                        \
   60     const X v[], size_t nv, X key, zidx_t i, zidx_t min, zidx_t max) \
   61 {                               \
   62 /* Given key K find the index of the transition before */   \
   63     do {                            \
   64         X lo, up;                   \
   65                                 \
   66         lo = v[i];                  \
   67         up = v[i + 1];                  \
   68                                 \
   69         if (key > lo && key <= up) {            \
   70             /* found him */             \
   71             break;                  \
   72         } else if (key > up) {              \
   73             min = i + 1;                \
   74             i = (i + max) / 2;          \
   75         } else if (key <= lo) {             \
   76             max = i - 1;                \
   77             i = (i + min) / 2;          \
   78         }                       \
   79     } while (max > min && i < nv);              \
   80     return i;                       \
   81 }                               \
   82 static const int UNUSED(defined_find_before_##name##_p)
   83 
   84 DEF_FIND_BEFORE(ui32, uint32_t);
   85 DEF_FIND_BEFORE(si32, int32_t);
   86 DEF_FIND_BEFORE(ui64, uint64_t);
   87 DEF_FIND_BEFORE(si64, int64_t);
   88 
   89 
   90 /* public apis */
   91 DEFUN zidx_t
   92 leaps_before_ui32(const uint32_t fld[], size_t nfld, uint32_t key)
   93 {
   94     zidx_t min = 0;
   95     zidx_t max = nfld - 1;
   96     zidx_t this = max / 2;
   97 
   98     return find_before_ui32(fld, nfld, key, this, min, max);
   99 }
  100 
  101 DEFUN zidx_t
  102 leaps_before_si32(const int32_t fld[], size_t nfld, int32_t key)
  103 {
  104     zidx_t min = 0;
  105     zidx_t max = nfld - 1;
  106     zidx_t this = max / 2;
  107 
  108     return find_before_si32(fld, nfld, key, this, min, max);
  109 }
  110 
  111 DEFUN zidx_t
  112 leaps_before_ui64(const uint64_t fld[], size_t nfld, uint64_t key)
  113 {
  114     zidx_t min = 0;
  115     zidx_t max = nfld - 1;
  116     zidx_t this = max / 2;
  117 
  118     return find_before_ui64(fld, nfld, key, this, min, max);
  119 }
  120 
  121 DEFUN zidx_t
  122 leaps_before_si64(const int64_t fld[], size_t nfld, int64_t key)
  123 {
  124     zidx_t min = 0;
  125     zidx_t max = nfld - 1;
  126     zidx_t this = max / 2;
  127 
  128     return find_before_si64(fld, nfld, key, this, min, max);
  129 }
  130 
  131 #endif  /* INCLUDED_leaps_c_ */
  132 /* leaps.c ends here */