|
| 1 | +// Suppress rounding mode information messages that are expected during constant generation |
| 2 | +suppressmessage(185, 186); // suppress expected info round-up, round-down |
| 3 | + |
| 4 | +// Helper procedure to format constants array for C++ output |
| 5 | +// Takes a type descriptor followed by constant values and formats them |
| 6 | +// as a C array with 4 elements per line |
| 7 | +procedure KArray_(pArgs = ...) { |
| 8 | + var pT; |
| 9 | + pT = head(pArgs); |
| 10 | + return CArrayT(pT, Constants @ tail(pArgs), 4) @ ";"; |
| 11 | +}; |
| 12 | + |
| 13 | +// Generate C++ header with various π-related constants for Cody-Waite reduction |
| 14 | +// These constants enable accurate computation of r = x - n*π with extended precision |
| 15 | +// |
| 16 | +// The Cody-Waite method splits π into multiple parts: |
| 17 | +// r = x - n*π₁ - n*π₂ - n*π₃ ... |
| 18 | +// where each πᵢ has limited precision to ensure exact multiplication |
| 19 | +// |
| 20 | +// Different versions are provided for: |
| 21 | +// - FMA (Fused Multiply-Add) vs non-FMA architectures |
| 22 | +// - float vs double precision |
| 23 | +// - Various precision requirements |
| 24 | +Append( |
| 25 | + // Generic template declaration |
| 26 | + "template <typename T, bool FMA> inline constexpr char kPi[] = {};", |
| 27 | + |
| 28 | + // Float π constants for Low precision implementation |
| 29 | + "template <> inline constexpr float kPi<float, true>[] = " @ |
| 30 | + KArray_(Float32, pi, [|RN, 24, 24, 24|]), // FMA: 3x24-bit pieces (full precision each) |
| 31 | + |
| 32 | + "template <> inline constexpr float kPi<float, false>[] = " @ |
| 33 | + KArray_(Float32, pi, [|RD, 11, 11, 11|], [|RN, 24|]), // no FMA: 3x11-bit + 1x24-bit |
| 34 | + // The 11-bit pieces ensure n*πᵢ is exact (no rounding) for |n| < 2^13 |
| 35 | + |
| 36 | + // Double π constants for Low precision implementation |
| 37 | + "template <> inline constexpr double kPi<double, true>[] = " @ |
| 38 | + KArray_(Float64, pi, [|RN, 53|], [|RD, 53|], [|RU, 53|]), // FMA: Different roundings for error compensation |
| 39 | + |
| 40 | + "template <> inline constexpr double kPi<double, false>[] = " @ |
| 41 | + KArray_(Float64, pi, [|RN, 24, 24, 24|], [|RN, 53|]), // no FMA: 3x24-bit + 1x53-bit |
| 42 | + // The 24-bit pieces ensure n*πᵢ is exact for |n| < 2^29 |
| 43 | + "", |
| 44 | + |
| 45 | + // Special 35-bit precision π for specific algorithms |
| 46 | + "template <bool FMA> inline constexpr double kPiPrec35[] = " @ |
| 47 | + KArray_(Float64, pi, [|RN, 35|], [|RD, 53|]), |
| 48 | + |
| 49 | + "template <> inline constexpr double kPiPrec35<false>[] = " @ |
| 50 | + KArray_(Float64, pi, [|RN, 24, 24, 24|]), |
| 51 | + "", |
| 52 | + |
| 53 | + // 2π constants for angle wrapping |
| 54 | + "template <typename T> inline constexpr char kPiMul2[] = {};", |
| 55 | + "template <> inline constexpr float kPiMul2<float>[] = " @ |
| 56 | + KArray_(Float32, pi*2, [|RN, 24, 24|]), // 2x24-bit pieces |
| 57 | + |
| 58 | + "template <> inline constexpr double kPiMul2<double>[] = " @ |
| 59 | + KArray_(Float64, pi*2, [|RN, 53, 53|]), // 2x53-bit pieces |
| 60 | + "" |
| 61 | +); |
| 62 | + |
| 63 | +// Non-FMA version of π/16 for High precision implementation |
| 64 | +// Special handling: components are reordered [0,2,3,1] for proper evaluation |
| 65 | +// Without FMA, multiplication order matters to minimize rounding errors |
| 66 | +vNFma = Constants(pi/16, [|RN, 27, 27|], [|RN, 29|], [|RN, 53|]); |
| 67 | +Append( |
| 68 | + "template <bool FMA> inline constexpr double kPiDiv16Prec29[] = " @ |
| 69 | + KArray_(Float64, pi/16, [|RN, 53|], [|RN, 29|], [|RN, 53|]), |
| 70 | + |
| 71 | + // Non-FMA version reorders components: [0], [2], [3], [1] |
| 72 | + // This ordering ensures proper evaluation without FMA: |
| 73 | + // r = x - n*π₁/16 - n*π₃/16 - n*π₄/16 - n*π₂/16 |
| 74 | + "template <> inline constexpr double kPiDiv16Prec29<false>[] = " @ |
| 75 | + CArray([|vNFma[0], vNFma[2], vNFma[3], vNFma[1]|], 4) @ ";", |
| 76 | + "", |
| 77 | + |
| 78 | + // Simple scalar constants |
| 79 | + "template <typename T> inline constexpr char kInvPi = '_';", |
| 80 | + "template <> inline constexpr float kInvPi<float> = " @ single(1/pi) @ "f;", |
| 81 | + "template <> inline constexpr double kInvPi<double> = " @ double(1/pi) @ ";", |
| 82 | + "", |
| 83 | + |
| 84 | + "template <typename T> inline constexpr char kHalfPi = '_';", |
| 85 | + "template <> inline constexpr float kHalfPi<float> = " @ single(pi/2) @ "f;", |
| 86 | + "template <> inline constexpr double kHalfPi<double> = " @ double(pi/2) @ ";", |
| 87 | + "", |
| 88 | + |
| 89 | + "template <typename T> inline constexpr char k16DivPi = '_';", |
| 90 | + "template <> inline constexpr float k16DivPi<float> = " @ single(16/pi) @ "f;", |
| 91 | + "template <> inline constexpr double k16DivPi<double> = " @ double(16/pi) @ ";", |
| 92 | + "" |
| 93 | +); |
| 94 | +// Dump(); |
| 95 | + |
| 96 | +WriteCPPHeader("npsr::trig::data"); |
| 97 | + |
0 commit comments