src/audio/sdlgenaudiocvt.pl
changeset 2011 b2b7154ce016
parent 1985 8055185ae4ed
child 2859 99210400e8b9
equal deleted inserted replaced
2010:39897da56f63 2011:b2b7154ce016
    52 
    52 
    53 /* *INDENT-OFF* */
    53 /* *INDENT-OFF* */
    54 
    54 
    55 EOF
    55 EOF
    56 
    56 
    57     my @vals = ( 127, 255, 32767, 65535, 2147483647 );
    57     my @vals = ( 127, 32767, 2147483647 );
    58     foreach (@vals) {
    58     foreach (@vals) {
    59         my $val = $_;
    59         my $val = $_;
    60         my $fval = 1.0 / $val;
    60         my $fval = 1.0 / $val;
    61         print("#define DIVBY${val} ${fval}f\n");
    61         print("#define DIVBY${val} ${fval}f\n");
    62     }
    62     }
   111     return "${code}";
   111     return "${code}";
   112 }
   112 }
   113 
   113 
   114 
   114 
   115 sub maxIntVal {
   115 sub maxIntVal {
   116     my ($signed, $size) = @_;
   116     my $size = shift;
   117     if ($signed) {
   117     if ($size == 8) {
   118         if ($size == 8) {
   118         return 0x7F;
   119             return 0x7F;
   119     } elsif ($size == 16) {
   120         } elsif ($size == 16) {
   120         return 0x7FFF;
   121             return 0x7FFF;
   121     } elsif ($size == 32) {
   122         } elsif ($size == 32) {
   122         return 0x7FFFFFFF;
   123             return 0x7FFFFFFF;
       
   124         }
       
   125     } else {
       
   126         if ($size == 8) {
       
   127             return 0xFF;
       
   128         } elsif ($size == 16) {
       
   129             return 0xFFFF;
       
   130         } elsif ($size == 32) {
       
   131             return 0xFFFFFFFF;
       
   132         }
       
   133     }
   123     }
   134 
   124 
   135     die("bug in script.\n");
   125     die("bug in script.\n");
   136 }
   126 }
   137 
   127 
   138 sub getFloatToIntMult {
   128 sub getFloatToIntMult {
   139     my ($signed, $size) = @_;
   129     my $size = shift;
   140     my $val = maxIntVal($signed, $size) . '.0';
   130     my $val = maxIntVal($size) . '.0';
   141     $val .= 'f' if ($size < 32);
   131     $val .= 'f' if ($size < 32);
   142     return $val;
   132     return $val;
   143 }
   133 }
   144 
   134 
   145 sub getIntToFloatDivBy {
   135 sub getIntToFloatDivBy {
   146     my ($signed, $size) = @_;
   136     my $size = shift;
   147     return 'DIVBY' . maxIntVal($signed, $size);
   137     return 'DIVBY' . maxIntVal($size);
   148 }
   138 }
   149 
   139 
   150 sub getSignFlipVal {
   140 sub getSignFlipVal {
   151     my $size = shift;
   141     my $size = shift;
   152     if ($size == 8) {
   142     if ($size == 8) {
   213         # Have to convert to/from float/int.
   203         # Have to convert to/from float/int.
   214         # !!! FIXME: cast through double for int32<->float?
   204         # !!! FIXME: cast through double for int32<->float?
   215         my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
   205         my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
   216         if ($ffloat != $tfloat) {
   206         if ($ffloat != $tfloat) {
   217             if ($ffloat) {
   207             if ($ffloat) {
   218                 my $mult = getFloatToIntMult($tsigned, $tsize);
   208                 my $mult = getFloatToIntMult($tsize);
       
   209                 if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
       
   210                     $code = "($code + 1.0f)";
       
   211                 }
   219                 $code = "(($tctype) ($code * $mult))";
   212                 $code = "(($tctype) ($code * $mult))";
   220             } else {
   213             } else {
   221                 # $divby will be the reciprocal, to avoid pipeline stalls
   214                 # $divby will be the reciprocal, to avoid pipeline stalls
   222                 #  from floating point division...so multiply it.
   215                 #  from floating point division...so multiply it.
   223                 my $divby = getIntToFloatDivBy($fsigned, $fsize);
   216                 my $divby = getIntToFloatDivBy($fsize);
   224                 $code = "(((float) $code) * $divby)";
   217                 $code = "(((float) $code) * $divby)";
       
   218                 if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
       
   219                     $code = "($code - 1.0f)";
       
   220                 }
   225             }
   221             }
   226         } else {
   222         } else {
   227             # All integer conversions here.
   223             # All integer conversions here.
   228             if ($fsigned != $tsigned) {
   224             if ($fsigned != $tsigned) {
   229                 my $signflipval = getSignFlipVal($fsize);
   225                 my $signflipval = getSignFlipVal($fsize);