1 1.1 mgorny /* $NetBSD: t_convert_xmm_s87.c,v 1.1 2020/10/15 17:44:44 mgorny Exp $ */ 2 1.1 mgorny 3 1.1 mgorny /*- 4 1.1 mgorny * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 1.1 mgorny * All rights reserved. 6 1.1 mgorny * 7 1.1 mgorny * This code is derived from software contributed to The NetBSD Foundation 8 1.1 mgorny * by Micha Grny for Moritz Systems Technology Company Sp. z o.o. 9 1.1 mgorny * 10 1.1 mgorny * Redistribution and use in source and binary forms, with or without 11 1.1 mgorny * modification, are permitted provided that the following conditions 12 1.1 mgorny * are met: 13 1.1 mgorny * 1. Redistributions of source code must retain the above copyright 14 1.1 mgorny * notice, this list of conditions and the following disclaimer. 15 1.1 mgorny * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 mgorny * notice, this list of conditions and the following disclaimer in the 17 1.1 mgorny * documentation and/or other materials provided with the distribution. 18 1.1 mgorny * 19 1.1 mgorny * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 mgorny * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 mgorny * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 mgorny * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 mgorny * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 mgorny * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 mgorny * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 mgorny * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 mgorny * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 mgorny * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 mgorny * POSSIBILITY OF SUCH DAMAGE. 30 1.1 mgorny */ 31 1.1 mgorny #include <sys/cdefs.h> 32 1.1 mgorny __RCSID("$NetBSD: t_convert_xmm_s87.c,v 1.1 2020/10/15 17:44:44 mgorny Exp $"); 33 1.1 mgorny 34 1.1 mgorny #include <sys/types.h> 35 1.1 mgorny #include <sys/param.h> 36 1.1 mgorny #include <sys/sysctl.h> 37 1.1 mgorny #include <x86/cpu.h> 38 1.1 mgorny #include <x86/cpu_extended_state.h> 39 1.1 mgorny 40 1.1 mgorny #include <stdint.h> 41 1.1 mgorny #include <string.h> 42 1.1 mgorny 43 1.1 mgorny #include <atf-c.h> 44 1.1 mgorny 45 1.1 mgorny #include "arch/x86/x86/convert_xmm_s87.c" 46 1.1 mgorny 47 1.1 mgorny static const struct fpaccfx ST_INPUTS[] = { 48 1.1 mgorny {{0x8000000000000000, 0x4000}}, /* +2.0 */ 49 1.1 mgorny {{0x3f00000000000000, 0x0000}}, /* 1.654785e-4932 */ 50 1.1 mgorny {{0x0000000000000000, 0x0000}}, /* +0 */ 51 1.1 mgorny {{0x0000000000000000, 0x8000}}, /* -0 */ 52 1.1 mgorny {{0x8000000000000000, 0x7fff}}, /* +inf */ 53 1.1 mgorny {{0x8000000000000000, 0xffff}}, /* -inf */ 54 1.1 mgorny {{0xc000000000000000, 0xffff}}, /* nan */ 55 1.1 mgorny {{0x8000000000000000, 0xc000}}, /* -2.0 */ 56 1.1 mgorny }; 57 1.1 mgorny __CTASSERT(sizeof(*ST_INPUTS) == 16); 58 1.1 mgorny 59 1.1 mgorny ATF_TC(fsave_fxsave_hw); 60 1.1 mgorny ATF_TC_HEAD(fsave_fxsave_hw, tc) 61 1.1 mgorny { 62 1.1 mgorny atf_tc_set_md_var(tc, "descr", 63 1.1 mgorny "converting between FSAVE/FXSAVE comparing to actual results"); 64 1.1 mgorny } 65 1.1 mgorny 66 1.1 mgorny ATF_TC_BODY(fsave_fxsave_hw, tc) 67 1.1 mgorny { 68 1.1 mgorny struct save87 fsave, fsave_conv; 69 1.1 mgorny struct fxsave fxsave, fxsave_conv; 70 1.1 mgorny int i, j; 71 1.1 mgorny 72 1.1 mgorny int mib[] = { CTL_MACHDEP, CPU_OSFXSR }; 73 1.1 mgorny int has_fxsave; 74 1.1 mgorny size_t has_fxsave_size = sizeof(has_fxsave); 75 1.1 mgorny if (sysctl(mib, __arraycount(mib), &has_fxsave, &has_fxsave_size, 76 1.1 mgorny NULL, 0) == -1 || !has_fxsave) 77 1.1 mgorny atf_tc_skip("FXSAVE not supported"); 78 1.1 mgorny 79 1.1 mgorny for (i = 1; i <= __arraycount(ST_INPUTS); i++) { 80 1.1 mgorny unsigned long unused1, unused2; 81 1.1 mgorny __asm__ __volatile__( 82 1.1 mgorny "finit\n" 83 1.1 mgorny ".loadfp:\n\t" 84 1.1 mgorny "fldt (%2)\n\t" 85 1.1 mgorny "add $0x10, %2\n\t" 86 1.1 mgorny "loop .loadfp\n\t" 87 1.1 mgorny "fxsave %5\n\t" 88 1.1 mgorny "fsave %4\n\t" 89 1.1 mgorny : "=b"(unused1), "=c"(unused2) 90 1.1 mgorny : "b"(ST_INPUTS), "c"(i), "m"(fsave), "m"(fxsave) 91 1.1 mgorny : "st" 92 1.1 mgorny ); 93 1.1 mgorny 94 1.1 mgorny /* Self-assertion for working FSAVE/FXSAVE */ 95 1.1 mgorny ATF_REQUIRE_EQ(fsave.s87_cw, fxsave.fx_cw); 96 1.1 mgorny ATF_REQUIRE_EQ(fsave.s87_sw, fxsave.fx_sw); 97 1.1 mgorny 98 1.1 mgorny /* Test process_xmm_to_s87() */ 99 1.1 mgorny process_xmm_to_s87(&fxsave, &fsave_conv); 100 1.1 mgorny ATF_CHECK_EQ(fsave_conv.s87_cw, fsave.s87_cw); 101 1.1 mgorny ATF_CHECK_EQ(fsave_conv.s87_sw, fsave.s87_sw); 102 1.1 mgorny ATF_CHECK_EQ(fsave_conv.s87_tw, fsave.s87_tw); 103 1.1 mgorny for (j = 0; j < i; j++) { 104 1.1 mgorny ATF_CHECK_EQ(fsave_conv.s87_ac[j].f87_exp_sign, 105 1.1 mgorny fsave.s87_ac[j].f87_exp_sign); 106 1.1 mgorny ATF_CHECK_EQ(fsave_conv.s87_ac[j].f87_mantissa, 107 1.1 mgorny fsave.s87_ac[j].f87_mantissa); 108 1.1 mgorny } 109 1.1 mgorny 110 1.1 mgorny /* Test process_s87_to_xmm() */ 111 1.1 mgorny process_s87_to_xmm(&fsave, &fxsave_conv); 112 1.1 mgorny ATF_CHECK_EQ(fxsave_conv.fx_cw, fxsave.fx_cw); 113 1.1 mgorny ATF_CHECK_EQ(fxsave_conv.fx_sw, fxsave.fx_sw); 114 1.1 mgorny ATF_CHECK_EQ(fxsave_conv.fx_tw, fxsave.fx_tw); 115 1.1 mgorny for (j = 0; j < i; j++) { 116 1.1 mgorny ATF_CHECK_EQ(fxsave_conv.fx_87_ac[j].r.f87_exp_sign, 117 1.1 mgorny fxsave.fx_87_ac[j].r.f87_exp_sign); 118 1.1 mgorny ATF_CHECK_EQ(fxsave_conv.fx_87_ac[j].r.f87_mantissa, 119 1.1 mgorny fxsave.fx_87_ac[j].r.f87_mantissa); 120 1.1 mgorny } 121 1.1 mgorny } 122 1.1 mgorny } 123 1.1 mgorny 124 1.1 mgorny struct s87_xmm_test_vector { 125 1.1 mgorny uint16_t sw; 126 1.1 mgorny uint16_t tw; 127 1.1 mgorny uint8_t tw_abridged; 128 1.1 mgorny }; 129 1.1 mgorny 130 1.1 mgorny struct s87_xmm_test_vector FIXED_TEST_VECTORS[] = { 131 1.1 mgorny {0x3800, 0x3fff, 0x80}, 132 1.1 mgorny {0x3000, 0x2fff, 0xc0}, 133 1.1 mgorny {0x2800, 0x27ff, 0xe0}, 134 1.1 mgorny {0x2000, 0x25ff, 0xf0}, 135 1.1 mgorny {0x1800, 0x25bf, 0xf8}, 136 1.1 mgorny {0x1000, 0x25af, 0xfc}, 137 1.1 mgorny {0x0800, 0x25ab, 0xfe}, 138 1.1 mgorny {0x0000, 0x25a8, 0xff}, 139 1.1 mgorny }; 140 1.1 mgorny 141 1.1 mgorny ATF_TC(s87_to_xmm); 142 1.1 mgorny ATF_TC_HEAD(s87_to_xmm, tc) 143 1.1 mgorny { 144 1.1 mgorny atf_tc_set_md_var(tc, "descr", 145 1.1 mgorny "converting from FSAVE to FXSAVE using fixed test vectors"); 146 1.1 mgorny } 147 1.1 mgorny 148 1.1 mgorny ATF_TC_BODY(s87_to_xmm, tc) 149 1.1 mgorny { 150 1.1 mgorny struct save87 fsave; 151 1.1 mgorny struct fxsave fxsave; 152 1.1 mgorny int i, j; 153 1.1 mgorny 154 1.1 mgorny memset(&fsave, 0, sizeof(fsave)); 155 1.1 mgorny for (i = 0; i < __arraycount(ST_INPUTS); i++) { 156 1.1 mgorny fsave.s87_sw = FIXED_TEST_VECTORS[i].sw; 157 1.1 mgorny fsave.s87_tw = FIXED_TEST_VECTORS[i].tw; 158 1.1 mgorny for (j = 0; j <= i; j++) 159 1.1 mgorny fsave.s87_ac[i - j] = ST_INPUTS[j].r; 160 1.1 mgorny 161 1.1 mgorny process_s87_to_xmm(&fsave, &fxsave); 162 1.1 mgorny ATF_CHECK_EQ(fxsave.fx_sw, FIXED_TEST_VECTORS[i].sw); 163 1.1 mgorny ATF_CHECK_EQ(fxsave.fx_tw, FIXED_TEST_VECTORS[i].tw_abridged); 164 1.1 mgorny for (j = 0; j < i; j++) { 165 1.1 mgorny ATF_CHECK_EQ(fxsave.fx_87_ac[i - j].r.f87_exp_sign, 166 1.1 mgorny ST_INPUTS[j].r.f87_exp_sign); 167 1.1 mgorny ATF_CHECK_EQ(fxsave.fx_87_ac[i - j].r.f87_mantissa, 168 1.1 mgorny ST_INPUTS[j].r.f87_mantissa); 169 1.1 mgorny } 170 1.1 mgorny } 171 1.1 mgorny } 172 1.1 mgorny 173 1.1 mgorny ATF_TC(xmm_to_s87); 174 1.1 mgorny ATF_TC_HEAD(xmm_to_s87, tc) 175 1.1 mgorny { 176 1.1 mgorny atf_tc_set_md_var(tc, "descr", 177 1.1 mgorny "converting from FSAVE to FXSAVE using fixed test vectors"); 178 1.1 mgorny } 179 1.1 mgorny 180 1.1 mgorny ATF_TC_BODY(xmm_to_s87, tc) 181 1.1 mgorny { 182 1.1 mgorny struct save87 fsave; 183 1.1 mgorny struct fxsave fxsave; 184 1.1 mgorny int i, j; 185 1.1 mgorny 186 1.1 mgorny memset(&fxsave, 0, sizeof(fxsave)); 187 1.1 mgorny for (i = 0; i < __arraycount(ST_INPUTS); i++) { 188 1.1 mgorny fxsave.fx_sw = FIXED_TEST_VECTORS[i].sw; 189 1.1 mgorny fxsave.fx_tw = FIXED_TEST_VECTORS[i].tw_abridged; 190 1.1 mgorny for (j = 0; j <= i; j++) 191 1.1 mgorny fxsave.fx_87_ac[i - j] = ST_INPUTS[j]; 192 1.1 mgorny 193 1.1 mgorny process_xmm_to_s87(&fxsave, &fsave); 194 1.1 mgorny ATF_CHECK_EQ(fsave.s87_sw, FIXED_TEST_VECTORS[i].sw); 195 1.1 mgorny ATF_CHECK_EQ(fsave.s87_tw, FIXED_TEST_VECTORS[i].tw); 196 1.1 mgorny for (j = 0; j < i; j++) { 197 1.1 mgorny ATF_CHECK_EQ(fsave.s87_ac[i - j].f87_exp_sign, 198 1.1 mgorny ST_INPUTS[j].r.f87_exp_sign); 199 1.1 mgorny ATF_CHECK_EQ(fsave.s87_ac[i - j].f87_mantissa, 200 1.1 mgorny ST_INPUTS[j].r.f87_mantissa); 201 1.1 mgorny } 202 1.1 mgorny } 203 1.1 mgorny } 204 1.1 mgorny 205 1.1 mgorny ATF_TP_ADD_TCS(tp) 206 1.1 mgorny { 207 1.1 mgorny ATF_TP_ADD_TC(tp, fsave_fxsave_hw); 208 1.1 mgorny ATF_TP_ADD_TC(tp, s87_to_xmm); 209 1.1 mgorny ATF_TP_ADD_TC(tp, xmm_to_s87); 210 1.1 mgorny return atf_no_error(); 211 1.1 mgorny } 212