1 | /* $Id: fmaf.cpp 96108 2022-08-08 11:16:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - No-CRT - fmaf().
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define IPRT_NO_CRT_FOR_3RD_PARTY
|
---|
32 | #include "internal/nocrt.h"
|
---|
33 | #include <iprt/nocrt/math.h>
|
---|
34 | #include <iprt/assertcompile.h>
|
---|
35 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
36 | # include <iprt/asm-amd64-x86.h>
|
---|
37 | # include <iprt/x86.h>
|
---|
38 | #endif
|
---|
39 | #include <softfloat.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * External Symbols *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | DECLASM(float) rtNoCrtMathFma3f(float r32Factor1, float r32Factor2, float r32Addend);
|
---|
46 | DECLASM(float) rtNoCrtMathFma4f(float r32Factor1, float r32Factor2, float r32Addend);
|
---|
47 |
|
---|
48 |
|
---|
49 | #undef fmaf
|
---|
50 | float RT_NOCRT(fmaf)(float r32Factor1, float r32Factor2, float r32Addend)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * We prefer using native FMA instructions when available.
|
---|
54 | */
|
---|
55 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
56 | typedef enum { kCpuDetect = 0, kCpuWithFma3, kCpuWithFma4, kCpuWithoutFma } CPUFMASUPPORT;
|
---|
57 | static CPUFMASUPPORT volatile s_enmSup = kCpuDetect;
|
---|
58 | CPUFMASUPPORT enmSup = s_enmSup;
|
---|
59 | if (enmSup != kCpuDetect)
|
---|
60 | { }
|
---|
61 | else
|
---|
62 | {
|
---|
63 | if (ASMCpuId_ECX(1) & X86_CPUID_FEATURE_ECX_FMA)
|
---|
64 | enmSup = kCpuWithFma3;
|
---|
65 | else if (ASMCpuId_ECX(UINT32_C(0x80000001)) & X86_CPUID_AMD_FEATURE_ECX_FMA4)
|
---|
66 | enmSup = kCpuWithFma4;
|
---|
67 | else
|
---|
68 | enmSup = kCpuWithoutFma;
|
---|
69 | s_enmSup = enmSup;
|
---|
70 | }
|
---|
71 | if (enmSup == kCpuWithFma3)
|
---|
72 | return rtNoCrtMathFma3f(r32Factor1, r32Factor2, r32Addend);
|
---|
73 | if (enmSup == kCpuWithFma4)
|
---|
74 | return rtNoCrtMathFma4f(r32Factor1, r32Factor2, r32Addend);
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Fall back on SoftFloat.
|
---|
79 | */
|
---|
80 | /** @todo couldn't we just use double as a fallback here? */
|
---|
81 | AssertCompile(sizeof(r32Factor1) == sizeof(RTFLOAT32U));
|
---|
82 | softfloat_state_t State = SOFTFLOAT_STATE_INIT_DEFAULTS(); /** @todo init from MXCSR/FCW */
|
---|
83 | union { RTFLOAT32U Iprt; float32_t SoftFloat; } uFactor1, uFactor2, uAddend, uResult;
|
---|
84 | uFactor1.Iprt.r = r32Factor1;
|
---|
85 | uFactor2.Iprt.r = r32Factor2;
|
---|
86 | uAddend.Iprt.r = r32Addend;
|
---|
87 | uResult.SoftFloat = f32_mulAdd(uFactor1.SoftFloat, uFactor2.SoftFloat, uAddend.SoftFloat, &State);
|
---|
88 | return uResult.Iprt.r;
|
---|
89 | }
|
---|
90 | RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(fmaf);
|
---|
91 |
|
---|