reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkPoint.h" |
| 3 | #include "SkRandom.h" |
| 4 | |
| 5 | #if defined(SkLONGLONG) |
| 6 | static int symmetric_fixmul(int a, int b) { |
| 7 | int sa = SkExtractSign(a); |
| 8 | int sb = SkExtractSign(b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 9 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | a = SkApplySign(a, sa); |
| 11 | b = SkApplySign(b, sb); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 12 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 13 | #if 1 |
| 14 | int c = (int)(((SkLONGLONG)a * b) >> 16); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 15 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | return SkApplySign(c, sa ^ sb); |
| 17 | #else |
| 18 | SkLONGLONG ab = (SkLONGLONG)a * b; |
| 19 | if (sa ^ sb) { |
| 20 | ab = -ab; |
| 21 | } |
| 22 | return ab >> 16; |
| 23 | #endif |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | static void check_length(skiatest::Reporter* reporter, |
| 28 | const SkPoint& p, SkScalar targetLen) { |
| 29 | #ifdef SK_CAN_USE_FLOAT |
| 30 | float x = SkScalarToFloat(p.fX); |
| 31 | float y = SkScalarToFloat(p.fY); |
| 32 | float len = sk_float_sqrt(x*x + y*y); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 33 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 34 | len /= SkScalarToFloat(targetLen); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 35 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
| 37 | #endif |
| 38 | } |
| 39 | |
| 40 | #if defined(SK_CAN_USE_FLOAT) |
| 41 | |
| 42 | static float nextFloat(SkRandom& rand) { |
| 43 | SkFloatIntUnion data; |
| 44 | data.fSignBitInt = rand.nextU(); |
| 45 | return data.fFloat; |
| 46 | } |
| 47 | |
| 48 | /* returns true if a == b as resulting from (int)x. Since it is undefined |
| 49 | what to do if the float exceeds 2^32-1, we check for that explicitly. |
| 50 | */ |
| 51 | static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { |
| 52 | if (!(x == x)) { // NAN |
| 53 | return si == SK_MaxS32 || si == SK_MinS32; |
| 54 | } |
| 55 | // for out of range, C is undefined, but skia always should return NaN32 |
| 56 | if (x > SK_MaxS32) { |
| 57 | return si == SK_MaxS32; |
| 58 | } |
| 59 | if (x < -SK_MaxS32) { |
| 60 | return si == SK_MinS32; |
| 61 | } |
| 62 | return si == ni; |
| 63 | } |
| 64 | |
| 65 | static void assert_float_equal(skiatest::Reporter* reporter, const char op[], |
| 66 | float x, uint32_t ni, uint32_t si) { |
| 67 | if (!equal_float_native_skia(x, ni, si)) { |
| 68 | SkString desc; |
| 69 | desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si); |
| 70 | reporter->reportFailed(desc); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void test_float_cast(skiatest::Reporter* reporter, float x) { |
| 75 | int ix = (int)x; |
| 76 | int iix = SkFloatToIntCast(x); |
| 77 | assert_float_equal(reporter, "cast", x, ix, iix); |
| 78 | } |
| 79 | |
| 80 | static void test_float_floor(skiatest::Reporter* reporter, float x) { |
| 81 | int ix = (int)floor(x); |
| 82 | int iix = SkFloatToIntFloor(x); |
| 83 | assert_float_equal(reporter, "floor", x, ix, iix); |
| 84 | } |
| 85 | |
| 86 | static void test_float_round(skiatest::Reporter* reporter, float x) { |
| 87 | double xx = x + 0.5; // need intermediate double to avoid temp loss |
| 88 | int ix = (int)floor(xx); |
| 89 | int iix = SkFloatToIntRound(x); |
| 90 | assert_float_equal(reporter, "round", x, ix, iix); |
| 91 | } |
| 92 | |
| 93 | static void test_float_ceil(skiatest::Reporter* reporter, float x) { |
| 94 | int ix = (int)ceil(x); |
| 95 | int iix = SkFloatToIntCeil(x); |
| 96 | assert_float_equal(reporter, "ceil", x, ix, iix); |
| 97 | } |
| 98 | |
| 99 | static void test_float_conversions(skiatest::Reporter* reporter, float x) { |
| 100 | test_float_cast(reporter, x); |
| 101 | test_float_floor(reporter, x); |
| 102 | test_float_round(reporter, x); |
| 103 | test_float_ceil(reporter, x); |
| 104 | } |
| 105 | |
| 106 | static void test_int2float(skiatest::Reporter* reporter, int ival) { |
| 107 | float x0 = (float)ival; |
| 108 | float x1 = SkIntToFloatCast(ival); |
| 109 | float x2 = SkIntToFloatCast_NoOverflowCheck(ival); |
| 110 | REPORTER_ASSERT(reporter, x0 == x1); |
| 111 | REPORTER_ASSERT(reporter, x0 == x2); |
| 112 | } |
| 113 | |
| 114 | static void unittest_fastfloat(skiatest::Reporter* reporter) { |
| 115 | SkRandom rand; |
| 116 | size_t i; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 117 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 118 | static const float gFloats[] = { |
| 119 | 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, |
| 120 | 0.000000001f, 1000000000.f, // doesn't overflow |
| 121 | 0.0000000001f, 10000000000.f // does overflow |
| 122 | }; |
| 123 | for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 124 | test_float_conversions(reporter, gFloats[i]); |
| 125 | test_float_conversions(reporter, -gFloats[i]); |
| 126 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 127 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 128 | for (int outer = 0; outer < 100; outer++) { |
| 129 | rand.setSeed(outer); |
| 130 | for (i = 0; i < 100000; i++) { |
| 131 | float x = nextFloat(rand); |
| 132 | test_float_conversions(reporter, x); |
| 133 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 134 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 135 | test_int2float(reporter, 0); |
| 136 | test_int2float(reporter, 1); |
| 137 | test_int2float(reporter, -1); |
| 138 | for (i = 0; i < 100000; i++) { |
| 139 | // for now only test ints that are 24bits or less, since we don't |
| 140 | // round (down) large ints the same as IEEE... |
| 141 | int ival = rand.nextU() & 0xFFFFFF; |
| 142 | test_int2float(reporter, ival); |
| 143 | test_int2float(reporter, -ival); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #endif |
| 149 | |
| 150 | static void test_muldiv255(skiatest::Reporter* reporter) { |
| 151 | #ifdef SK_CAN_USE_FLOAT |
| 152 | for (int a = 0; a <= 255; a++) { |
| 153 | for (int b = 0; b <= 255; b++) { |
| 154 | int ab = a * b; |
| 155 | float s = ab / 255.0f; |
| 156 | int round = (int)floorf(s + 0.5f); |
| 157 | int trunc = (int)floorf(s); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 158 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 159 | int iround = SkMulDiv255Round(a, b); |
| 160 | int itrunc = SkMulDiv255Trunc(a, b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 161 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, iround == round); |
| 163 | REPORTER_ASSERT(reporter, itrunc == trunc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 164 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, itrunc <= iround); |
| 166 | REPORTER_ASSERT(reporter, iround <= a); |
| 167 | REPORTER_ASSERT(reporter, iround <= b); |
| 168 | } |
| 169 | } |
| 170 | #endif |
| 171 | } |
| 172 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 173 | static void TestMath(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 174 | int i; |
| 175 | int32_t x; |
| 176 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 177 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 178 | // these should assert |
| 179 | #if 0 |
| 180 | SkToS8(128); |
| 181 | SkToS8(-129); |
| 182 | SkToU8(256); |
| 183 | SkToU8(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 184 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 185 | SkToS16(32768); |
| 186 | SkToS16(-32769); |
| 187 | SkToU16(65536); |
| 188 | SkToU16(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 189 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 190 | if (sizeof(size_t) > 4) { |
| 191 | SkToS32(4*1024*1024); |
| 192 | SkToS32(-4*1024*1024); |
| 193 | SkToU32(5*1024*1024); |
| 194 | SkToU32(-5); |
| 195 | } |
| 196 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 197 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 198 | test_muldiv255(reporter); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 199 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 200 | { |
| 201 | SkScalar x = SK_ScalarNaN; |
| 202 | REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
| 203 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 204 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 205 | for (i = 1; i <= 10; i++) { |
| 206 | x = SkCubeRootBits(i*i*i, 11); |
| 207 | REPORTER_ASSERT(reporter, x == i); |
| 208 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 209 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 210 | x = SkFixedSqrt(SK_Fixed1); |
| 211 | REPORTER_ASSERT(reporter, x == SK_Fixed1); |
| 212 | x = SkFixedSqrt(SK_Fixed1/4); |
| 213 | REPORTER_ASSERT(reporter, x == SK_Fixed1/2); |
| 214 | x = SkFixedSqrt(SK_Fixed1*4); |
| 215 | REPORTER_ASSERT(reporter, x == SK_Fixed1*2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 216 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 217 | x = SkFractSqrt(SK_Fract1); |
| 218 | REPORTER_ASSERT(reporter, x == SK_Fract1); |
| 219 | x = SkFractSqrt(SK_Fract1/4); |
| 220 | REPORTER_ASSERT(reporter, x == SK_Fract1/2); |
| 221 | x = SkFractSqrt(SK_Fract1/16); |
| 222 | REPORTER_ASSERT(reporter, x == SK_Fract1/4); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 223 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 224 | for (i = 1; i < 100; i++) { |
| 225 | x = SkFixedSqrt(SK_Fixed1 * i * i); |
| 226 | REPORTER_ASSERT(reporter, x == SK_Fixed1 * i); |
| 227 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 228 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 229 | for (i = 0; i < 1000; i++) { |
| 230 | int value = rand.nextS16(); |
| 231 | int max = rand.nextU16(); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 232 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 233 | int clamp = SkClampMax(value, max); |
| 234 | int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
| 235 | REPORTER_ASSERT(reporter, clamp == clamp2); |
| 236 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 237 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 238 | for (i = 0; i < 100000; i++) { |
| 239 | SkPoint p; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 240 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 241 | p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1); |
| 242 | check_length(reporter, p, SK_Scalar1); |
| 243 | p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1); |
| 244 | check_length(reporter, p, SK_Scalar1); |
| 245 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 246 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 247 | { |
| 248 | SkFixed result = SkFixedDiv(100, 100); |
| 249 | REPORTER_ASSERT(reporter, result == SK_Fixed1); |
| 250 | result = SkFixedDiv(1, SK_Fixed1); |
| 251 | REPORTER_ASSERT(reporter, result == 1); |
| 252 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 253 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 254 | #ifdef SK_CAN_USE_FLOAT |
| 255 | unittest_fastfloat(reporter); |
| 256 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 257 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 258 | #ifdef SkLONGLONG |
| 259 | for (i = 0; i < 100000; i++) { |
| 260 | SkFixed numer = rand.nextS(); |
| 261 | SkFixed denom = rand.nextS(); |
| 262 | SkFixed result = SkFixedDiv(numer, denom); |
| 263 | SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 264 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 265 | (void)SkCLZ(numer); |
| 266 | (void)SkCLZ(denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 267 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 268 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 269 | if (check > SK_MaxS32) { |
| 270 | check = SK_MaxS32; |
| 271 | } else if (check < -SK_MaxS32) { |
| 272 | check = SK_MinS32; |
| 273 | } |
| 274 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 275 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 276 | result = SkFractDiv(numer, denom); |
| 277 | check = ((SkLONGLONG)numer << 30) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 278 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 280 | if (check > SK_MaxS32) { |
| 281 | check = SK_MaxS32; |
| 282 | } else if (check < -SK_MaxS32) { |
| 283 | check = SK_MinS32; |
| 284 | } |
| 285 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 286 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 287 | // make them <= 2^24, so we don't overflow in fixmul |
| 288 | numer = numer << 8 >> 8; |
| 289 | denom = denom << 8 >> 8; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 290 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 291 | result = SkFixedMul(numer, denom); |
| 292 | SkFixed r2 = symmetric_fixmul(numer, denom); |
| 293 | // SkASSERT(result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 294 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 295 | result = SkFixedMul(numer, numer); |
| 296 | r2 = SkFixedSquare(numer); |
| 297 | REPORTER_ASSERT(reporter, result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 298 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 299 | #ifdef SK_CAN_USE_FLOAT |
| 300 | if (numer >= 0 && denom >= 0) { |
| 301 | SkFixed mean = SkFixedMean(numer, denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 302 | float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom); |
| 303 | float fm = sk_float_sqrt(sk_float_abs(prod)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 304 | SkFixed mean2 = SkFloatToFixed(fm); |
| 305 | int diff = SkAbs32(mean - mean2); |
| 306 | REPORTER_ASSERT(reporter, diff <= 1); |
| 307 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 308 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 309 | { |
| 310 | SkFixed mod = SkFixedMod(numer, denom); |
| 311 | float n = SkFixedToFloat(numer); |
| 312 | float d = SkFixedToFloat(denom); |
| 313 | float m = sk_float_mod(n, d); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 314 | // ensure the same sign |
| 315 | REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 316 | int diff = SkAbs32(mod - SkFloatToFixed(m)); |
| 317 | REPORTER_ASSERT(reporter, (diff >> 7) == 0); |
| 318 | } |
| 319 | #endif |
| 320 | } |
| 321 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 322 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 323 | #ifdef SK_CAN_USE_FLOAT |
| 324 | for (i = 0; i < 100000; i++) { |
| 325 | SkFract x = rand.nextU() >> 1; |
| 326 | double xx = (double)x / SK_Fract1; |
| 327 | SkFract xr = SkFractSqrt(x); |
| 328 | SkFract check = SkFloatToFract(sqrt(xx)); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 329 | REPORTER_ASSERT(reporter, xr == check || |
| 330 | xr == check-1 || |
| 331 | xr == check+1); |
| 332 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 333 | xr = SkFixedSqrt(x); |
| 334 | xx = (double)x / SK_Fixed1; |
| 335 | check = SkFloatToFixed(sqrt(xx)); |
| 336 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 337 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 338 | xr = SkSqrt32(x); |
| 339 | xx = (double)x; |
| 340 | check = (int32_t)sqrt(xx); |
| 341 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
| 342 | } |
| 343 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 344 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 345 | #if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT) |
| 346 | { |
| 347 | SkFixed s, c; |
| 348 | s = SkFixedSinCos(0, &c); |
| 349 | REPORTER_ASSERT(reporter, s == 0); |
| 350 | REPORTER_ASSERT(reporter, c == SK_Fixed1); |
| 351 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 352 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 353 | int maxDiff = 0; |
| 354 | for (i = 0; i < 10000; i++) { |
| 355 | SkFixed rads = rand.nextS() >> 10; |
| 356 | double frads = SkFixedToFloat(rads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 357 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 358 | SkFixed s, c; |
| 359 | s = SkScalarSinCos(rads, &c); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 360 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 361 | double fs = sin(frads); |
| 362 | double fc = cos(frads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 363 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 364 | SkFixed is = SkFloatToFixed(fs); |
| 365 | SkFixed ic = SkFloatToFixed(fc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 366 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 367 | maxDiff = SkMax32(maxDiff, SkAbs32(is - s)); |
| 368 | maxDiff = SkMax32(maxDiff, SkAbs32(ic - c)); |
| 369 | } |
| 370 | SkDebugf("SinCos: maximum error = %d\n", maxDiff); |
| 371 | #endif |
| 372 | } |
| 373 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 374 | #include "TestClassDef.h" |
| 375 | DEFINE_TESTCLASS("Math", MathTestClass, TestMath) |