reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 1 | #include "Test.h" |
reed@android.com | c846ede | 2010-04-13 15:29:15 +0000 | [diff] [blame] | 2 | #include "SkFloatingPoint.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 3 | #include "SkPoint.h" |
| 4 | #include "SkRandom.h" |
| 5 | |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 6 | #if 0 |
| 7 | static U8CPU premul_fast(U8CPU a, U8CPU x) { |
| 8 | return a * x * 32897 >> 23; |
| 9 | } |
| 10 | |
| 11 | static U8CPU premul_trunc(U8CPU a, U8CPU x) { |
| 12 | double result = a * x; |
| 13 | result /= 255.0; |
| 14 | return (unsigned)floor(result + 0.0); |
| 15 | } |
| 16 | |
| 17 | static U8CPU premul_round(U8CPU a, U8CPU x) { |
| 18 | double result = a * x; |
| 19 | result /= 255.0; |
| 20 | return (unsigned)floor(result + 0.5); |
| 21 | } |
| 22 | |
| 23 | static void test_premul(skiatest::Reporter* reporter) { |
| 24 | for (int a = 0; a <= 255; a++) { |
| 25 | for (int x = 0; x <= 255; x++) { |
| 26 | unsigned curr_trunc = SkMulDiv255Trunc(a, x); |
| 27 | unsigned curr_round = SkMulDiv255Round(a, x); |
| 28 | unsigned fast = premul_fast(a, x); |
| 29 | unsigned slow_round = premul_round(a, x); |
| 30 | unsigned slow_trunc = premul_trunc(a, x); |
| 31 | if (fast != slow || curr != fast) { |
| 32 | SkDebugf("---- premul(%d %d) curr=%d fast=%d slow=%d\n", a, x, |
| 33 | curr, fast, slow); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | #endif |
| 39 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 40 | #if defined(SkLONGLONG) |
| 41 | static int symmetric_fixmul(int a, int b) { |
| 42 | int sa = SkExtractSign(a); |
| 43 | int sb = SkExtractSign(b); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 44 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 45 | a = SkApplySign(a, sa); |
| 46 | b = SkApplySign(b, sb); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 47 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 48 | #if 1 |
| 49 | int c = (int)(((SkLONGLONG)a * b) >> 16); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 50 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 51 | return SkApplySign(c, sa ^ sb); |
| 52 | #else |
| 53 | SkLONGLONG ab = (SkLONGLONG)a * b; |
| 54 | if (sa ^ sb) { |
| 55 | ab = -ab; |
| 56 | } |
| 57 | return ab >> 16; |
| 58 | #endif |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | static void check_length(skiatest::Reporter* reporter, |
| 63 | const SkPoint& p, SkScalar targetLen) { |
| 64 | #ifdef SK_CAN_USE_FLOAT |
| 65 | float x = SkScalarToFloat(p.fX); |
| 66 | float y = SkScalarToFloat(p.fY); |
| 67 | float len = sk_float_sqrt(x*x + y*y); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 68 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 69 | len /= SkScalarToFloat(targetLen); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 70 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, len > 0.999f && len < 1.001f); |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | #if defined(SK_CAN_USE_FLOAT) |
| 76 | |
| 77 | static float nextFloat(SkRandom& rand) { |
| 78 | SkFloatIntUnion data; |
| 79 | data.fSignBitInt = rand.nextU(); |
| 80 | return data.fFloat; |
| 81 | } |
| 82 | |
| 83 | /* returns true if a == b as resulting from (int)x. Since it is undefined |
| 84 | what to do if the float exceeds 2^32-1, we check for that explicitly. |
| 85 | */ |
| 86 | static bool equal_float_native_skia(float x, uint32_t ni, uint32_t si) { |
| 87 | if (!(x == x)) { // NAN |
| 88 | return si == SK_MaxS32 || si == SK_MinS32; |
| 89 | } |
| 90 | // for out of range, C is undefined, but skia always should return NaN32 |
| 91 | if (x > SK_MaxS32) { |
| 92 | return si == SK_MaxS32; |
| 93 | } |
| 94 | if (x < -SK_MaxS32) { |
| 95 | return si == SK_MinS32; |
| 96 | } |
| 97 | return si == ni; |
| 98 | } |
| 99 | |
| 100 | static void assert_float_equal(skiatest::Reporter* reporter, const char op[], |
| 101 | float x, uint32_t ni, uint32_t si) { |
| 102 | if (!equal_float_native_skia(x, ni, si)) { |
| 103 | SkString desc; |
| 104 | desc.printf("%s float %g bits %x native %x skia %x\n", op, x, ni, si); |
| 105 | reporter->reportFailed(desc); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void test_float_cast(skiatest::Reporter* reporter, float x) { |
| 110 | int ix = (int)x; |
| 111 | int iix = SkFloatToIntCast(x); |
| 112 | assert_float_equal(reporter, "cast", x, ix, iix); |
| 113 | } |
| 114 | |
| 115 | static void test_float_floor(skiatest::Reporter* reporter, float x) { |
| 116 | int ix = (int)floor(x); |
| 117 | int iix = SkFloatToIntFloor(x); |
| 118 | assert_float_equal(reporter, "floor", x, ix, iix); |
| 119 | } |
| 120 | |
| 121 | static void test_float_round(skiatest::Reporter* reporter, float x) { |
| 122 | double xx = x + 0.5; // need intermediate double to avoid temp loss |
| 123 | int ix = (int)floor(xx); |
| 124 | int iix = SkFloatToIntRound(x); |
| 125 | assert_float_equal(reporter, "round", x, ix, iix); |
| 126 | } |
| 127 | |
| 128 | static void test_float_ceil(skiatest::Reporter* reporter, float x) { |
| 129 | int ix = (int)ceil(x); |
| 130 | int iix = SkFloatToIntCeil(x); |
| 131 | assert_float_equal(reporter, "ceil", x, ix, iix); |
| 132 | } |
| 133 | |
| 134 | static void test_float_conversions(skiatest::Reporter* reporter, float x) { |
| 135 | test_float_cast(reporter, x); |
| 136 | test_float_floor(reporter, x); |
| 137 | test_float_round(reporter, x); |
| 138 | test_float_ceil(reporter, x); |
| 139 | } |
| 140 | |
| 141 | static void test_int2float(skiatest::Reporter* reporter, int ival) { |
| 142 | float x0 = (float)ival; |
| 143 | float x1 = SkIntToFloatCast(ival); |
| 144 | float x2 = SkIntToFloatCast_NoOverflowCheck(ival); |
| 145 | REPORTER_ASSERT(reporter, x0 == x1); |
| 146 | REPORTER_ASSERT(reporter, x0 == x2); |
| 147 | } |
| 148 | |
| 149 | static void unittest_fastfloat(skiatest::Reporter* reporter) { |
| 150 | SkRandom rand; |
| 151 | size_t i; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 152 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 153 | static const float gFloats[] = { |
| 154 | 0.f, 1.f, 0.5f, 0.499999f, 0.5000001f, 1.f/3, |
| 155 | 0.000000001f, 1000000000.f, // doesn't overflow |
| 156 | 0.0000000001f, 10000000000.f // does overflow |
| 157 | }; |
| 158 | for (i = 0; i < SK_ARRAY_COUNT(gFloats); i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 159 | test_float_conversions(reporter, gFloats[i]); |
| 160 | test_float_conversions(reporter, -gFloats[i]); |
| 161 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 162 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 163 | for (int outer = 0; outer < 100; outer++) { |
| 164 | rand.setSeed(outer); |
| 165 | for (i = 0; i < 100000; i++) { |
| 166 | float x = nextFloat(rand); |
| 167 | test_float_conversions(reporter, x); |
| 168 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 169 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 170 | test_int2float(reporter, 0); |
| 171 | test_int2float(reporter, 1); |
| 172 | test_int2float(reporter, -1); |
| 173 | for (i = 0; i < 100000; i++) { |
| 174 | // for now only test ints that are 24bits or less, since we don't |
| 175 | // round (down) large ints the same as IEEE... |
| 176 | int ival = rand.nextU() & 0xFFFFFF; |
| 177 | test_int2float(reporter, ival); |
| 178 | test_int2float(reporter, -ival); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 183 | #ifdef SK_SCALAR_IS_FLOAT |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 184 | static float make_zero() { |
| 185 | return sk_float_sin(0); |
| 186 | } |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 187 | #endif |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 188 | |
| 189 | static void unittest_isfinite(skiatest::Reporter* reporter) { |
| 190 | #ifdef SK_SCALAR_IS_FLOAT |
epoger@google.com | bf083a9 | 2011-06-08 18:26:08 +0000 | [diff] [blame] | 191 | float nan = sk_float_asin(2); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 192 | float inf = 1.0 / make_zero(); |
| 193 | float big = 3.40282e+038; |
| 194 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(inf)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-inf)); |
| 197 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(inf)); |
| 198 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(-inf)); |
| 199 | #else |
| 200 | SkFixed nan = SK_FixedNaN; |
| 201 | SkFixed big = SK_FixedMax; |
| 202 | #endif |
| 203 | |
| 204 | REPORTER_ASSERT(reporter, SkScalarIsNaN(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(big)); |
| 206 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(-big)); |
| 207 | REPORTER_ASSERT(reporter, !SkScalarIsNaN(0)); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 208 | |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 209 | REPORTER_ASSERT(reporter, !SkScalarIsFinite(nan)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, SkScalarIsFinite(big)); |
| 211 | REPORTER_ASSERT(reporter, SkScalarIsFinite(-big)); |
| 212 | REPORTER_ASSERT(reporter, SkScalarIsFinite(0)); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 213 | } |
| 214 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 215 | #endif |
| 216 | |
| 217 | static void test_muldiv255(skiatest::Reporter* reporter) { |
| 218 | #ifdef SK_CAN_USE_FLOAT |
| 219 | for (int a = 0; a <= 255; a++) { |
| 220 | for (int b = 0; b <= 255; b++) { |
| 221 | int ab = a * b; |
| 222 | float s = ab / 255.0f; |
| 223 | int round = (int)floorf(s + 0.5f); |
| 224 | int trunc = (int)floorf(s); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 225 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 226 | int iround = SkMulDiv255Round(a, b); |
| 227 | int itrunc = SkMulDiv255Trunc(a, b); |
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 | REPORTER_ASSERT(reporter, iround == round); |
| 230 | REPORTER_ASSERT(reporter, itrunc == trunc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 231 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 232 | REPORTER_ASSERT(reporter, itrunc <= iround); |
| 233 | REPORTER_ASSERT(reporter, iround <= a); |
| 234 | REPORTER_ASSERT(reporter, iround <= b); |
| 235 | } |
| 236 | } |
| 237 | #endif |
| 238 | } |
| 239 | |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 240 | static void test_muldiv255ceiling(skiatest::Reporter* reporter) { |
| 241 | for (int c = 0; c <= 255; c++) { |
| 242 | for (int a = 0; a <= 255; a++) { |
| 243 | int product = (c * a + 255); |
| 244 | int expected_ceiling = (product + (product >> 8)) >> 8; |
| 245 | int webkit_ceiling = (c * a + 254) / 255; |
| 246 | REPORTER_ASSERT(reporter, expected_ceiling == webkit_ceiling); |
| 247 | int skia_ceiling = SkMulDiv255Ceiling(c, a); |
| 248 | REPORTER_ASSERT(reporter, skia_ceiling == webkit_ceiling); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 253 | static void test_copysign(skiatest::Reporter* reporter) { |
| 254 | static const int32_t gTriples[] = { |
| 255 | // x, y, expected result |
| 256 | 0, 0, 0, |
| 257 | 0, 1, 0, |
| 258 | 0, -1, 0, |
| 259 | 1, 0, 1, |
| 260 | 1, 1, 1, |
| 261 | 1, -1, -1, |
| 262 | -1, 0, 1, |
| 263 | -1, 1, 1, |
| 264 | -1, -1, -1, |
| 265 | }; |
| 266 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTriples); i += 3) { |
| 267 | REPORTER_ASSERT(reporter, |
| 268 | SkCopySign32(gTriples[i], gTriples[i+1]) == gTriples[i+2]); |
| 269 | #ifdef SK_CAN_USE_FLOAT |
| 270 | float x = (float)gTriples[i]; |
| 271 | float y = (float)gTriples[i+1]; |
| 272 | float expected = (float)gTriples[i+2]; |
| 273 | REPORTER_ASSERT(reporter, sk_float_copysign(x, y) == expected); |
| 274 | #endif |
| 275 | } |
| 276 | |
| 277 | SkRandom rand; |
| 278 | for (int j = 0; j < 1000; j++) { |
| 279 | int ix = rand.nextS(); |
| 280 | REPORTER_ASSERT(reporter, SkCopySign32(ix, ix) == ix); |
| 281 | REPORTER_ASSERT(reporter, SkCopySign32(ix, -ix) == -ix); |
| 282 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, ix) == ix); |
| 283 | REPORTER_ASSERT(reporter, SkCopySign32(-ix, -ix) == -ix); |
| 284 | |
| 285 | SkScalar sx = rand.nextSScalar1(); |
| 286 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, sx) == sx); |
| 287 | REPORTER_ASSERT(reporter, SkScalarCopySign(sx, -sx) == -sx); |
| 288 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, sx) == sx); |
| 289 | REPORTER_ASSERT(reporter, SkScalarCopySign(-sx, -sx) == -sx); |
| 290 | } |
| 291 | } |
| 292 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 293 | static void TestMath(skiatest::Reporter* reporter) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 294 | int i; |
| 295 | int32_t x; |
| 296 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 297 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 298 | // these should assert |
| 299 | #if 0 |
| 300 | SkToS8(128); |
| 301 | SkToS8(-129); |
| 302 | SkToU8(256); |
| 303 | SkToU8(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 304 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 305 | SkToS16(32768); |
| 306 | SkToS16(-32769); |
| 307 | SkToU16(65536); |
| 308 | SkToU16(-5); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 309 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 310 | if (sizeof(size_t) > 4) { |
| 311 | SkToS32(4*1024*1024); |
| 312 | SkToS32(-4*1024*1024); |
| 313 | SkToU32(5*1024*1024); |
| 314 | SkToU32(-5); |
| 315 | } |
| 316 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 317 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 318 | test_muldiv255(reporter); |
senorblanco@chromium.org | ec7a30c | 2010-12-07 21:07:56 +0000 | [diff] [blame] | 319 | test_muldiv255ceiling(reporter); |
reed@android.com | f0ad086 | 2010-02-09 19:18:38 +0000 | [diff] [blame] | 320 | test_copysign(reporter); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 321 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 322 | { |
| 323 | SkScalar x = SK_ScalarNaN; |
| 324 | REPORTER_ASSERT(reporter, SkScalarIsNaN(x)); |
| 325 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 326 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 327 | for (i = 1; i <= 10; i++) { |
| 328 | x = SkCubeRootBits(i*i*i, 11); |
| 329 | REPORTER_ASSERT(reporter, x == i); |
| 330 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 331 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 332 | x = SkFixedSqrt(SK_Fixed1); |
| 333 | REPORTER_ASSERT(reporter, x == SK_Fixed1); |
| 334 | x = SkFixedSqrt(SK_Fixed1/4); |
| 335 | REPORTER_ASSERT(reporter, x == SK_Fixed1/2); |
| 336 | x = SkFixedSqrt(SK_Fixed1*4); |
| 337 | REPORTER_ASSERT(reporter, x == SK_Fixed1*2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 338 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 339 | x = SkFractSqrt(SK_Fract1); |
| 340 | REPORTER_ASSERT(reporter, x == SK_Fract1); |
| 341 | x = SkFractSqrt(SK_Fract1/4); |
| 342 | REPORTER_ASSERT(reporter, x == SK_Fract1/2); |
| 343 | x = SkFractSqrt(SK_Fract1/16); |
| 344 | REPORTER_ASSERT(reporter, x == SK_Fract1/4); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 345 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 346 | for (i = 1; i < 100; i++) { |
| 347 | x = SkFixedSqrt(SK_Fixed1 * i * i); |
| 348 | REPORTER_ASSERT(reporter, x == SK_Fixed1 * i); |
| 349 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 350 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 351 | for (i = 0; i < 1000; i++) { |
| 352 | int value = rand.nextS16(); |
| 353 | int max = rand.nextU16(); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 354 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 355 | int clamp = SkClampMax(value, max); |
| 356 | int clamp2 = value < 0 ? 0 : (value > max ? max : value); |
| 357 | REPORTER_ASSERT(reporter, clamp == clamp2); |
| 358 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 359 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 360 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 361 | SkPoint p; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 362 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 363 | p.setLength(rand.nextS(), rand.nextS(), SK_Scalar1); |
| 364 | check_length(reporter, p, SK_Scalar1); |
| 365 | p.setLength(rand.nextS() >> 13, rand.nextS() >> 13, SK_Scalar1); |
| 366 | check_length(reporter, p, SK_Scalar1); |
| 367 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 368 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 369 | { |
| 370 | SkFixed result = SkFixedDiv(100, 100); |
| 371 | REPORTER_ASSERT(reporter, result == SK_Fixed1); |
| 372 | result = SkFixedDiv(1, SK_Fixed1); |
| 373 | REPORTER_ASSERT(reporter, result == 1); |
| 374 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 375 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 376 | #ifdef SK_CAN_USE_FLOAT |
| 377 | unittest_fastfloat(reporter); |
reed@google.com | 077910e | 2011-02-08 21:56:39 +0000 | [diff] [blame] | 378 | unittest_isfinite(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 379 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 380 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 381 | #ifdef SkLONGLONG |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 382 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 383 | SkFixed numer = rand.nextS(); |
| 384 | SkFixed denom = rand.nextS(); |
| 385 | SkFixed result = SkFixedDiv(numer, denom); |
| 386 | SkLONGLONG check = ((SkLONGLONG)numer << 16) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 387 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 388 | (void)SkCLZ(numer); |
| 389 | (void)SkCLZ(denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 390 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 391 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 392 | if (check > SK_MaxS32) { |
| 393 | check = SK_MaxS32; |
| 394 | } else if (check < -SK_MaxS32) { |
| 395 | check = SK_MinS32; |
| 396 | } |
| 397 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 398 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 399 | result = SkFractDiv(numer, denom); |
| 400 | check = ((SkLONGLONG)numer << 30) / denom; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 401 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, result != (SkFixed)SK_NaN32); |
| 403 | if (check > SK_MaxS32) { |
| 404 | check = SK_MaxS32; |
| 405 | } else if (check < -SK_MaxS32) { |
| 406 | check = SK_MinS32; |
| 407 | } |
| 408 | REPORTER_ASSERT(reporter, result == (int32_t)check); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 409 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 410 | // make them <= 2^24, so we don't overflow in fixmul |
| 411 | numer = numer << 8 >> 8; |
| 412 | denom = denom << 8 >> 8; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 413 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 414 | result = SkFixedMul(numer, denom); |
| 415 | SkFixed r2 = symmetric_fixmul(numer, denom); |
| 416 | // SkASSERT(result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 417 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 418 | result = SkFixedMul(numer, numer); |
| 419 | r2 = SkFixedSquare(numer); |
| 420 | REPORTER_ASSERT(reporter, result == r2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 421 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 422 | #ifdef SK_CAN_USE_FLOAT |
| 423 | if (numer >= 0 && denom >= 0) { |
| 424 | SkFixed mean = SkFixedMean(numer, denom); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 425 | float prod = SkFixedToFloat(numer) * SkFixedToFloat(denom); |
| 426 | float fm = sk_float_sqrt(sk_float_abs(prod)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 427 | SkFixed mean2 = SkFloatToFixed(fm); |
| 428 | int diff = SkAbs32(mean - mean2); |
| 429 | REPORTER_ASSERT(reporter, diff <= 1); |
| 430 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 431 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 432 | { |
| 433 | SkFixed mod = SkFixedMod(numer, denom); |
| 434 | float n = SkFixedToFloat(numer); |
| 435 | float d = SkFixedToFloat(denom); |
| 436 | float m = sk_float_mod(n, d); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 437 | // ensure the same sign |
| 438 | REPORTER_ASSERT(reporter, mod == 0 || (mod < 0) == (m < 0)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 439 | int diff = SkAbs32(mod - SkFloatToFixed(m)); |
| 440 | REPORTER_ASSERT(reporter, (diff >> 7) == 0); |
| 441 | } |
| 442 | #endif |
| 443 | } |
| 444 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 445 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 446 | #ifdef SK_CAN_USE_FLOAT |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 447 | for (i = 0; i < 10000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 448 | SkFract x = rand.nextU() >> 1; |
| 449 | double xx = (double)x / SK_Fract1; |
| 450 | SkFract xr = SkFractSqrt(x); |
| 451 | SkFract check = SkFloatToFract(sqrt(xx)); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 452 | REPORTER_ASSERT(reporter, xr == check || |
| 453 | xr == check-1 || |
| 454 | xr == check+1); |
| 455 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 456 | xr = SkFixedSqrt(x); |
| 457 | xx = (double)x / SK_Fixed1; |
| 458 | check = SkFloatToFixed(sqrt(xx)); |
| 459 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 460 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 461 | xr = SkSqrt32(x); |
| 462 | xx = (double)x; |
| 463 | check = (int32_t)sqrt(xx); |
| 464 | REPORTER_ASSERT(reporter, xr == check || xr == check-1); |
| 465 | } |
| 466 | #endif |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 467 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 468 | #if !defined(SK_SCALAR_IS_FLOAT) && defined(SK_CAN_USE_FLOAT) |
| 469 | { |
| 470 | SkFixed s, c; |
| 471 | s = SkFixedSinCos(0, &c); |
| 472 | REPORTER_ASSERT(reporter, s == 0); |
| 473 | REPORTER_ASSERT(reporter, c == SK_Fixed1); |
| 474 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 475 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 476 | int maxDiff = 0; |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 477 | for (i = 0; i < 1000; i++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 478 | SkFixed rads = rand.nextS() >> 10; |
| 479 | double frads = SkFixedToFloat(rads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 480 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 481 | SkFixed s, c; |
| 482 | s = SkScalarSinCos(rads, &c); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 483 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 484 | double fs = sin(frads); |
| 485 | double fc = cos(frads); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 486 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 487 | SkFixed is = SkFloatToFixed(fs); |
| 488 | SkFixed ic = SkFloatToFixed(fc); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 489 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 490 | maxDiff = SkMax32(maxDiff, SkAbs32(is - s)); |
| 491 | maxDiff = SkMax32(maxDiff, SkAbs32(ic - c)); |
| 492 | } |
| 493 | SkDebugf("SinCos: maximum error = %d\n", maxDiff); |
| 494 | #endif |
reed@google.com | 772813a | 2011-03-30 22:34:45 +0000 | [diff] [blame] | 495 | |
| 496 | // test_premul(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 497 | } |
| 498 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 499 | #include "TestClassDef.h" |
| 500 | DEFINE_TESTCLASS("Math", MathTestClass, TestMath) |