subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

ringbuffer.c (2334B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2008 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #include <assert.h>
     23 
     24 #include "ringbuffer.h"
     25 
     26 
     27 static unsigned int
     28 modulo (unsigned int x, unsigned int n, unsigned int d)
     29 {
     30 	unsigned int result = 0;
     31 	if (d > x) {
     32 #if 0
     33 		result = (n - (d - x) % n) % n;
     34 #else
     35 		unsigned int m = (d - x) % n;
     36 		result = m ? n - m : m;
     37 #endif
     38 	} else {
     39 		result = (x - d) % n;
     40 	}
     41 
     42 	return result + d;
     43 }
     44 
     45 
     46 static unsigned int
     47 distance (unsigned int a, unsigned int b, unsigned int n, unsigned int mode)
     48 {
     49 	unsigned int result = 0;
     50 	if (a > b) {
     51 #if 0
     52 		result = (n - (a - b) % n) % n;
     53 #else
     54 		unsigned int m = (a - b) % n;
     55 		result = m ? n - m : m;
     56 #endif
     57 	} else {
     58 		result = (b - a) % n;
     59 	}
     60 
     61 	if (result == 0) {
     62 		return (mode == 0 ? 0 : n);
     63 	} else {
     64 		return result;
     65 	}
     66 }
     67 
     68 
     69 unsigned int
     70 ringbuffer_normalize (unsigned int a, unsigned int begin, unsigned int end)
     71 {
     72 	assert (end > begin);
     73 
     74 	unsigned int n = end - begin;
     75 	return modulo (a, n, begin);
     76 }
     77 
     78 
     79 unsigned int
     80 ringbuffer_distance (unsigned int a, unsigned int b, int mode, unsigned int begin, unsigned int end)
     81 {
     82 	assert (end > begin);
     83 
     84 	unsigned int n = end - begin;
     85 	return distance (a, b, n, mode);
     86 }
     87 
     88 
     89 unsigned int
     90 ringbuffer_increment (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end)
     91 {
     92 	assert (end > begin);
     93 
     94 	unsigned int n = end - begin;
     95 	return modulo (a + delta % n, n, begin);
     96 }
     97 
     98 
     99 unsigned int
    100 ringbuffer_decrement (unsigned int a, unsigned int delta, unsigned int begin, unsigned int end)
    101 {
    102 	assert (end > begin);
    103 
    104 	unsigned int n = end - begin;
    105 	return modulo (a + n - delta % n, n, begin);
    106 }