File: fast_div_generator.cpp

package info (click to toggle)
libdivide 5.2.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,564 kB
  • sloc: ansic: 131,647; cpp: 70,136; python: 47; makefile: 3
file content (174 lines) | stat: -rw-r--r-- 5,365 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Silence MSVC sprintf unsafe warnings
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <sstream>
#include <iomanip>
#include <limits>
#include <string>
#include <type_traits>
#include <algorithm>
#include <functional>
#include <ctype.h>

#include "libdivide.h"
#include "type_mappings.h"

inline std::string to_lower(std::string str) {
    std::transform(str.begin(), str.end(), str.begin(),
               [](unsigned char c){ return (char)::tolower(c); });
    return str;
}

inline std::string to_upper(std::string str) {
    std::transform(str.begin(), str.end(), str.begin(),
               [](unsigned char c){ return (char)::toupper(c); });
    return str;
}

template <typename _IntT>
void generator(std::function<void(_IntT)> pGen) {
    // We're dealing with integer division. For any denominator:
    //  if (numerator<denominator) numerator/denominator==0
    //  if (numerator==denominator) numerator/denominator==1
    // Compilers know this. So there's no point in generating constants > MAX/2. 
    for (_IntT denom = std::numeric_limits<_IntT>::min()/2;
            denom<std::numeric_limits<_IntT>::max()/2;
            ++denom ) {
        if (denom!=0) {
            pGen(denom);
        }
    }
}

template <typename _IntT>
static std::string const_macro_name(_IntT denom) {
    std::string name(to_upper(type_tag<_IntT>::get_tag()));
    name += "LD_DENOM_";
    if (denom<0) {
        name += "MINUS_";
        denom = -denom;
    }
    name += std::to_string(denom);
    return name;
}

template <typename _IntT>
static std::string magic_tostr(_IntT magic) {
    // Don't use hex representation here. Some hex literals are considered unsigned, so
    // will cause a warning when cast to unsigned
    std::stringstream stream;
    stream << "(" << type_name<_IntT>::get_name() << ")" << magic;
    return stream.str();
}

static std::string more_tostr(uint8_t more) {
    std::stringstream stream;
    stream << "(uint8_t)" << (uint16_t)more;
    return stream.str();
}


template <typename _IntT>
void generate_constant_macro(_IntT denom) {
    auto magic = libdivide_gen(denom);
    std::cout << "#define " << const_macro_name(denom)+"_MAGIC" << " "  << magic_tostr(magic.magic) << "\n";
    std::cout << "#define " << const_macro_name(denom)+"_MORE" << " "  << more_tostr(magic.more) << "\n";
} 


template <typename _IntT>
void generate_specialized_template(_IntT denom) {
    std::cout << "template<> struct libdivide_constants"
        << "<" << type_name<_IntT>::get_name() << "," << denom << "> "
        << "{ "
        << "static constexpr " << struct_selector<_IntT>::get_name() << " libdivide = { "
        <<              ".magic = " << const_macro_name(denom)+"_MAGIC, "
        <<              ".more = " <<  const_macro_name(denom)+"_MORE};"
        << "};\n";
}

template <typename _IntT>
void generate_ternary_primary(_IntT denom, const char *varName) {
    std::cout   << varName << "==" << denom << " ? "
                << struct_selector<_IntT>::get_name() << " { "
                << ".magic = " << const_macro_name(denom)+"_MAGIC, "
                << ".more = " <<  const_macro_name(denom)+"_MORE} :\n";
}


template <typename _IntT>
void generate_const_func() {
    const char param_name[] = "v";
    std::cout << "LIBDIVIDE_INLINE constexpr " << struct_selector<_IntT>::get_name()
                << " get_divider_" << type_tag<_IntT>::get_tag() 
                << "(" << type_name<_IntT>::get_name() << " " << param_name << ") {\n"
                << "\treturn\n";
    generator<_IntT>([param_name](_IntT denom) { 
            std::cout << "\t";
            generate_ternary_primary<_IntT>(denom, param_name); 
            });
    std:: cout  << "\t" << struct_selector<_IntT>::get_name() << " { 0, 0 }; // Unreachable\n"
                << "}";
}

enum style {
    macro,
    cpp_template,
    const_func,
};


template <typename _IntT>
void generate(style gen_style) {
    switch (gen_style)
    {
    case style::macro:
        generator<_IntT>(&generate_constant_macro<_IntT>);
        break;
    case style::cpp_template:
        generator<_IntT>(&generate_specialized_template<_IntT>);
        break;
    
    case style::const_func:
        generate_const_func<_IntT>();
        break;

    default:
        break;
    }
}


void print_disclaimer()
{
    std::cout   << "// This file is machine generated\n"
                << "// Do not make changes to it.\n"
                << "// See " << __FILE__ << "\n";
}

extern "C" int main(int argc, char *argv[]) {
    if (argc!=3) {
        std::cout
                << "Usage: fast_div_generator [DATATYPE] [STYLE]\n"
                   "\n"
                   "[DATATYPE] in [s16, u16]\n"
                   "[STYLE] in [MACRO, TEMPLATE, CONST_FUNC]"
                << std::endl;
            exit(1);        
    }

    print_disclaimer();

    std::string data_type(to_lower(argv[1]));
    std::string style_param(to_lower(argv[2]));
    style generate_template = "template"==style_param ? style::cpp_template
                            : "macro"==style_param ? style::macro 
                            : style::const_func;

    if (data_type == type_tag<int16_t>::get_tag()) {
        generate<int16_t>(generate_template);
    } else if (data_type == type_tag<uint16_t>::get_tag()) {
        generate<uint16_t>(generate_template);
    }
}