OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [Integration_test/] [synthetic_sim/] [perl_lib/] [Number/] [Compare.pm] - Blame information for rev 56

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 alirezamon
package Number::Compare;
2
use strict;
3
use Carp qw(croak);
4
use vars qw/$VERSION/;
5
$VERSION = '0.03';
6
 
7
sub new  {
8
    my $referent = shift;
9
    my $class = ref $referent || $referent;
10
    my $expr = $class->parse_to_perl( shift );
11
 
12
    bless eval "sub { \$_[0] $expr }", $class;
13
}
14
 
15
sub parse_to_perl {
16
    shift;
17
    my $test = shift;
18
 
19
    $test =~ m{^
20
               ([<>]=?)?   # comparison
21
               (.*?)       # value
22
               ([kmg]i?)?  # magnitude
23
              $}ix
24
       or croak "don't understand '$test' as a test";
25
 
26
    my $comparison = $1 || '==';
27
    my $target     = $2;
28
    my $magnitude  = $3 || '';
29
    $target *=           1000 if lc $magnitude eq 'k';
30
    $target *=           1024 if lc $magnitude eq 'ki';
31
    $target *=        1000000 if lc $magnitude eq 'm';
32
    $target *=      1024*1024 if lc $magnitude eq 'mi';
33
    $target *=     1000000000 if lc $magnitude eq 'g';
34
    $target *= 1024*1024*1024 if lc $magnitude eq 'gi';
35
 
36
    return "$comparison $target";
37
}
38
 
39
sub test { $_[0]->( $_[1] ) }
40
 
41
1;
42
 
43
__END__
44
 
45
=head1 NAME
46
 
47
Number::Compare - numeric comparisons
48
 
49
=head1 SYNOPSIS
50
 
51
 Number::Compare->new(">1Ki")->test(1025); # is 1025 > 1024
52
 
53
 my $c = Number::Compare->new(">1M");
54
 $c->(1_200_000);                          # slightly terser invocation
55
 
56
=head1 DESCRIPTION
57
 
58
Number::Compare compiles a simple comparison to an anonymous
59
subroutine, which you can call with a value to be tested again.
60
 
61
Now this would be very pointless, if Number::Compare didn't understand
62
magnitudes.
63
 
64
The target value may use magnitudes of kilobytes (C<k>, C<ki>),
65
megabytes (C<m>, C<mi>), or gigabytes (C<g>, C<gi>).  Those suffixed
66
with an C<i> use the appropriate 2**n version in accordance with the
67
IEC standard: http://physics.nist.gov/cuu/Units/binary.html
68
 
69
=head1 METHODS
70
 
71
=head2 ->new( $test )
72
 
73
Returns a new object that compares the specified test.
74
 
75
=head2 ->test( $value )
76
 
77
A longhanded version of $compare->( $value ).  Predates blessed
78
subroutine reference implementation.
79
 
80
=head2 ->parse_to_perl( $test )
81
 
82
Returns a perl code fragment equivalent to the test.
83
 
84
=head1 AUTHOR
85
 
86
Richard Clamp <richardc@unixbeard.net>
87
 
88
=head1 COPYRIGHT
89
 
90
Copyright (C) 2002,2011 Richard Clamp.  All Rights Reserved.
91
 
92
This module is free software; you can redistribute it and/or modify it
93
under the same terms as Perl itself.
94
 
95
=head1 SEE ALSO
96
 
97
http://physics.nist.gov/cuu/Units/binary.html
98
 
99
=cut

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.