⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 import.pl

📁 这是整套横扫千军3D版游戏的源码
💻 PL
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/perl

# streflop: STandalone REproducible FLOating-Point

# This script imports the GNU libm files and convert them in such a way they can be compiled with streflop types.

# Code released according to the GNU Lesser General Public License
# Nicolas Brodu, 2006

# Please read the history and copyright information in the accompanying documentation

if ($#ARGV==-1) {
print <<ENDHELP;
Perl script to import the libm components from the GNU glibc inside this project.
Tested with glibc-2.4

Usage: ./import.pl glibc_src_dir
Where: glibc_src_dir is the source directory obtained by uncompressing the GNU libc archive.

This script copies the libm directories for generic IEEE754 math with float (32 bits), double (64 bits) and long double (80 bits). The files are then post-processed to remove all dependencies from glibc so they can be compiled standalone.
ENDHELP
exit 0;
}

$dir=$ARGV[0];
if ((! -d "$dir/sysdeps/ieee754/flt-32")
 || (! -d "$dir/sysdeps/ieee754/dbl-64")
 || (! -d "$dir/sysdeps/ieee754/ldbl-96")
 || (! -r "$dir/math/s_ldexpf.c")        # was put here for unknown reason, probably because not system-dependent
 || (! -r "$dir/math/s_ldexp.c")         # was put here for unknown reason, probably because not system-dependent
 || (! -r "$dir/math/s_nextafter.c")     # was put here for unknown reason, probably because not system-dependent
 || (! -r "$dir/math/math_private.h")
 || (! -r "$dir/sysdeps/ieee754/ieee754.h")
 || (! -r "$dir/include/features.h")
 || (! -r "$dir/string/endian.h")
 || (! -r "$dir/include/endian.h")
) {
print "Invalid glibc directory\n";
exit 1;
}

$importNotice="/* See the import.pl script for potential modifications */\n";

# Copy files
system("cp -r $dir/sysdeps/ieee754/flt-32 $dir/sysdeps/ieee754/dbl-64 $dir/sysdeps/ieee754/ldbl-96 .");
if (! -d "headers") {mkdir("headers");}
system("cp -r $dir/math/s_ldexpf.c flt-32");
system("cp -r $dir/math/s_ldexp.c $dir/math/s_nextafter.c dbl-64");
# this ldbl function is actually an alias to the dbl one in the libm. Make a hard copy here.
system("cp -r $dir/math/s_ldexp.c ldbl-96/s_ldexpl.c");
system("cp -r $dir/math/math_private.h $dir/sysdeps/ieee754/ieee754.h $dir/include/features.h $dir/string/endian.h $dir/bits/wchar.h headers/");
# Merge include files
system("cat $dir/include/endian.h >> headers/endian.h");

# remove some routines we don't need
if (-r "ldbl-96/printf_fphex.c") {unlink "ldbl-96/printf_fphex.c";}
if (-r "ldbl-96/strtold_l.c") {unlink "ldbl-96/strtold_l.c";}
if (-r "flt-32/mpn2flt.c") {unlink "flt-32/mpn2flt.c";}
if (-r "dbl-64/mpn2dbl.c") {unlink "dbl-64/mpn2dbl.c";}
if (-r "dbl-64/dbl2mpn.c") {unlink "dbl-64/dbl2mpn.c";}
if (-r "dbl-64/t_exp.c") {unlink "dbl-64/t_exp.c";}
if (-r "ldbl-96/mpn2ldbl.c") {unlink "ldbl-96/mpn2ldbl.c";}
if (-r "ldbl-96/ldbl2mpn.c") {unlink "ldbl-96/ldbl2mpn.c";}
if (-r "ldbl-96/s_nexttoward.c") {unlink "ldbl-96/s_nexttoward.c";}
if (-r "ldbl-96/s_nexttowardf.c") {unlink "ldbl-96/s_nexttowardf.c";}
if (-r "ldbl-96/math_ldbl.h") {unlink "ldbl-96/math_ldbl.h";}

# The float exp in e_expf.c uses doubles internally since revision 1.2 in the libm-ieee754 CVS attic!
# Roll back to the slower, but purely float version, and also overwrite the wrapper by a wraper to the float only version
system("cp -f w_expf.c e_expf.c flt-32");

# convert .c => .cpp for clarity
@filelist = glob("flt-32/*.c dbl-64/*.c ldbl-96/*.c");
foreach $f (@filelist) {
    $g = $f;
    $g =~ s/\.c$/.cpp/;
    rename $f, $g;
}

# create dummy math.h stub
open(FILE, ">headers/math.h");
print FILE <<MATH_H;
/* This file is stub automatically generated by import.pl */

// Include bridge, just in case math_private is not included
#include "../streflop_libm_bridge.h"

MATH_H
close FILE;

