/
format.pm
229 lines (199 loc) · 6.19 KB
/
format.pm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright (C) 2001-2012, Parrot Foundation.
=head1 NAME
config/auto/format.pm - Sprintf Formats
=head1 DESCRIPTION
Figures out what integer and number formats should be used for C<sprintf()>.
=cut
package auto::format;
use strict;
use warnings;
use base qw(Parrot::Configure::Step);
use Parrot::Configure::Step;
sub _init {
my $self = shift;
my %data;
$data{description} = q{What formats should be used for sprintf};
$data{result} = q{};
return \%data;
}
sub runstep {
my ( $self, $conf ) = @_;
_set_intvalfmt($conf);
_set_floatvalfmt_nvsize($conf);
$conf->cc_clean();
return 1;
}
sub _set_intvalfmt {
my $conf = shift;
my $ivformat;
my $iv = $conf->data->get(qw(iv));
if ( $iv eq "int" ) {
$ivformat = "%d";
}
elsif ( ( $iv eq "long" ) || ( $iv eq "long int" ) ) {
$ivformat = "%ld";
}
elsif ( ( $iv eq "long long" ) || ( $iv eq "long long int" ) ) {
$ivformat = "%lld";
}
else {
die qq{Configure.pl: Can't find a printf-style format specifier for type '$iv'\n};
}
$conf->data->set( intvalfmt => $ivformat );
}
sub _set_floatvalfmt_nvsize {
my $conf = shift;
my ( $nv, $numvalsize, $cpuarch, $floatvaldig ) =
$conf->data->get(qw(nv numvalsize cpuarch floatvaldig));
my ( $nvformat, $nvsize, $floattype );
$nvsize = $numvalsize;
my $fltdig = _get_floatvaldigits($conf, $floatvaldig)
if $floatvaldig;
if ( $nv eq "double" ) {
$nvformat = sprintf("%%.%dg", $fltdig ? $fltdig : 15);
$floattype = 'FLOATTYPE_8';
}
elsif ( $nv eq "long double" ) {
# 64 or 80 bits, 12 or 16 bytes
my $spri = $conf->data->get('sPRIgldbl_provisional');
if ( defined $spri ) {
# TT #308 same values as in imcc
if ($nvsize == 8) {
$floattype = 'FLOATTYPE_8';
$nvformat = sprintf("%%.%dg", $fltdig ? $fltdig : 16);
}
elsif ($nvsize == 12) {
$floattype = 'FLOATTYPE_10'; # i386 only
$nvformat = sprintf("%%.%dLg", $fltdig ? $fltdig : 18);
}
elsif ($nvsize == 16) {
$nvformat = sprintf("%%.%dLg", $fltdig ? $fltdig : 41);
if ($cpuarch =~ /^i386|amd64|ia64$/) {
$floattype = 'FLOATTYPE_10';
$nvformat = "%.18Lg" unless $fltdig;
}
elsif ($cpuarch eq 'mips') {
# quadmath with special NaN
$floattype = 'FLOATTYPE_16MIPS';
}
elsif ($cpuarch eq 'ppc') {
# double-double https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/float.3.html
$nvformat = "%.31Lg" unless $fltdig;
$floattype = 'FLOATTYPE_16PPC';
}
elsif ($cpuarch =~ /^s390|sparc/) {
# native IEEE-754 quadmath
$floattype = 'FLOATTYPE_16';
}
else {
die qq{Configure.pl: Can't find the binary representation for '$nv'\n};
}
}
$nvformat =~ s/"//g; # Perl 5's Config value has embedded double quotes
}
else {
die qq{Configure.pl: Can't find a printf-style format specifier for type '$nv'\n};
}
}
elsif ( $nv eq '__float128' ) {
# Probe for "%Q" libquadmath printf hook support (linux only)
if( _test_format($conf, "%Qg", 1.0)) {
$nvformat = "%Qg";
}
else {
$nvformat = sprintf("%%.%dLg", $fltdig ? $fltdig : 41);
}
$floattype = 'FLOATTYPE_16';
}
elsif ( $nv eq "float" ) {
$nvsize = 4;
# http://www.keil.com/support/docs/2191.htm
$nvformat = sprintf("%%.%dg", $fltdig ? $fltdig : 7);
$floattype = 'FLOATTYPE_4';
}
else {
die qq{Configure.pl: Can't find a printf-style format specifier for type '$nv'\n};
}
# For a series of random numbers test the nvformat precision, and decrease it.
# 4: 7 digits, 8: 15, 10: 18, 16ppc: 31, 16: 41
my @TEST = (-2.5, -4.003052, -10.48576, 1.0/3.0 );
push @TEST, (-4.0030526, 4.398046511104) if $nvsize > 4;
push @TEST, (-104.398046517704) if $nvsize >= 16;
for my $num (@TEST) {
if (!_test_format($conf, $nvformat, $num)) {
$nvformat = _decrease_nvformat_precision($conf, $nvformat);
$conf->debug("nvformat reduced: $nvformat\n");
redo;
}
}
$conf->data->set(
floatvalfmt => $nvformat,
nvsize => $nvsize,
floattype => $floattype
);
}
sub _decrease_nvformat_precision {
my ($conf, $nvformat) = @_;
my ($prefix, $num, $suff) = $nvformat =~ m/^(%\.?)([Q\d]+)(.+)$/;
$num = 41 if $num eq 'Q';
# require at least some sort of precision
if ($num < 6) {
my ( $nv, $numvalsize, $cpuarch ) =
$conf->data->get(qw(nv numvalsize cpuarch));
die "\nConfigure.pl: Unable to find stable round-trip numeric precision\n"
. "for $nv, size $numvalsize on $cpuarch. Please choose another --floatval\n";
}
return sprintf("%s%d%s", $prefix, $num-1, $suff);
}
sub _rounded_numeq {
my ($conf, $n1, $n2, $prec) = @_;
return 1 if $n1 == $n2;
if (length("$n1") > 3) {
if (length("$n1") >= 7) {
$n1 = sprintf("%.$prec"."g", $n1);
}
if (length("$n2") >= 7) {
$n2 = sprintf("%.$prec"."g", $n2);
}
$n1 = substr("$n1", 0, $prec-1);
$n2 = substr("$n2", 0, $prec-1);
$conf->debug(" nvformat:\n \"$n1\" == \n \"$n2\" ($prec digits)\n");
return $n1 == $n2;
}
else {
$conf->debug(" nvformat: \n $n1 == \n $n2 ($prec digits)\n");
return $n1 == $n2;
}
}
sub _test_format {
my ($conf, $nvformat, $number) = @_;
my $num = $number;
$num .= "L" if $nvformat =~ /L/;
$num .= "Q" if $nvformat =~ /Q/;
my ($prefix, $prec, $suff) = $nvformat =~ m/^(%\.?)([Q\d]+)(.+)$/;
$prec = 41 if $prec eq 'Q';
$conf->data->set( TEMP_nvformat => $nvformat,
TEMP_number => $num);
$conf->cc_gen('config/auto/format/test_c.in');
eval { $conf->cc_build() };
my $ret = $@ ? 0 : eval $conf->cc_run();
$conf->cc_clean();
return _rounded_numeq($conf, $ret, $number, $prec);
}
sub _get_floatvaldigits {
my ($conf, $floatvaldig) = @_;
$conf->data->set( TEMP_nvformat => "%u",
TEMP_number => $floatvaldig);
$conf->cc_gen('config/auto/format/test_c.in');
eval { $conf->cc_build() };
my $ret = $@ ? 0 : eval $conf->cc_run();
$conf->cc_clean();
return $ret;
}
1;
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: