blib/lib/Math/FakeFloat16.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary FakeFloat16.xs »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Math::FakeFloat16;
4 0 ▶use Math::MPFR qw(:mpfr);
Subroutine BEGIN: called CC=1 SCAR=2.6
5  
6 use constant f16_EMIN => -23;
7 use constant f16_EMAX => 16;
8 use constant f16_MANTBITS => 11;
9 use constant MPFR_PREC_MIN => Math::MPFR::MPFR_PREC_MIN;
10  
11  
12 use overload
13 '+' => \&oload_add,
14 '-' => \&oload_sub,
15 '*' => \&oload_mul,
16 '/' => \&oload_div,
17 '%' => \&oload_fmod,
18 '**' => \&oload_pow,
19  
20 '==' => \&oload_equiv,
21 '!=' => \&oload_not_equiv,
22 '>' => \&oload_gt,
23 '>=' => \&oload_gte,
24 '<' => \&oload_lt,
25 '<=' => \&oload_lte,
26 '<=>' => \&oload_spaceship,
27 'abs' => \&oload_abs,
28 '""' => \&oload_interp,
29  
30 'sqrt' => \&oload_sqrt,
31 'exp' => \&oload_exp,
32 'log' => \&oload_log,
33 'int' => \&oload_int,
34 '!' => \&oload_not,
35 'bool' => \&oload_bool,
36 ;
37  
38 require Exporter;
39 *import = \&Exporter::import;
40 require DynaLoader;
41  
42 $Math::FakeFloat16::VERSION = '0.01';
43 Math::FakeFloat16->DynaLoader::bootstrap($Math::FakeFloat16::VERSION);
44  
45 sub dl_load_flags {0} # Prevent DynaLoader from complaining and croaking
46  
47  
48 my @tagged = qw( is_f16_nan is_f16_inf is_f16_zero f16_set_nan f16_set_inf f16_set_zero
49 f16_set
50 f16_nextabove f16_nextbelow
51 unpack_f16_hex
52 f16_EMIN f16_EMAX f16_MANTBITS
53 f16_to_NV f16_to_MPFR f16_to_PV
54 );
55  
56 @Math::FakeFloat16::EXPORT = ();
57 @Math::FakeFloat16::EXPORT_OK = @tagged;
58 %Math::FakeFloat16::EXPORT_TAGS = (all => \@tagged);
59  
60  
61 %Math::FakeFloat16::handler = (1 => sub {print "OK: 1\n"},
62 2 => sub {return _fromIV(shift)},
63 4 => sub {return _fromPV(shift)},
64 3 => sub {return _fromNV(shift)},
65 5 => sub {return _fromMPFR(shift)},
66 6 => sub {return _fromGMPf(shift)},
67 7 => sub {return _fromGMPq(shift)},
68  
69 31 => sub {return _fromFloat16(shift)},
70 );
71  
72 $Math::FakeFloat16::f16_DENORM_MIN = Math::FakeFloat16->new(2) ** (f16_EMIN - 1); # 5.9605e-8
73 $Math::FakeFloat16::f16_DENORM_MAX = Math::FakeFloat16->new(_get_denorm_max()); # 6.0976e-5
74 $Math::FakeFloat16::f16_NORM_MIN = Math::FakeFloat16->new(2) ** (f16_EMIN + (f16_MANTBITS - 2)); # 6.1035e-5
75 $Math::FakeFloat16::f16_NORM_MAX = Math::FakeFloat16->new(_get_norm_max()); # 6.5504e4
76  
77 sub new {
78 shift if (@_ > 0 && !ref($_[0]) && _itsa($_[0]) == 4 && $_[0] eq "Math::FakeFloat16");
79 if(!@_) {
80 my $ret = Rmpfr_init2(f16_MANTBITS);
81 return bless(\$ret);
82 }
83 die "Too many args given to new()" if @_ > 1;
84 my $itsa = _itsa($_[0]);
85 if($itsa) {
86 return _fromIV($_[0]) if($itsa == 2);
87 return _fromFloat16($_[0]) if ($itsa == 31);
88 my $ret = subnormalize_generic($_[0], f16_EMIN, f16_EMAX, f16_MANTBITS);
89 return bless(\$ret);
90 # my $coderef = $Math::FakeFloat16::handler{$itsa};
91 # return $coderef->($_[0]);
92 }
93 die "Unrecognized 1st argument passed to new() function";
94 }
95  
96 sub _fromPV {
97 my $pv = shift;
98 my $ret = subnormalize_generic($pv, f16_EMIN, f16_EMAX, f16_MANTBITS);
99 return bless(\$ret);
100 }
101  
102 sub _fromIV {
103 my $iv = shift;
104 my $ret = Rmpfr_init2(f16_MANTBITS);
105 Rmpfr_set_IV($ret, $iv, MPFR_RNDN);
106 # Make appropriate correction if abs($ret) > 65504
107 if ($ret > 65504) { Rmpfr_set_inf($ret, 1) }
108 elsif($ret < -65504) { Rmpfr_set_inf($ret, -1) }
109 return bless(\$ret);
110 }
111  
112 sub _fromNV {
113 my $nv = shift;
114 my $ret = subnormalize_generic($nv, f16_EMIN, f16_EMAX, f16_MANTBITS);
115 return bless(\$ret);
116 }
117  
118 sub _fromMPFR {
119 my $mpfr = shift;
120 my $ret = subnormalize_generic($mpfr, f16_EMIN, f16_EMAX, f16_MANTBITS);
121 return bless(\$ret);
122 }
123  
124 sub _fromGMPf {
125 my $mpf = shift;
126 my $ret = subnormalize_generic($mpf, f16_EMIN, f16_EMAX, f16_MANTBITS);
127 return bless(\$ret);
128 }
129  
130 sub _fromGMPq {
131 my $mpq = shift;
132 my $ret = subnormalize_generic($mpq, f16_EMIN, f16_EMAX, f16_MANTBITS);
133 return bless(\$ret);
134 }
135  
136 sub _fromFloat16 { # ie from Math::FakeFloat16 object
137 my $f16 = shift;
138 my $ret = Rmpfr_init2(f16_MANTBITS);
139 my $inex = Rmpfr_set($ret, $$f16, MPFR_RNDN);
140 die "Error in assignment" if $inex;
141 return bless(\$ret);
142 }
143  
144 sub f16_to_NV {
145 # For compatibility with Math::Float16
146 return Rmpfr_get_NV(${$_[0]}, MPFR_RNDN);
147 }
148  
149 sub f16_to_MPFR {
150 # For compatibility with Math::Float16
151 my $ret = Rmpfr_init2(11);
152 Rmpfr_set($ret, ${$_[0]}, MPFR_RNDN);
153 return $ret;
154 }
155  
156 sub is_f16_nan {
157 return 1 if Rmpfr_nan_p(${$_[0]});
158 return 0;
159 }
160  
161 sub is_f16_inf {
162 if( Rmpfr_inf_p(${$_[0]}) ) {
163 return 1 if ${$_[0]} > 0;
164 return -1;
165 }
166 return 0;
167 }
168  
169 sub is_f16_zero {
170 if ( Rmpfr_zero_p(${$_[0]}) ) {
171 return -1 if Rmpfr_signbit( ${$_[0]} );
172 return 1;
173 }
174 return 0;
175 }
176  
177 sub f16_set_nan {
178 Rmpfr_set_nan(${$_[0]});
179 }
180  
181 sub f16_set_inf {
182 Rmpfr_set_inf(${$_[0]}, $_[1]);
183 }
184  
185 sub f16_set_zero {
186 Rmpfr_set_zero(${$_[0]}, $_[1]);
187 }
188  
189 sub f16_set {
190 die "f16_set expects to receive precisely 2 arguments" if @_ != 2;
191 my $coderef = $Math::FakeFloat16::handler{_itsa($_[1])};
192 my $arg = $coderef->($_[1]);
193 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
194 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
195 my $inex = Rmpfr_set(${$_[0]}, $$arg, MPFR_RNDN);
196 Rmpfr_subnormalize(${$_[0]}, $inex, MPFR_RNDN);
197 RESET_EMIN_EMAX($emin_orig, $emax_orig);
198 }
199  
200 sub oload_add {
201 my $ret = Rmpfr_init2(f16_MANTBITS);
202 my $itsa = _itsa($_[1]);
203 my $coderef = $Math::FakeFloat16::handler{$itsa};
204 die "Unrecognized 2nd argument passed to oload_add() function"
205 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
206 my $arg = $coderef->($_[1]);
207 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
208 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
209 my $inex = Rmpfr_add($ret, ${$_[0]}, $$arg, MPFR_RNDN);
210 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
211 RESET_EMIN_EMAX($emin_orig, $emax_orig);
212 return bless(\$ret);
213 }
214  
215 sub oload_mul {
216 my $ret = Rmpfr_init2(f16_MANTBITS);
217 my $itsa = _itsa($_[1]);
218 my $coderef = $Math::FakeFloat16::handler{$itsa};
219 die "Unrecognized 2nd argument passed to oload_mul() function"
220 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
221 my $arg = $coderef->($_[1]);
222 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
223 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
224 my $inex = Rmpfr_mul($ret, ${$_[0]}, $$arg, MPFR_RNDN);
225 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
226 RESET_EMIN_EMAX($emin_orig, $emax_orig);
227 return bless(\$ret);
228 }
229  
230 sub oload_sub {
231 my $ret = Rmpfr_init2(f16_MANTBITS);
232 my $itsa = _itsa($_[1]);
233 my $coderef = $Math::FakeFloat16::handler{$itsa};
234 die "Unrecognized 2nd argument passed to oload_sub() function"
235 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
236 my $arg = $coderef->($_[1]);
237 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
238 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
239 my $inex;
240 if($_[2]) { $inex = Rmpfr_sub($ret, $$arg, ${$_[0]}, MPFR_RNDN) }
241 else { $inex = Rmpfr_sub($ret, ${$_[0]}, $$arg, MPFR_RNDN) }
242 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
243 RESET_EMIN_EMAX($emin_orig, $emax_orig);
244 return bless(\$ret);
245 }
246  
247 sub oload_div {
248 my $ret = Rmpfr_init2(f16_MANTBITS);
249 my $itsa = _itsa($_[1]);
250 my $coderef = $Math::FakeFloat16::handler{$itsa};
251 die "Unrecognized 2nd argument passed to oload_div() function"
252 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
253 my $arg = $coderef->($_[1]);
254 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
255 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
256 my $inex;
257 if($_[2]) { $inex = Rmpfr_div($ret, $$arg, ${$_[0]}, MPFR_RNDN) }
258 else { $inex = Rmpfr_div($ret, ${$_[0]}, $$arg, MPFR_RNDN) }
259 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
260 RESET_EMIN_EMAX($emin_orig, $emax_orig);
261 return bless(\$ret);
262 }
263  
264 sub oload_pow {
265 my $ret = Rmpfr_init2(f16_MANTBITS);
266 my $itsa = _itsa($_[1]);
267 my $coderef = $Math::FakeFloat16::handler{$itsa};
268 die "Unrecognized 2nd argument passed to oload_pow() function"
269 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
270 my $arg = $coderef->($_[1]);
271 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
272 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
273 my $inex;
274 if($_[2]) { $inex = Rmpfr_pow($ret, $$arg, ${$_[0]}, MPFR_RNDN) }
275 else { $inex = Rmpfr_pow($ret, ${$_[0]}, $$arg, MPFR_RNDN) }
276 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
277 RESET_EMIN_EMAX($emin_orig, $emax_orig);
278 return bless(\$ret);
279 }
280  
281 sub oload_fmod {
282 my $ret = Rmpfr_init2(f16_MANTBITS);
283 my $itsa = _itsa($_[1]);
284 my $coderef = $Math::FakeFloat16::handler{$itsa};
285 die "Unrecognized 2nd argument passed to oload_fmod() function"
286 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
287 my $arg = $coderef->($_[1]);
288 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
289 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
290 my $inex;
291 if($_[2]) { $inex = Rmpfr_fmod($ret, $$arg, ${$_[0]}, MPFR_RNDN) }
292 else { $inex = Rmpfr_fmod($ret, ${$_[0]}, $$arg, MPFR_RNDN) }
293 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
294 RESET_EMIN_EMAX($emin_orig, $emax_orig);
295 return bless(\$ret);
296 }
297  
298 sub oload_sqrt {
299 my $ret = Rmpfr_init2(f16_MANTBITS);
300 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
301 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
302 my $inex = Rmpfr_sqrt($ret, ${$_[0]}, MPFR_RNDN);
303 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
304 RESET_EMIN_EMAX($emin_orig, $emax_orig);
305 return bless(\$ret);
306 }
307  
308 sub oload_log {
309 my $ret = Rmpfr_init2(f16_MANTBITS);
310 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
311 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
312 my $inex = Rmpfr_log($ret, ${$_[0]}, MPFR_RNDN);
313 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
314 RESET_EMIN_EMAX($emin_orig, $emax_orig);
315 return bless(\$ret);
316 }
317  
318 sub oload_exp {
319 my $ret = Rmpfr_init2(f16_MANTBITS);
320 my ($emin_orig, $emax_orig) = (Rmpfr_get_emin(), Rmpfr_get_emax());
321 SET_EMIN_EMAX(f16_EMIN, f16_EMAX);
322 my $inex = Rmpfr_exp($ret, ${$_[0]}, MPFR_RNDN);
323 Rmpfr_subnormalize($ret, $inex, MPFR_RNDN);
324 RESET_EMIN_EMAX($emin_orig, $emax_orig);
325 return bless(\$ret);
326 }
327  
328 sub oload_int {
329 my $ret = Rmpfr_init2(f16_MANTBITS);
330 Rmpfr_trunc($ret, ${$_[0]});
331 return bless(\$ret);
332 }
333  
334 sub oload_abs {
335 return $_[0] * -1 if $_[0] < 0;
336 return $_[0];
337 }
338  
339 sub oload_not {
340 return 1 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_zero_p(${$_[0]}) );
341 return 0;
342 }
343  
344 sub oload_bool {
345 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_zero_p(${$_[0]}) );
346 return 1;
347 }
348  
349 sub oload_equiv {
350 my $itsa = _itsa($_[1]);
351 my $coderef = $Math::FakeFloat16::handler{$itsa};
352 die "Unrecognized 2nd argument passed to oload_equiv() function"
353 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
354 my $arg = $coderef->($_[1]);
355 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
356 return 0 if Rmpfr_cmp(${$_[0]}, $$arg);
357 return 1;
358 }
359  
360 sub oload_not_equiv {
361 my $itsa = _itsa($_[1]);
362 my $coderef = $Math::FakeFloat16::handler{$itsa};
363 die "Unrecognized 2nd argument passed to oload_not_equiv() function"
364 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
365 my $arg = $coderef->($_[1]);
366 return 1 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
367 return 1 if Rmpfr_cmp(${$_[0]}, $$arg);
368 return 0;
369 }
370  
371 sub oload_gt {
372 my $itsa = _itsa($_[1]);
373 my $coderef = $Math::FakeFloat16::handler{$itsa};
374 die "Unrecognized 2nd argument passed to oload_gt() function"
375 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
376 my $arg = $coderef->($_[1]);
377 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
378 if($_[2]) {
379 return 1 if Rmpfr_cmp($$arg, ${$_[0]}) > 0;
380 return 0;
381 }
382 return 1 if Rmpfr_cmp(${$_[0]}, $$arg) > 0;
383 return 0;
384 }
385  
386 sub oload_gte {
387 my $itsa = _itsa($_[1]);
388 my $coderef = $Math::FakeFloat16::handler{$itsa};
389 die "Unrecognized 2nd argument passed to oload_gte() function"
390 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
391 my $arg = $coderef->($_[1]);
392 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
393 if($_[2]) {
394 return 1 if Rmpfr_cmp($$arg, ${$_[0]}) >= 0;
395 return 0;
396 }
397 return 1 if Rmpfr_cmp(${$_[0]}, $$arg) >= 0;
398 return 0;
399 }
400  
401 sub oload_lt {
402 my $itsa = _itsa($_[1]);
403 my $coderef = $Math::FakeFloat16::handler{$itsa};
404 die "Unrecognized 2nd argument passed to oload_lt() function"
405 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
406 my $arg = $coderef->($_[1]);
407 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
408 if($_[2]) {
409 return 1 if Rmpfr_cmp($$arg, ${$_[0]}) < 0;
410 return 0;
411 }
412 return 1 if Rmpfr_cmp(${$_[0]}, $$arg) < 0;
413 return 0;
414 }
415  
416 sub oload_lte {
417 my $itsa = _itsa($_[1]);
418 my $coderef = $Math::FakeFloat16::handler{$itsa};
419 die "Unrecognized 2nd argument passed to oload_lte() function"
420 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
421 my $arg = $coderef->($_[1]);
422 return 0 if( Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg) );
423 if($_[2]) {
424 return 1 if Rmpfr_cmp($$arg, ${$_[0]}) <= 0;
425 return 0;
426 }
427 return 1 if Rmpfr_cmp(${$_[0]}, $$arg) <= 0;
428 return 0;
429 }
430  
431  
432 sub oload_spaceship {
433 my $itsa = _itsa($_[1]);
434 my $coderef = $Math::FakeFloat16::handler{$itsa};
435 die "Unrecognized 2nd argument passed to oload_spaceship() function"
436 if ($itsa != 31 && ($itsa < 2 || $itsa > 4));
437 my $arg = $coderef->($_[1]);
438 return undef if(Rmpfr_nan_p(${$_[0]}) || Rmpfr_nan_p($$arg));
439 my $ret = Rmpfr_cmp(${$_[0]}, $$arg);
440 $ret = 1 if $ret > 0;
441 $ret = -1 if $ret < 0;
442 $ret *= -1 if $_[2];
443 return $ret;
444 }
445  
446 sub oload_interp {
447 my $ret = Math::MPFR::Rmpfr_get_str(${$_[0]}, 10, 0, MPFR_RNDN);
448 $ret =~ s/\@//g;
449 return $ret;
450 }
451  
452 sub f16_nextabove {
453 #if(Rmpfr_zero_p(${$_[0]})) {
454 # f16_set($_[0], $Math::FakeFloat16::f16_DENORM_MIN);
455 #}
456 if($_[0] == $Math::FakeFloat16::f16_NORM_MAX) {
457 Rmpfr_set_inf(${$_[0]}, 1);
458 }
459 elsif(is_f16_inf($_[0]) == -1) {
460 f16_set($_[0], -$Math::FakeFloat16::f16_NORM_MAX);
461 }
462 elsif($_[0] < $Math::FakeFloat16::f16_NORM_MIN && $_[0] >= -$Math::FakeFloat16::f16_NORM_MIN ) {
463 $_[0] += $Math::FakeFloat16::f16_DENORM_MIN;
464 f16_set_zero($_[0], -1) if is_f16_zero($_[0]);
465 }
466 else {
467 Rmpfr_nextabove(${$_[0]});
468 }
469 }
470  
471 sub f16_nextbelow {
472 #if(Rmpfr_zero_p(${$_[0]})) {
473 # f16_set($_[0], -$Math::FakeFloat16::f16_DENORM_MIN);
474 #}
475 if($_[0] == -$Math::FakeFloat16::f16_NORM_MAX) {
476 Rmpfr_set_inf(${$_[0]}, -1);
477 }
478 elsif(is_f16_inf( $_[0] ) == 1) {
479 f16_set($_[0], $Math::FakeFloat16::f16_NORM_MAX);
480 }
481 elsif($_[0] <= $Math::FakeFloat16::f16_NORM_MIN && $_[0] > -$Math::FakeFloat16::f16_NORM_MIN ) {
482 $_[0] -= $Math::FakeFloat16::f16_DENORM_MIN;
483 }
484 else {
485 Rmpfr_nextbelow( ${$_[0]} );
486 }
487 }
488  
489 sub unpack_f16_hex {
490 if(MPFR_PREC_MIN > 1) {
491 warn " The unpack_f16_hex() function is disabled because the mpfr library\n",
492 " against which Math::MPFR was built does not support a precision of 1.\n";
493  
494 die " Please rebuild Math::MPFR against a modern mpfr library (v4.2.0 or later)\n";
495 }
496  
497 die "Math::FakeFloat16::unpack_f16_hex() accepts only a Math::FakeFloat16 object as its argument"
498 unless ref($_[0]) eq "Math::FakeFloat16";
499 return _unpack_irregular($_[0]) unless Rmpfr_regular_p(${$_[0]});
500  
501 # This sub will have already returned
502 # if $_[0] is Inf, NaN or Zero.
503  
504 if(Rmpfr_get_exp(${$_[0]}) < -12) { # ${$_[0]} < 1.2207e-4
505 my $prefix = '0';
506 my $count = int( (23 + Rmpfr_get_exp(${$_[0]})) / 4 ) + 1;
507 my @res = Rmpfr_deref2(${$_[0]}, 16, $count, MPFR_RNDN);
508 $prefix ='8' if $res[0] =~ s/\-//;
509 $res[0] = '0' . $res[0] while length($res[0]) < 3;
510 return $prefix . uc($res[0]);
511 }
512  
513 my $signbit = '0';
514 my @res = Rmpfr_deref2(${$_[0]}, 2, 11, MPFR_RNDD);
515 $signbit ='1' if $res[0] =~ s/\-//;
516 my $exp = $res[1] + 14; # 14 == 15 - 1;
517 my $expstr = sprintf "%b", $exp;
518 $expstr = '0' . $expstr while length($expstr) < 5;
519 my $manstr = substr($res[0], -10, 10);
520 my $to_pack = $signbit . $expstr . $manstr;
521 return uc(unpack "H4", pack("B16", $to_pack));
522 }
523  
524 sub _unpack_irregular {
525 my $ret;
526 my $is_type = is_f16_nan($_[0]);
527 return 'FE00' if $is_type;
528  
529 $is_type = is_f16_inf($_[0]);
530 if($is_type) {
531 $ret = '7C00';
532 $ret = 'FC00' if $is_type == -1;
533 return $ret
534 }
535  
536 $is_type = is_f16_zero($_[0]);
537 if($is_type) {
538 $ret = '0000';
539 $ret = '8000' if $is_type == -1;
540 return $ret
541 }
542  
543 die "Unrecognized type passed to _unpack_irregular()";
544 }
545  
546 sub _get_norm_max {
547 my $ret = 0;
548 for my $p(1 .. f16_MANTBITS) { $ret += 2 ** (f16_EMAX - $p) }
549 return $ret;
550 }
551  
552 sub _get_denorm_max {
553 my $ret = 0;
554 my $max = -(f16_EMIN - 1);
555 my $min = $max - (f16_MANTBITS - 2);
556 for my $p($min .. $max) { $ret += 2 ** -$p }
557 return $ret;
558 }
559  
560 ############################
561 ############################
562  
563 sub SET_EMIN_EMAX {
564 Rmpfr_set_emin($_[0]);
565 Rmpfr_set_emax($_[1]);
566 }
567  
568 sub RESET_EMIN_EMAX {
569 Rmpfr_set_emin($_[0]);
570 Rmpfr_set_emax($_[1]);
571 }
572 1;
573  
574 __END__
↑ Summary FakeFloat16.xs »