File: var_base_reader.h

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (81 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2012 Shlomi Fish
 */
// var_base_reader.h - read from a packed integer using digits of variable
// bases. Also see var_base_writer.h which complements it.
#pragma once

#include "var_base_int.h"

typedef struct
{
    var_base_int data;
#ifndef FCS_USE_INT128_FOR_VAR_BASE
    /* To avoid memory fragmentation, we keep those here and re use them. */
    var_base_int data_byte_offset;
    var_base_int r;
#endif
} fcs_var_base_reader;

#ifdef FCS_USE_INT128_FOR_VAR_BASE
#define fc_solve_var_base_reader_init(s)
#else
static inline void fc_solve_var_base_reader_init(fcs_var_base_reader *const s)
{
    FCS_var_base_int__init(s->data);
    FCS_var_base_int__init(s->data_byte_offset);
    FCS_var_base_int__init(s->r);
}
#endif
static inline void fc_solve_var_base_reader_start(fcs_var_base_reader *const s,
    const unsigned char *const data, const size_t data_len)
{
    FCS_var_base_int__set_ui(s->data, 0);
#define NUM_BITS 8
    unsigned long shift_count = 0;
    for (size_t count = 0; count < data_len; count++, shift_count += NUM_BITS)
    {
#ifdef FCS_USE_INT128_FOR_VAR_BASE
        s->data |= (((var_base_int)data[count]) << shift_count);
#else
        FCS_var_base_int__set_ui(
            s->data_byte_offset, (unsigned long)data[count]);
        FCS_var_base_int__left_shift(s->data_byte_offset, shift_count);
        FCS_var_base_int__add(s->data, s->data_byte_offset);
#endif
    }
}

static inline unsigned long fc_solve_var_base_reader_read(
    fcs_var_base_reader *const reader, const unsigned long base)
{
#ifdef FCS_USE_INT128_FOR_VAR_BASE
    var_base_int rem;
#else
#define rem (reader->r)
#endif
    FCS_var_base_int__mod_div(reader->data, rem, base);

    return FCS_var_base_int__get_ui(rem);
#ifndef FCS_USE_INT128_FOR_VAR_BASE
#undef rem
#endif
}

#ifdef FCS_USE_INT128_FOR_VAR_BASE
#define fc_solve_var_base_reader_release(s)
#else
static inline void fc_solve_var_base_reader_release(
    fcs_var_base_reader *const s)
{
    FCS_var_base_int__clear(s->data);
    FCS_var_base_int__clear(s->r);
    FCS_var_base_int__clear(s->data_byte_offset);
}
#endif