VirtualBox

source: vbox/trunk/src/VBox/VMM/tools/IEMGenFpuConstants.c@ 94617

Last change on this file since 94617 was 94538, checked in by vboxsync, 3 years ago

VMM/IEM: Implemented f2xm1. bugref:9898

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: IEMGenFpuConstants.c 94538 2022-04-10 14:16:03Z vboxsync $ */
2/** @file
3 * IEMGenFpuConstants - Generates FPU constants for IEMAllAImplC.cpp.
4 *
5 * Compile on linux: gcc -I../../../../include -DIN_RING3 IEMGenFpuConstants.c -lmpfr -g -o IEMGenFpuConstants
6 */
7
8/*
9 * Copyright (C) 2022 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <iprt/types.h>
25#include <iprt/assertcompile.h>
26#include <stdio.h>
27#define MPFR_WANT_FLOAT128
28#include <gmp.h>
29#include <mpfr.h>
30
31
32void PrintComment(const char *pszComment, va_list va, mpfr_srcptr pVal, bool fList)
33{
34 const char * const pszIndent = fList ? " " : "";
35 printf(fList ? " /* " : "/** ");
36 vprintf(pszComment, va);
37 printf("\n%s * base-10: ", pszIndent);
38 mpfr_out_str(stdout, 10, 0, pVal, MPFR_RNDD);
39 printf("\n%s * base-16: ", pszIndent);
40 mpfr_out_str(stdout, 16, 0, pVal, MPFR_RNDD);
41 printf("\n%s * base-2 : ", pszIndent);
42 mpfr_out_str(stdout, 2, 0, pVal, MPFR_RNDD);
43 printf(" */\n");
44}
45
46
47uint64_t BinStrToU64(const char *psz, size_t cch)
48{
49 uint64_t u = 0;
50 while (cch-- > 0)
51 {
52 u <<= 1;
53 u |= *psz++ == '1';
54 }
55 return u;
56}
57
58
59void PrintU128(mpfr_srcptr pVal, const char *pszVariable, const char *pszComment, ...)
60{
61 va_list va;
62 va_start(va, pszComment);
63 PrintComment(pszComment, va, pVal, !pszVariable);
64 va_end(va);
65 if (pszVariable)
66 printf("const RTUINT128U %s = ", pszVariable);
67 else
68 printf(" ");
69 mpfr_exp_t iExpBinary;
70 char *pszBinary = mpfr_get_str(NULL, &iExpBinary, 2, 0, pVal, MPFR_RNDD);
71 printf("RTUINT128_INIT_C(%#llx, %#llx)%s\n",
72 BinStrToU64(pszBinary, 64), BinStrToU64(&pszBinary[64], 64), pszVariable ? ";" : ",");
73 mpfr_free_str(pszBinary);
74}
75
76
77void PrintF128(mpfr_srcptr pVal, const char *pszVariable, const char *pszComment, ...)
78{
79 RTFLOAT128U r128;
80 *(_Float128 *)&r128 = mpfr_get_float128(pVal, MPFR_RNDD);
81
82 va_list va;
83 va_start(va, pszComment);
84 PrintComment(pszComment, va, pVal, !pszVariable);
85 va_end(va);
86 if (pszVariable)
87 printf("const RTFLOAT128U %s = ", pszVariable);
88 else
89 printf(" ");
90 printf("RTFLOAT128U_INIT_C(%d, 0x%012llx, 0x%016llx, 0x%04x)%s\n",
91 r128.s.fSign, r128.s64.uFractionHi, r128.s64.uFractionLo, r128.s64.uExponent, pszVariable ? ";" : ",");
92}
93
94
95int main(void)
96{
97 mpfr_t Val;
98
99 mpfr_init2(Val, 112 + 1);
100 mpfr_const_log2(Val, MPFR_RNDN);
101 PrintF128(Val, "g_r128Ln2", "The ln2 constant as 128-bit floating point value.");
102
103 mpfr_init2(Val, 128);
104 mpfr_const_log2(Val, MPFR_RNDN);
105 PrintU128(Val, "g_u128Ln2Mantissa", "High precision ln2 value.");
106
107 mpfr_t Val2;
108 mpfr_init2(Val2, 67);
109 mpfr_const_log2(Val2, MPFR_RNDN);
110 mpfr_set(Val, Val2, MPFR_RNDN);
111 PrintU128(Val, "g_u128Ln2MantissaIntel", "High precision ln2 value, compatible with f2xm1 results on intel 10980XE.");
112
113 /** @todo emit constants with 68-bit precision (1+67 bits), as that's what we
114 * use for intel now. */
115 printf("\n"
116 "/** Horner constants for f2xm1 */\n"
117 "const RTFLOAT128U g_ar128F2xm1HornerConsts[] =\n"
118 "{\n");
119 mpfr_t One;
120 mpfr_init2(One, 112 + 1);
121 mpfr_set_ui(One, 1, MPFR_RNDD);
122 PrintF128(One, NULL, "a0");
123
124 mpfr_init2(Val, 112 + 1);
125 unsigned long uFactorial = 1; AssertCompile(sizeof(uFactorial) >= 8);
126 for (unsigned a = 1; a < 22; a++)
127 {
128 uFactorial *= (a + 1);
129 mpfr_div_ui(Val, One, uFactorial, MPFR_RNDD);
130 PrintF128(Val, NULL, "a%u", a);
131 }
132
133 printf("};\n");
134
135 mpfr_clear(Val);
136 mpfr_clear(Val2);
137 mpfr_clear(One);
138 mpfr_free_cache();
139 return 0;
140}
141
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette