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