LCOV - code coverage report
Current view: top level - source/maths - Fixed.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 96 98 98.0 %
Date: 2026-12-03 01:19:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Fixed.h"
      21             : 
      22             : #include "ps/CStr.h"
      23             : #include "ps/strings/StringBuilder.h"
      24             : 
      25             : #include <string>
      26             : 
      27             : template<>
      28       74554 : CFixed_15_16 CFixed_15_16::FromString(const CStr8& s)
      29             : {
      30             :     // Parse a superset of the xsd:decimal syntax: [-+]?\d*(\.\d*)?
      31             : 
      32       74554 :     if (s.empty())
      33         198 :         return CFixed_15_16::Zero();
      34             : 
      35       74356 :     bool neg = false;
      36       74356 :     CFixed_15_16 r;
      37       74356 :     const char* c = &s[0];
      38             : 
      39       74356 :     if (*c == '+')
      40             :     {
      41           2 :         ++c;
      42             :     }
      43       74354 :     else if (*c == '-')
      44             :     {
      45         155 :         ++c;
      46         155 :         neg = true;
      47             :     }
      48             : 
      49      228946 :     while (true)
      50             :     {
      51             :         // Integer part:
      52      151651 :         if (*c >= '0' && *c <= '9')
      53             :         {
      54       77295 :             r = r * 10; // TODO: handle overflow gracefully, maybe
      55       77295 :             r += CFixed_15_16::FromInt(*c - '0');
      56       77295 :             ++c;
      57             :         }
      58       74356 :         else if (*c == '.')
      59             :         {
      60       70223 :             ++c;
      61       70223 :             std::uint32_t frac = 0;
      62       70223 :             std::uint32_t div = 1;
      63             :             // Fractional part
      64      338885 :             while (*c >= '0' && *c <= '9')
      65             :             {
      66      324443 :                 frac *= 10;
      67      324443 :                 frac += (*c - '0');
      68      324443 :                 div *= 10;
      69      324443 :                 ++c;
      70      324443 :                 if (div >= 100000)
      71             :                 {
      72             :                     // any further digits will be too small to have any major effect
      73             :                     break;
      74             :                 }
      75             :             }
      76             :             // too many digits or invalid character or end of string - add the fractional part and stop
      77       70223 :             r += CFixed_15_16((static_cast<std::uint64_t>(frac) << 16) / div);
      78       70223 :             break;
      79             :         }
      80             :         else
      81             :         {
      82             :             // invalid character or end of string
      83             :             break;
      84             :         }
      85       77295 :     }
      86             : 
      87       74356 :     return (neg ? -r : r);
      88             : }
      89             : 
      90             : template<>
      91           0 : CFixed_15_16 CFixed_15_16::FromString(const CStrW& s)
      92             : {
      93           0 :     return FromString(s.ToUTF8());
      94             : }
      95             : 
      96             : template<>
      97       68542 : CStr8 CFixed_15_16::ToString() const
      98             : {
      99       68542 :     char buffer[16];
     100       68542 :     PS::StringBuilder builder({std::begin(buffer), std::end(buffer)});
     101             : 
     102       68542 :     std::uint32_t posvalue = abs(value);
     103       68542 :     if (value < 0)
     104           6 :         builder.Append('-');
     105             : 
     106       68542 :     builder.Append(posvalue >> fract_bits);
     107             : 
     108       68542 :     std::uint16_t fraction = posvalue & ((1 << fract_bits) - 1);
     109       68542 :     if (fraction)
     110             :     {
     111       67157 :         builder.Append('.');
     112             : 
     113             :         std::uint32_t frac = 0;
     114             :         std::uint32_t div = 1;
     115             : 
     116             :         // Do the inverse of FromString: Keep adding digits until (frac<<16)/div == expected fraction
     117      579413 :         while (true)
     118             :         {
     119      323285 :             frac *= 10;
     120      323285 :             div *= 10;
     121             : 
     122             :             // Low estimate of d such that ((frac+d)<<16)/div == fraction
     123      323285 :             std::uint32_t digit = ((static_cast<std::uint64_t>(fraction) * div) >> 16) - frac;
     124      323285 :             frac += digit;
     125             : 
     126             :             // If this gives the exact target, then add the digit and stop
     127      323285 :             if ((static_cast<std::uint64_t>(frac) << 16) / div == fraction)
     128             :             {
     129          93 :                 builder.Append(digit);
     130          93 :                 break;
     131             :             }
     132             : 
     133             :             // If the next higher digit gives the exact target, then add that digit and stop
     134      323192 :             if (digit <= 8 && ((static_cast<std::uint64_t>(frac) + 1) << 16) / div == fraction)
     135             :             {
     136       67064 :                 builder.Append(digit+1);
     137       67064 :                 break;
     138             :             }
     139             : 
     140             :             // Otherwise add the digit and continue
     141      256128 :             builder.Append(digit);
     142      256128 :         }
     143             :     }
     144             : 
     145       68542 :     return CStr(builder.Str());
     146             : }
     147             : 
     148             : // Based on http://www.dspguru.com/dsp/tricks/fixed-point-atan2-with-self-normalization
     149       14945 : CFixed_15_16 atan2_approx(CFixed_15_16 y, CFixed_15_16 x)
     150             : {
     151       14945 :     CFixed_15_16 zero;
     152             : 
     153             :     // Special case to avoid division-by-zero
     154       14945 :     if (x.IsZero() && y.IsZero())
     155           1 :         return zero;
     156             : 
     157       14944 :     CFixed_15_16 c1;
     158       14944 :     c1.SetInternalValue(51472); // pi/4 << 16
     159             : 
     160       14944 :     CFixed_15_16 c2;
     161       14944 :     c2.SetInternalValue(154415); // 3*pi/4 << 16
     162             : 
     163       14944 :     CFixed_15_16 abs_y = y.Absolute();
     164             : 
     165       14944 :     CFixed_15_16 angle;
     166       14944 :     if (x >= zero)
     167             :     {
     168       14932 :         CFixed_15_16 r = (x - abs_y) / (x + abs_y);
     169       14932 :         angle = c1 - c1.Multiply(r);
     170             :     }
     171             :     else
     172             :     {
     173          12 :         CFixed_15_16 r = (x + abs_y) / (abs_y - x);
     174          12 :         angle = c2 - c1.Multiply(r);
     175             :     }
     176             : 
     177       14944 :     if (y < zero)
     178        2805 :         return -angle;
     179             :     else
     180       12139 :         return angle;
     181             : }
     182             : 
     183             : template<>
     184       40533 : CFixed_15_16 CFixed_15_16::Pi()
     185             : {
     186       40533 :     return CFixed_15_16(205887); // = pi << 16
     187             : }
     188             : 
     189       19622 : void sincos_approx(CFixed_15_16 a, CFixed_15_16& sin_out, CFixed_15_16& cos_out)
     190             : {
     191             :     // Based on http://www.coranac.com/2009/07/sines/
     192             : 
     193             :     // TODO: this could be made a bit more precise by being careful about scaling
     194             : 
     195       19622 :     typedef CFixed_15_16 fixed;
     196             : 
     197       19622 :     fixed c2_pi;
     198       19622 :     c2_pi.SetInternalValue(41721); // = 2/pi << 16
     199             : 
     200             :     // Map radians onto the range [0, 4)
     201       19622 :     fixed z = a.Multiply(c2_pi) % fixed::FromInt(4);
     202             : 
     203             :     // Map z onto the range [-1, +1] for sin, and the same with z+1 to compute cos
     204       19622 :     fixed sz, cz;
     205       19622 :     if (z >= fixed::FromInt(3)) // [3, 4)
     206             :     {
     207        4528 :         sz = z - fixed::FromInt(4);
     208        4528 :         cz = z - fixed::FromInt(3);
     209             :     }
     210       15094 :     else if (z >= fixed::FromInt(2)) // [2, 3)
     211             :     {
     212        3142 :         sz = fixed::FromInt(2) - z;
     213        3142 :         cz = z - fixed::FromInt(3);
     214             :     }
     215       11952 :     else if (z >= fixed::FromInt(1)) // [1, 2)
     216             :     {
     217        3145 :         sz = fixed::FromInt(2) - z;
     218        3145 :         cz = fixed::FromInt(1) - z;
     219             :     }
     220             :     else // [0, 1)
     221             :     {
     222        8807 :         sz = z;
     223        8807 :         cz = fixed::FromInt(1) - z;
     224             :     }
     225             : 
     226             :     // Third-order (max absolute error ~0.02)
     227             : 
     228             : //  sin_out = (sz / 2).Multiply(fixed::FromInt(3) - sz.Multiply(sz));
     229             : //  cos_out = (cz / 2).Multiply(fixed::FromInt(3) - cz.Multiply(cz));
     230             : 
     231             :     // Fifth-order (max absolute error ~0.0005)
     232             : 
     233       19622 :     fixed sz2 = sz.Multiply(sz);
     234       19622 :     sin_out = sz.Multiply(fixed::Pi() - sz2.Multiply(fixed::Pi()*2 - fixed::FromInt(5) - sz2.Multiply(fixed::Pi() - fixed::FromInt(3)))) / 2;
     235             : 
     236       19622 :     fixed cz2 = cz.Multiply(cz);
     237       19622 :     cos_out = cz.Multiply(fixed::Pi() - cz2.Multiply(fixed::Pi()*2 - fixed::FromInt(5) - cz2.Multiply(fixed::Pi() - fixed::FromInt(3)))) / 2;
     238       19622 : }

Generated by: LCOV version 1.16