plugins/include/float.inc
author David Anderson <dvander@alliedmods.net>
Fri Aug 15 05:22:26 2008 +0000 (2008-08-15)
changeset 2226 d10b38524be3
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Landed sourcepawn-1.2. The big changes:

1) JIT compilation/optimization now occurs per-function, and only when functions are first used. We're now officially a whole-method JIT rather than an AOT compiler (albiet, still a simple JIT). This has two implications: Functions are now much better abstracted internally, and loading a plugin is now much less expensive. If a function contains calls to other functions, THOSE functions are only compiled when they're invoked as well.

2) I've removed debug mode. We always show full backtraces now, as there was a very cheap way to implement this which really cleaned up everything. This is great for a number of reasons -- there's less code, the JIT is better designed, we don't need to relocate debug tables, and best of all we no longer have to tell users to enable debug mode at their own expense.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _float_included
    34  #endinput
    35 #endif
    36 #define _float_included
    37 
    38 /**
    39  * Converts an integer into a floating point value.
    40  *
    41  * @param value			Integer to convert.
    42  * @return				Floating point value.
    43  */
    44 native Float:float(value);
    45 
    46 /**
    47  * Multiplies two floats together.
    48  *
    49  * @param oper1			First value.
    50  * @param oper2			Second value.
    51  * @return				oper1*oper2.
    52  */
    53 native Float:FloatMul(Float:oper1, Float:oper2);
    54 
    55 /**
    56  * Divides the dividend by the divisor.
    57  *
    58  * @param dividend		First value.
    59  * @param divisor		Second value.
    60  * @return				dividend/divisor.
    61  */
    62 native Float:FloatDiv(Float:dividend, Float:divisor);
    63 
    64 /**
    65  * Adds two floats together.
    66  *
    67  * @param oper1			First value.
    68  * @param oper2			Second value.
    69  * @return				oper1+oper2.
    70  */
    71 native Float:FloatAdd(Float:oper1, Float:oper2);
    72 
    73 /**
    74  * Subtracts oper2 from oper1.
    75  *
    76  * @param oper1			First value.
    77  * @param oper2			Second value.
    78  * @return				oper1-oper2.
    79  */
    80 native Float:FloatSub(Float:oper1, Float:oper2);
    81 
    82 /**
    83  * Returns the decimal part of a float.
    84  *
    85  * @param value			Input value.
    86  * @return				Decimal part.
    87  */
    88 native Float:FloatFraction(Float:value);
    89 
    90 /**
    91  * Rounds a float to the closest integer to zero.
    92  *
    93  * @param value			Input value to be rounded.
    94  * @return				Rounded value.
    95  */
    96 native RoundToZero(Float:value);
    97 
    98 /**
    99  * Rounds a float to the next highest integer value.
   100  *
   101  * @param value			Input value to be rounded.
   102  * @return				Rounded value.
   103  */
   104 native RoundToCeil(Float:value);
   105 
   106 /**
   107  * Rounds a float to the next lowest integer value.
   108  *
   109  * @param value			Input value to be rounded.
   110  * @return				Rounded value.
   111  */
   112 native RoundToFloor(Float:value);
   113 
   114 /**
   115  * Standard IEEE rounding.
   116  *
   117  * @param value			Input value to be rounded.
   118  * @return				Rounded value.
   119  */
   120 native RoundToNearest(Float:value);
   121 
   122 /**
   123  * Compares two floats.
   124  *
   125  * @param fOne			First value.
   126  * @param fTwo			Second value.
   127  * @return				Returns 1 if the first argument is greater than the second argument.
   128  *                      Returns -1 if the first argument is smaller than the second argument.
   129  *                      Returns 0 if both arguments are equal.
   130  */
   131 native FloatCompare(Float:fOne, Float:fTwo);
   132 
   133 /**
   134  * Returns the square root of the input value, equivalent to floatpower(value, 0.5).
   135  *
   136  * @param value			Input value.
   137  * @return				Square root of the value.
   138  */
   139 native Float:SquareRoot(Float:value);
   140 
   141 /**
   142  * Returns the value raised to the power of the exponent.
   143  *
   144  * @param value			Value to be raised.
   145  * @param exponent		Value to raise the base.
   146  * @return				value^exponent.
   147  */
   148 native Float:Pow(Float:value, Float:exponent);
   149 
   150 /**
   151  * Returns the value of raising the input by e.
   152  *
   153  * @param value			Input value.
   154  * @return				exp(value).
   155  */
   156 native Float:Exponential(Float:value);
   157 
   158 /**
   159  * Returns the logarithm of any base specified.
   160  *
   161  * @param value			Input value.
   162  * @param base			Logarithm base to use, default is 10.
   163  * @return				log(value)/log(base).
   164  */
   165 native Float:Logarithm(Float:value, Float:base=10.0);
   166 
   167 /**
   168  * Returns the sine of the argument.
   169  *
   170  * @param value			Input value in radians.
   171  * @return				sin(value).
   172  */
   173 native Float:Sine(Float:value);
   174 
   175 /**
   176  * Returns the cosine of the argument.
   177  *
   178  * @param value			Input value in radians.
   179  * @return				cos(value).
   180  */
   181 native Float:Cosine(Float:value);
   182 
   183 /**
   184  * Returns the tangent of the argument.
   185  *
   186  * @param value			Input value in radians.
   187  * @return				tan(value).
   188  */
   189 native Float:Tangent(Float:value);
   190 
   191 /**
   192  * Returns an absolute value.
   193  *
   194  * @param value			Input value.
   195  * @return				Absolute value of the input.
   196  */
   197 native Float:FloatAbs(Float:value);
   198 
   199 /**
   200  * Returns the arctangent of the input value.
   201  *
   202  * @param angle			Input value.
   203  * @return				atan(value) in radians.
   204  */
   205 native Float:ArcTangent(Float:angle);
   206 
   207 /**
   208  * Returns the arccosine of the input value.
   209  *
   210  * @param angle			Input value.
   211  * @return				acos(value) in radians.
   212  */
   213 native Float:ArcCosine(Float:angle);
   214 
   215 /**
   216  * Returns the arcsine of the input value.
   217  *
   218  * @param angle			Input value.
   219  * @return				asin(value) in radians.
   220  */
   221 native Float:ArcSine(Float:angle);
   222 
   223 /**
   224  * Returns the arctangent2 of the input values.
   225  *
   226  * @param x				Horizontal value.
   227  * @param y				Vertical value.
   228  * @return				atan2(value) in radians.
   229  */
   230 native Float:ArcTangent2(Float:x, Float:y);
   231 
   232 /**
   233  * Rounds a floating point number using the "round to nearest" algorithm.
   234  *
   235  * @param value			Floating point value to round.
   236  * @return				The value rounded to the nearest integer.
   237  */
   238 stock RoundFloat(Float:value)
   239 {
   240 	return RoundToNearest(value);
   241 }
   242 
   243 /**
   244  * User defined operators.
   245  *
   246  */
   247 #pragma rational Float
   248 
   249 native Float:operator*(Float:oper1, Float:oper2) = FloatMul;
   250 native Float:operator/(Float:oper1, Float:oper2) = FloatDiv;
   251 native Float:operator+(Float:oper1, Float:oper2) = FloatAdd;
   252 native Float:operator-(Float:oper1, Float:oper2) = FloatSub;
   253 
   254 stock Float:operator++(Float:oper)
   255 {
   256 	return oper+1.0;
   257 }
   258 
   259 stock Float:operator--(Float:oper)
   260 {
   261 	return oper-1.0;
   262 }
   263 
   264 stock Float:operator-(Float:oper)
   265 {
   266 	return oper^Float:((-1)^((-1)/2));				/* IEEE values are sign/magnitude */
   267 }
   268 
   269 stock Float:operator*(Float:oper1, oper2)
   270 {
   271 	return FloatMul(oper1, float(oper2));			/* "*" is commutative */
   272 }
   273 
   274 stock Float:operator/(Float:oper1, oper2)
   275 {
   276 	return FloatDiv(oper1, float(oper2));
   277 }
   278 
   279 stock Float:operator/(oper1, Float:oper2)
   280 {
   281 	return FloatDiv(float(oper1), oper2);
   282 }
   283 
   284 stock Float:operator+(Float:oper1, oper2)
   285 {
   286 	return FloatAdd(oper1, float(oper2));			/* "+" is commutative */
   287 }
   288 
   289 stock Float:operator-(Float:oper1, oper2)
   290 {
   291 	return FloatSub(oper1, float(oper2));
   292 }
   293 
   294 stock Float:operator-(oper1, Float:oper2)
   295 {
   296 	return FloatSub(float(oper1), oper2);
   297 }
   298 
   299 stock bool:operator==(Float:oper1, Float:oper2)
   300 {
   301 	return FloatCompare(oper1, oper2) == 0;
   302 }
   303 
   304 stock bool:operator==(Float:oper1, oper2)
   305 {
   306 	return FloatCompare(oper1, float(oper2)) == 0; 	/* "==" is commutative */
   307 }
   308 
   309 stock bool:operator!=(Float:oper1, Float:oper2)
   310 {
   311 	return FloatCompare(oper1, oper2) != 0;
   312 }
   313 
   314 stock bool:operator!=(Float:oper1, oper2)
   315 {
   316 	return FloatCompare(oper1, float(oper2)) != 0; 	/* "==" is commutative */
   317 }
   318 
   319 stock bool:operator>(Float:oper1, Float:oper2)
   320 {
   321 	return FloatCompare(oper1, oper2) > 0;
   322 }
   323 
   324 stock bool:operator>(Float:oper1, oper2)
   325 {
   326 	return FloatCompare(oper1, float(oper2)) > 0;
   327 }
   328 
   329 stock bool:operator>(oper1, Float:oper2)
   330 {
   331 	return FloatCompare(float(oper1), oper2) > 0;
   332 }
   333 
   334 stock bool:operator>=(Float:oper1, Float:oper2)
   335 {
   336 	return FloatCompare(oper1, oper2) >= 0;
   337 }
   338 
   339 stock bool:operator>=(Float:oper1, oper2)
   340 {
   341 	return FloatCompare(oper1, float(oper2)) >= 0;
   342 }
   343 
   344 stock bool:operator>=(oper1, Float:oper2)
   345 {
   346 	return FloatCompare(float(oper1), oper2) >= 0;
   347 }
   348 
   349 stock bool:operator<(Float:oper1, Float:oper2)
   350 {
   351 	return FloatCompare(oper1, oper2) < 0;
   352 }
   353 
   354 stock bool:operator<(Float:oper1, oper2)
   355 {
   356 	return FloatCompare(oper1, float(oper2)) < 0;
   357 }
   358 
   359 stock bool:operator<(oper1, Float:oper2)
   360 {
   361 	return FloatCompare(float(oper1), oper2) < 0;
   362 }
   363 
   364 stock bool:operator<=(Float:oper1, Float:oper2)
   365 {
   366 	return FloatCompare(oper1, oper2) <= 0;
   367 }
   368 
   369 stock bool:operator<=(Float:oper1, oper2)
   370 {
   371 	return FloatCompare(oper1, float(oper2)) <= 0;
   372 }
   373 
   374 stock bool:operator<=(oper1, Float:oper2)
   375 {
   376 	return FloatCompare(float(oper1), oper2) <= 0;
   377 }
   378 
   379 stock bool:operator!(Float:oper)
   380 {
   381 	return (_:oper & ((-1)/2)) == 0;		/* -1 = all bits to 1; /2 = remove most significant bit (sign)
   382 											works on both 32bit and 64bit systems; no constant required */
   383 }
   384 
   385 /**
   386  * Forbidden operators.
   387  *
   388  */
   389 forward operator%(Float:oper1, Float:oper2);
   390 forward operator%(Float:oper1, oper2);
   391 forward operator%(oper1, Float:oper2);
   392 
   393 #define FLOAT_PI 3.1415926535897932384626433832795
   394 
   395 /**
   396  * Converts degrees to radians.
   397  *
   398  * @param angle		Degrees.
   399  * @return			Radians.
   400  */
   401 stock Float:DegToRad(Float:angle)
   402 {
   403 	return (angle*FLOAT_PI)/180;
   404 }
   405 
   406 /**
   407  * Converts degrees to radians.
   408  *
   409  * @param angle		Radians.
   410  * @return			Degrees.
   411  */
   412 stock Float:RadToDeg(Float:angle)
   413 {
   414 	return (angle*180)/FLOAT_PI;
   415 }