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