# These files in the flt directory use double but could use floats instead
foreach $f ("flt-32/s_cbrtf.cpp", "flt-32/s_llrintf.cpp", "flt-32/s_lrintf.cpp") {
    open(FILE,"<$f");
    $content = "";
    while(<FILE>) {
        # replace double by float, OK in these files
        s/double/float/g;
        $content.=$_;
    }
    close FILE;
    open(FILE,">$f");
    print FILE $content;
    close FILE;
}

# These files in the dbl directory wrongly use float, long double and float functions
foreach $f ("dbl-64/e_lgamma_r.cpp", "dbl-64/t_exp2.h", "dbl-64/s_llrint.cpp") {
    open(FILE,"<$f");
    $content = "";
    while(<FILE>) {
        s/fabsf/fabs/g;
        s/float/double/g;
        s/long double/double/g;
        $content.=$_;
    }
    close FILE;
    open(FILE,">$f");
    print FILE $content;
    close FILE;
}

# These files in the ldbl directory wrongly use lower precision types
foreach $f ("ldbl-96/e_lgammal_r.cpp", "ldbl-96/s_cbrtl.cpp", "ldbl-96/s_logbl.cpp", "ldbl-96/s_ldexpl.cpp") {
    open(FILE,"<$f");
    $content = "";
    while(<FILE>) {
        # do the substitution now
        s/long double/Extended/g;
        s/\(double\) i;/(long double) i;/g;
        s/double (_|v)/long double $1/g;
        s/const double factor/const long double factor/g;
        s/fabs(?!l)/fabsl$1/g;
        s/ldexp(?!l)/ldexpl$1/g;
        s/__finite\(/__finitel\(/g;
        s/__scalbn\(/__scalbnl\(/g;
        $content.=$_;
    }
    close FILE;
    open(FILE,">$f");
    print FILE $content;
    close FILE;
}


# DOUBLE_FROM_INT_PTR(x) could simply be *reinterpret_cast<double*>(x)
# for basic types, but may use a dedicated factory for Object wrappers
# BaseType is either the same as FloatType for plain old float/double, or it is
# the float/double type when FloatType is an Object wrapper
$xdaccessor=
 "inline Double& d() {return DOUBLE_FROM_INT_PTR(&i[0]);}\n"
."inline Double& x() {return DOUBLE_FROM_INT_PTR(&i[0]);}\n"
."inline Double& d(int idx) {return DOUBLE_FROM_INT_PTR(&i[idx*(sizeof(double)/sizeof(i))]);}\n"
."inline Double& x(int idx) {return DOUBLE_FROM_INT_PTR(&i[idx*(sizeof(double)/sizeof(i))]);}\n"
."inline const Double& d() const {return CONST_DOUBLE_FROM_INT_PTR(&i[0]);}\n"
."inline const Double& x() const {return CONST_DOUBLE_FROM_INT_PTR(&i[0]);}\n"
."inline const Double& d(int idx) const {return CONST_DOUBLE_FROM_INT_PTR(&i[idx*(sizeof(double)/sizeof(i))]);}\n"
."inline const Double& x(int idx) const {return CONST_DOUBLE_FROM_INT_PTR(&i[idx*(sizeof(double)/sizeof(i))]);}\n"
;

@filelist = glob("flt-32/* dbl-64/* ldbl-96/*");

foreach $f (@filelist) {

    open(FILE,"<$f");
    $content = "";
    $opened_namespace = 0;
    while(<FILE>) {
        # remove all system-wide includes
        if (/.*?#include(.*?)<sys\/(.*)/) {$_="//".$_;}
        # Convert all includes to local files
        s/^([ \t]*)#include([ \t]*)<(.*)>(.*)/#$1include$2\"$3\"$4/g;
        # fp environment is now handled directly by streflop (and FPUSettings.h)
        s/fenv\.h/..\/streflop_libm_bridge.h/g;
        # integer types handled by the bridge
        s/inttypes\.h/..\/streflop_libm_bridge.h/g;
        # get rid of stdlib too
        s/stdlib\.h/..\/streflop_libm_bridge.h/g;
        # and limits.h
        s/limits\.h/..\/streflop_libm_bridge.h/g;
        # float.h too
        s/float\.h/..\/streflop_libm_bridge.h/g;
        # errno.h falls back to system, remove it
        s/errno\.h/..\/streflop_libm_bridge.h/g;
        # replace all occurences of problematic union fields with objects
        # by proper C++ accessors
        s/\b(\.d(?!\[)\b|\.d\[(.*?)\])/.d($2)/g;
        s/\b(\.x(?!\[)\b|\.x\[(.*?)\])/.x($2)/g;
        s/\b(\.f(?!\[)\b|\.f\[(.*?)\])/.f($2)/g; # ieee754.h
        s/\b(->d(?!\[)\b|->d\[(.*?)\])/->d($2)/g;
        s/\b(->x(?!\[)\b|->x\[(.*?)\])/->x($2)/g;
        s/\b(->f(?!\[)\b|->f\[(.*?)\])/->f($2)/g; # ieee754.h
        # Some more occurences from arrays of such unions/structs
        s/\]\.d\b/].d()/g;
        # named field C construct is invalid (C++ would initialize first union member)
        # and we replace unions by struct + accessor anyway
        s/{ *?.i *?= *?{/{{/g;
        # volatile declaration of static const global variables poses problem with the wrapper types
        s/volatile //g;
        # Now substitute the base types by their Simple/Double/Extended aliases or wrapper
        s/long double/Extended/g; # before double
        s/\bdouble\b/Double/g;
        s/\bfloat\b/Simple/g;
        # Replace problematic int += double
        s/E(X|Y|Z)(.*?=.*?)ONE/E${1}${2}1/g;
        # problematic ?: operator with different types. This simple check catches all problematic occurences
#        if (/\?(.*?(\b0\.|\.0).*?:.*?|.*?:.*?(\b0\.|\.0).*?);/) {
#            print "$f => ?$1;\n";
#        }
        if (/\?(.*?):(.*?)\b(0\.0|10\.0|1\.0)\b/) {
            my $type = "";
            if ($f =~ /flt-32/) {$type = "Simple";}
            if ($f =~ /dbl-64/) {$type = "Double";}
            if ($f =~ /ldbl-96/) {$type = "Extended";}
            s/\?(.*?):(.*?)\b(0\.0|10\.0|1\.0)\b/?$1:$2$type($3)/g;
        }
        my $type = "";
        my $flit = "";
        if ($f =~ /flt-32/) {$type = "Simple"; $flit = "f";}
        if ($f =~ /dbl-64/) {$type = "Double"; $flit = "";}
        if ($f =~ /ldbl-96/) {$type = "Extended"; $flit = "l";}
        # replace problematic mixed-type ?: operators where an int and a float are used as arguments, incompatible with wrappers
        if (/\?(.*?)\b(0\.0|1\.0)\b(.*?):(.*?)/) {
            s/\?(.*?)\b(0\.0|1\.0)\b(.*?):(.*?)/?$1$type($2)$3:$4/g;
        }
        # These special cases are OK because no other ?: pattern use them in the 3 subdirs
        s/\?0:/?Double(0.0):/;
        s/:0;/:Double(0.0);/;
        # protect the new symbol names by namespace to avoid any conflict with system libm
        if (((/#ifdef __STDC__/) || (/.*? (__|mcr|ss32)[a-z,A-Z,_,0-9]*?\(.*?{$/) || (/Double (atan2Mp|atanMp|slow|tanMp|__exp1|__ieee754_remainder|__ieee754_sqrt)/) || (/^(Simple|Double|Extended|void|int)$/) || ((/^#ifdef BIG_ENDI$/) && ($f =~ /(uatan|mpa2|mpexp|atnat|sincos32)/)) || (/^#define MM 5$/) || (/^void __mp(log|sqrt|exp|atan)\(/) || (/^int __(b|mp)ranred\(/)) && ($opened_namespace == 0)) {
            $_ = "namespace streflop_libm {\n".$_;
            $opened_namespace = 1;
        }

        # Prevent type promotion for native aliases
        # Ex: promotion would cause computations like y = 0.5 * x to be done in double for x,y float, then cast back to float
        # We want the whole computation done in float, not temporary switching to double.
        # In some case the FPU internal precision is enough to mask the problem, but for SSE for example, the float/double
        # are using different instructions and the problem cannot be ignored (in these cases, there are differences from
        # soft float implementation)
        # Cannot replace by s/blah/$type($1)/g; because of static order initialization fiasco with wrapper types
        # So use next best option to use litteral type specification
        # => this solves the problem for native types
        # => wrappers are OK thanks to the redefinitions of the operators
        s/\b((\d+\.\d*|\d*\.\d+)((e|E)[-+]?\d+)?)(f|F|l|L)?\b/$1$flit/g;
        $content.=$_;
    }
    close FILE;
    # multi-line spanning regexp
    $content =~ s/union ?{(.*?);.*?Double.*?}/struct {\n$xdaccessor$1;}/sg;
    # close opened namespace
    if ($opened_namespace==1) {
        $content .= "}\n";
    }
    open(FILE,">$f");
    print FILE $importNotice;
    print FILE $content;
    close FILE;
}


# ieee754.h union+accessor
open(FILE,"<headers/ieee754.h");
$content = "";
while(<FILE>) {
    # Comment out decls sections
    if (/.*?__.*?_DECLS.*/) {$_="//".$_;}
    # Convert all includes to local files
    s/^([ \t]*)#include([ \t]*)<(.*)>(.*)/#$1include$2\"$3\"$4/g;
    # insert our own bridge

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -