|
| 1 | +// Suppress rounding mode information messages for irrational table values |
| 2 | +suppressmessage(184, 185, 186); // suppress info no rounding, round-up, round-down |
| 3 | +suppressmessage(160); // inequality decided by faithful evaluation |
| 4 | + |
| 5 | +// Generates a 4-element lookup table for fast trigonometric function approximation. |
| 6 | +// |
| 7 | +// The table format per angle is: [deriv, sigma, high, low] |
| 8 | +// where: |
| 9 | +// sigma + deriv = f'(angle) sigma = sign * 2^k nearest the derivative; |
| 10 | +// being a power of two, sigma*z is exact for any |
| 11 | +// small remainder z, confining the interpolation |
| 12 | +// rounding error to the tiny deriv*z term |
| 13 | +// high + low = f(angle) double-float split of the function value |
| 14 | +// |
| 15 | +// The values reproduce Intel SVML's AVX-512 high-accuracy sin breakpoint |
| 16 | +// tables byte-for-byte (SVML packs the fields in a different, per-precision |
| 17 | +// order; this table keeps one order for both precisions). Two conventions |
| 18 | +// are per-precision, pinned down by byte comparison against SVML: |
| 19 | +// - low rounding: float RN full precision / double RZ 24-bit |
| 20 | +// - sigma cut: bump 2^k -> 2^(k+1) when |derivative| > cut * 2^k, |
| 21 | +// cut = sqrt(2) for float / 1.5 for double |
| 22 | +// |
| 23 | +// Two structural tricks keep every rounding and comparison decidable |
| 24 | +// (Sollya brackets irrational values but can never prove one is exactly |
| 25 | +// zero, nor decide a comparison whose sides are exactly equal): |
| 26 | +// - Quadrant reduction: evaluate sin/cos only in [0, pi/2) and select by |
| 27 | +// quadrant. Where f or f' is exactly 0/+-1 (angle a multiple of pi/2) |
| 28 | +// the reduced angle is the exact rational 0, so sin(0)/cos(0) evaluate |
| 29 | +// symbolically and those entries store clean +0.0. |
| 30 | +// - Cut nudge: scaling the float cut by (1 - 2^-200) resolves the exact |
| 31 | +// tie |derivative| = 2^(-1/2) upward like SVML does; no other entry on |
| 32 | +// either grid comes anywhere near a cut point. |
| 33 | +// |
| 34 | +// Parameters: |
| 35 | +// pT - Type descriptor with .kSize, .kDigits, .kRound |
| 36 | +// pQuad - Quadrant offset of the function to approximate: |
| 37 | +// 0 for sin (derivative cos), 1 for cos (derivative -sin), |
| 38 | +// using cos(x) = sin(x + pi/2) |
| 39 | +procedure ApproxLut4_(pT, pQuad) { |
| 40 | + var r, i, $; |
| 41 | + // Table size: 512 entries for 64-bit, 256 for 32-bit |
| 42 | + // These sizes balance table memory usage with interpolation accuracy: |
| 43 | + // - 256 entries = 1.4° spacing for float (sufficient for 24-bit mantissa) |
| 44 | + // - 512 entries = 0.7° spacing for double (needed for 53-bit mantissa) |
| 45 | + $.num_lut = match pT.kSize |
| 46 | + with 64: (2^9) |
| 47 | + default: (2^8); |
| 48 | + |
| 49 | + // Low part rounding: full precision RN for float, truncated 24-bit for double |
| 50 | + $.low_round = match pT.kSize |
| 51 | + with 64: ([|24, RZ|]) |
| 52 | + default: ([|pT.kDigits, RN|]); |
| 53 | + |
| 54 | + // Sigma cut: geometric nearest power of two for float, linear for double, |
| 55 | + // nudged below the float tie point (see header) |
| 56 | + $.cut = match pT.kSize |
| 57 | + with 64: (3 / 2) |
| 58 | + default: (sqrt(2)); |
| 59 | + $.cut = $.cut * (1 - 2^(-200)); |
| 60 | + |
| 61 | + r = [||]; |
| 62 | + for i from 0 to $.num_lut - 1 do { |
| 63 | + // Quadrant reduction: angle = quad*pi/2 + reduced with reduced in [0, pi/2) |
| 64 | + $.quadrant = floor(4 * i / $.num_lut); |
| 65 | + $.reduced = 2 * pi * (i - $.quadrant * $.num_lut / 4) / $.num_lut; |
| 66 | + $.sin_red = sin($.reduced); |
| 67 | + $.cos_red = cos($.reduced); |
| 68 | + $.sin_by_quad = [| $.sin_red, $.cos_red, -$.sin_red, -$.cos_red |]; |
| 69 | + $.cos_by_quad = [| $.cos_red, -$.sin_red, -$.cos_red, $.sin_red |]; |
| 70 | + |
| 71 | + // Shift by the function's quadrant offset: |
| 72 | + // f(angle) = sin(angle + pQuad*pi/2), f'(angle) = cos(angle + pQuad*pi/2) |
| 73 | + $.quadrant = $.quadrant + pQuad; |
| 74 | + if ($.quadrant > 3) then $.quadrant = $.quadrant - 4; |
| 75 | + |
| 76 | + // Exact function value, split into high and low parts: |
| 77 | + // value ≈ high + low with high rounded to type precision |
| 78 | + $.exact = $.sin_by_quad[$.quadrant]; |
| 79 | + $.high = pT.kRound($.exact); |
| 80 | + $.low = pT.kRound(round($.exact - $.high, $.low_round[0], $.low_round[1])); |
| 81 | + |
| 82 | + // Exact derivative for interpolation |
| 83 | + $.deriv_exact = $.cos_by_quad[$.quadrant]; |
| 84 | + |
| 85 | + if ($.deriv_exact == 0) then { |
| 86 | + // Derivative term vanishes (angle at pi/2 or 3pi/2 relative to f) |
| 87 | + $.sigma = 0; |
| 88 | + $.deriv = 0; |
| 89 | + } else { |
| 90 | + // Power-of-2 scale factor nearest the derivative under the cut rule; |
| 91 | + // actual derivative = sigma + stored deriv |
| 92 | + $.k = floor(log2(abs($.deriv_exact))); |
| 93 | + $.sigma = 2.0^$.k; |
| 94 | + if (abs($.deriv_exact) > $.cut * $.sigma) then $.sigma = 2 * $.sigma; |
| 95 | + if ($.deriv_exact < 0) then $.sigma = -$.sigma; |
| 96 | + $.deriv = pT.kRound($.deriv_exact - $.sigma); |
| 97 | + }; |
| 98 | + |
| 99 | + r = r @ [|$.deriv, $.sigma, $.high, $.low|]; |
| 100 | + }; |
| 101 | + |
| 102 | + // Format as C array with 4 elements per table entry |
| 103 | + return CArrayT(pT, r, 4) @ ";"; |
| 104 | +}; |
| 105 | + |
| 106 | +// Generate C++ header content with specialized lookup tables |
| 107 | +// for both float and double precision sine and cosine |
| 108 | +// Template declarations (empty for unsupported types) |
| 109 | +Append( |
| 110 | + "template <typename T> inline constexpr char kSinApproxTable[] = {};", |
| 111 | + "template <> inline constexpr float kSinApproxTable<float>[] = ", |
| 112 | + ApproxLut4_(Float32, 0), // sin table with cos derivative |
| 113 | + "", |
| 114 | + "template <> inline constexpr double kSinApproxTable<double>[] = ", |
| 115 | + ApproxLut4_(Float64, 0), |
| 116 | + "", |
| 117 | + "template <typename T> inline constexpr char kCosApproxTable[] = {};", |
| 118 | + "template <> inline constexpr float kCosApproxTable<float>[] = ", |
| 119 | + ApproxLut4_(Float32, 1), // cos table with -sin derivative |
| 120 | + "", |
| 121 | + "template <> inline constexpr double kCosApproxTable<double>[] = ", |
| 122 | + ApproxLut4_(Float64, 1), |
| 123 | + "" |
| 124 | +); |
| 125 | + |
| 126 | +WriteCPPHeader("npsr::trig::data"); |
0 commit comments