Math Functions
v3{bigger}{smaller}Syntax#include "cosmmath.h" u64 v3u64u32( u32 a ); s64 v3s64s32( s32 a ); u128 v3u128u32( u32 a ); s128 v3s128s32( s32 a ); u128 v3u128u64( u64 a ); s128 v3s128s64( s64 a ); DescriptionConverts an integer into a larger sized integer. Cannot convert signed to unsigned, or unsigned to signed. Return ValuesThe larger sized integer. ErrorsNone. Exampleu32 cat = 39; u64 dog; dog = v3u64u32( cat ); v3{smaller}{bigger}Syntax#include "cosmmath.h" u32 v3u32u64( u64 a ); s32 v3s32s64( s64 a ); u32 v3u32u128( u128 a ); s32 v3s32s128( s128 a ); u64 v3u64u128( u128 a ); s64 v3s64s128( s128 a ); DescriptionConverts an integer into a smaller sized integer. Truncation will occur. Cannot convert signed to unsigned, or unsigned to signed. Return ValuesThe smaller sized integer. ErrorsNone. Exampleu64 pig = 39; u32 ham; ham = v3u32u64( pig ); v3{unsigned}{signed}Syntax#include "cosmmath.h" u64 v3u64s64( s64 a ); /* u64 <- s64 */ u128 v3u128s128( s128 a ); /* u128 <- s128 */ DescriptionConverts a signed integer into an unsigned integer of the same size. The sign may change. Return ValuesThe unsigned integer. ErrorsNone. Examples64 a = -39; u64 b; b = v3u64u64( a ); v3{signed}{unsigned}Syntax#include "cosmmath.h" s64 v3s64u64( u64 a ); /* s64 <- u64 */ s128 v3s128u128( u128 a ); /* s128 <- u128 */ DescriptionConverts an unsigned integer into a signed integer of the same size. The sign may change. Return ValuesThe signed integer. ErrorsNone. Exampleu64 a = 27; s64 b; b = v3u64u64( a ); v3AddSyntax#include "cosmmath.h" u64 v3u64Add( u64 a, u64 b ); s64 v3s64Add( s64 a, s64 b ); u128 v3u128Add( u128 a, u128 b ); s128 v3s128Add( s128 a, s128 b ); DescriptionAdds two integers of specified type and returns the result. Return ValuesThe sum of the two arguments. ErrorsNone. Exampleu64 expenses = 45; u64 fees = 102; u64 total_costs; total_costs = v3u64Add( fees, expenses ); v3SubSyntax#include "cosmmath.h" u64 v3u64Sub( u64 a, u64 b ); s64 v3s64Sub( s64 a, s64 b ); u128 v3u128Sub( u128 a, u128 b ); s128 v3s128Sub( s128 a, s128 b ); DescriptionSubtracts two integers of specified type and returns the result. Return ValuesThe difference of the two arguments. ErrorsNone. Exampleu64 income = 235; u64 expenses = 19; u64 net; net = v3u64Sub( income, expenses ); v3MulSyntax#include "cosmmath.h" u64 v3u64Mul( u64 a, u64 b ); s64 v3s64Mul( s64 a, s64 b ); u128 v3u128Mul( u128 a, u128 b ); s128 v3s128Mul( s128 a, s128 b ); DescriptionMultiplies two integers of specified type and returns the result. Return ValuesThe product of the two arguments. ErrorsNone. Exampleu64 length = 7; u64 width = 13; u64 area; area = v3u64Mul( length, width ); v3DivSyntax#include "cosmmath.h" u64 v3u64Div( u64 a, u64 b ); s64 v3s64Div( s64 a, s64 b ); u128 v3u128Div( u128 a, u128 b ); s128 v3s128Div( s128 a, s128 b ); DescriptionDivides two integers of specified type and returns the result. Return ValuesThe quotient of the two arguments. ErrorsIf b is zero (divide by zero), 0 will be returned. Exampleu64 candies = 100; u64 children = 5; u64 candies_each; candies_each = v3u64Div( candies, children ); v3ModSyntax#include "cosmmath.h" u64 v3u64Mod( u64 a, u64 b ); s64 v3s64Mod( s64 a, s64 b ); u128 v3u128Mod( u128 a, u128 b ); s128 v3s128Mod( s128 a, s128 b ); DescriptionFinds the residue class of a mod b. Return Values0 to |b|-1. ErrorsIf b is zero (divide by zero), 0 will be returned. Exampleu64 cards = 52; u64 players = 102; u64 leftover_cards; leftover_cards = v3u64Mod( cards, players ); v3IncSyntax#include "cosmmath.h" u64 v3u64Inc( u64 * a ); s64 v3s64Inc( s64 * a ); u128 v3u128Inc( u128 * a ); s128 v3s128Inc( s128 * a ); DescriptionAdds one to an integer of specified type, by reference. Return ValuesThe value of a BEFORE it is incremented. a is incremented, possibly wrapping the value of a. The return value is equivalent to a++. ErrorsNone. Exampleu64 i, j; _V3_SET64( i, 01234567, 89ABCDEF ); j = v3u64Inc( &i ); /* i is now 0x0123456789ABCDF0 and j is 0x0123456789ABCDEF */ v3DecSyntax#include "cosmmath.h" u64 v3u64Dec( u64 * a ); s64 v3s64Dec( s64 * a ); u128 v3u128Dec( u128 * a ); s128 v3s128Dec( s128 * a ); DescriptionSubtracts one from an integer of specified type, by reference. Return ValuesThe value of a BEFORE it is decremented. a is decremented, possibly wrapping the value of a. The return value is equivalent to a--. ErrorsNone. Exampleu64 i; _V3_SET64( i, 13845A34, 09AB4DEF ); j = v3u64Dec( &i ); /* i is now 0x0123456789ABCDEE and j is 0x0123456789ABCDEF */ v3EqSyntax#include "cosmmath.h" u32 v3u64Eq( u64 a, u64 b ); u32 v3s64Eq( s64 a, s64 b ); u32 v3u128Eq( u128 a, u128 b ); u32 v3s128Eq( s128 a, s128 b ); DescriptionChecks the equality of two integers of specified type. Return Values1 if the numbers are equal, or 0 if they are not equal. ErrorsNone. Exampleu64 bytes_received = 45; u64 bytes_sent = 39; u32 alldatasent; alldatasent = v3u64Eq( bytes_received, bytes_sent ); v3GtSyntax#include "cosmmath.h" u32 v3u64Gt( u64 a, u64 b ); u32 v3s64Gt( s64 a, s64 b ); u32 v3u128Gt( u128 a, u128 b ); u32 v3s128Gt( s128 a, s128 b ); DescriptionChecks to see if the first integer of specified type is greater than the second. Return Values1 if the first number is greater than the second, otherwise 0. ErrorsNone. Exampleu64 chairs = 45; u64 people = 39; u32 enough_chairs; enough_chairs = v3u64Gt( chairs, people ); enough_chairs would be 1. v3LtSyntax#include "cosmmath.h" u32 v3u64Lt( u64 a, u64 b ); u32 v3s64Lt( s64 a, s64 b ); u32 v3u128Lt( u128 a, u128 b ); u32 v3s128Lt( s128 a, s128 b ); DescriptionChecks to see if the first integer of specified type is less than the second. Return Values1 if the first number is less than the second, otherwise 0. ErrorsNone. Exampleu64 uv_rays = 27; u64 max = 46; u32 no_sunburn; no_sunburn = v3u64Lt( uv_rays, max ); no_sunburn would be 1. v3AndSyntax#include "cosmmath.h" u64 v3u64And( u64 a, u64 b ); u128 v3u128And( u128 a, u128 b ); DescriptionPerforms the logical AND of two integers of specified type. Return ValuesThe logical AND of the two arguments. ErrorsNone. Exampleu64 foo = 0x0000001C; u64 bar = 0x000001E3; u64 go; go = v3u64And( foo, bar ); v3OrSyntax#include "cosmmath.h" u64 v3u64Or( u64 a, u64 b ); u128 v3u128Or( u128 a, u128 b ); DescriptionPerforms the logical OR of two integers of specified type. Return ValuesThe logical OR of the two arguments. ErrorsNone. Exampleu64 foo = 0x000002E3; u64 bar = 0x0000008F; u64 go; go = v3u64Or( foo, bar ); v3XorSyntax#include "cosmmath.h" u64 v3u64Xor( u64 a, u64 b ); u128 v3u128Xor( u128 a, u128 b ); DescriptionPerforms the logical exclusive OR of two integers of specified type. Return ValuesThe logical exclusive OR of the two arguments. ErrorsNone. Exampleu64 foo = 0x021C4F19; u64 bar = 0x00074B31; u64 go; go = v3u64Xor( foo, bar ); v3NotSyntax#include "cosmmath.h" u64 v3u64Not( u64 a ); u128 v3u128Not( u128 a ); DescriptionPerforms the logical NOT of two integers of specified type. Return ValuesThe logical NOT of the argument. ErrorsNone. Exampleu64 foo = 0x00F0401C; u64 notfoo; notfoo = v3u64Not( foo ); v3LshSyntax#include "cosmmath.h" u64 v3u64Lsh( u64 a, u32 x ); u128 v3u128Lsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the left. Return ValuesThe shifted integer. ErrorsNone. Exampleu64 foo = 0x300E481C; u64 fooshifted; fooshifted = v3u64Lsh( foo, (u32) 4 ); Would shift foo 4 digits left. v3RshSyntax#include "cosmmath.h" u64 v3u64Rsh( u64 a, u32 x ); u128 v3u128Rsh( u128 a, u32 x ); DescriptionShifts an integer of specified type by x digits to the right. Return ValuesThe shifted integer. ErrorsNone. Exampleu64 foo = 0x000F061C; u64 fooshifted; fooshifted = v3u64Rsh( foo, (u32) 2 ); Would shift foo 2 digits right.
© Copyright Mithral Communications & Design, Inc. 1999.
All rights reserved.
Mithral(tm) and Cosm(tm) are trademarks of
Mithral Communications & Design, Inc.
|