Bladeren bron

[test] Add tests for 64-bit division

On a 32-bit system, 64-bit division is implemented using the libgcc
functions provided in __udivmoddi4.c etc.  Calls to these functions
are generated automatically by gcc, with a calling convention that is
somewhat empirical in nature.  Add these self-tests primarily as a
check that we are using the correct calling convention.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 jaren geleden
bovenliggende
commit
ad7d5af5e1
1 gewijzigde bestanden met toevoegingen van 122 en 0 verwijderingen
  1. 122
    0
      src/tests/math_test.c

+ 122
- 0
src/tests/math_test.c Bestand weergeven

@@ -43,6 +43,54 @@ __attribute__ (( noinline )) int flsl_var ( long value ) {
43 43
 	return flsl ( value );
44 44
 }
45 45
 
46
+/**
47
+ * Force a use of runtime 64-bit unsigned integer division
48
+ *
49
+ * @v dividend		Dividend
50
+ * @v divisor		Divisor
51
+ * @ret quotient	Quotient
52
+ */
53
+__attribute__ (( noinline )) uint64_t u64div_var ( uint64_t dividend,
54
+						   uint64_t divisor ) {
55
+	return ( dividend / divisor );
56
+}
57
+
58
+/**
59
+ * Force a use of runtime 64-bit unsigned integer modulus
60
+ *
61
+ * @v dividend		Dividend
62
+ * @v divisor		Divisor
63
+ * @ret remainder	Remainder
64
+ */
65
+__attribute__ (( noinline )) uint64_t u64mod_var ( uint64_t dividend,
66
+						   uint64_t divisor ) {
67
+	return ( dividend % divisor );
68
+}
69
+
70
+/**
71
+ * Force a use of runtime 64-bit signed integer division
72
+ *
73
+ * @v dividend		Dividend
74
+ * @v divisor		Divisor
75
+ * @ret quotient	Quotient
76
+ */
77
+__attribute__ (( noinline )) int64_t s64div_var ( int64_t dividend,
78
+						  int64_t divisor ) {
79
+	return ( dividend / divisor );
80
+}
81
+
82
+/**
83
+ * Force a use of runtime 64-bit unsigned integer modulus
84
+ *
85
+ * @v dividend		Dividend
86
+ * @v divisor		Divisor
87
+ * @ret remainder	Remainder
88
+ */
89
+__attribute__ (( noinline )) int64_t s64mod_var ( int64_t dividend,
90
+						  int64_t divisor ) {
91
+	return ( dividend % divisor );
92
+}
93
+
46 94
 /**
47 95
  * Report a flsl() test result
48 96
  *
@@ -62,6 +110,60 @@ flsl_okx ( long value, int msb, const char *file, unsigned int line ) {
62 110
 }
63 111
 #define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )
64 112
 
113
+/**
114
+ * Report a 64-bit unsigned integer division test result
115
+ *
116
+ * @v dividend		Dividend
117
+ * @v divisor		Divisor
118
+ * @v quotient		Quotient
119
+ * @v remainder		Remainder
120
+ * @v file		Test code file
121
+ * @v line		Test code line
122
+ */
123
+static void u64divmod_okx ( uint64_t dividend, uint64_t divisor,
124
+			    uint64_t quotient, uint64_t remainder,
125
+			    const char *file, unsigned int line ) {
126
+
127
+	/* Sanity check */
128
+	okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
129
+
130
+	/* Check division */
131
+	okx ( u64div_var ( dividend, divisor ) == quotient, file, line );
132
+
133
+	/* Check modulus */
134
+	okx ( u64mod_var ( dividend, divisor ) == remainder, file, line );
135
+}
136
+#define u64divmod_ok( dividend, divisor, quotient, remainder )	\
137
+	u64divmod_okx ( dividend, divisor, quotient, remainder,	\
138
+			__FILE__, __LINE__ )
139
+
140
+/**
141
+ * Report a 64-bit signed integer division test result
142
+ *
143
+ * @v dividend		Dividend
144
+ * @v divisor		Divisor
145
+ * @v quotient		Quotient
146
+ * @v remainder		Remainder
147
+ * @v file		Test code file
148
+ * @v line		Test code line
149
+ */
150
+static void s64divmod_okx ( int64_t dividend, int64_t divisor,
151
+			    int64_t quotient, int64_t remainder,
152
+			    const char *file, unsigned int line ) {
153
+
154
+	/* Sanity check */
155
+	okx ( ( ( divisor * quotient ) + remainder ) == dividend, file, line );
156
+
157
+	/* Check division */
158
+	okx ( s64div_var ( dividend, divisor ) == quotient, file, line );
159
+
160
+	/* Check modulus */
161
+	okx ( s64mod_var ( dividend, divisor ) == remainder, file, line );
162
+}
163
+#define s64divmod_ok( dividend, divisor, quotient, remainder )	\
164
+	s64divmod_okx ( dividend, divisor, quotient, remainder,	\
165
+			__FILE__, __LINE__ )
166
+
65 167
 /**
66 168
  * Perform mathematical self-tests
67 169
  *
@@ -77,6 +179,26 @@ static void math_test_exec ( void ) {
77 179
 	flsl_ok ( 0x69505845, 31 );
78 180
 	flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
79 181
 	flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
182
+
183
+	/* Test 64-bit arithmetic
184
+	 *
185
+	 * On a 64-bit machine, these tests are fairly meaningless.
186
+	 *
187
+	 * On a 32-bit machine, these tests verify the correct
188
+	 * operation of our libgcc functions __udivmoddi4()
189
+	 * etc. (including checking that the implicit calling
190
+	 * convention assumed by gcc matches our expectations).
191
+	 */
192
+	u64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
193
+		       0x2eef6ab4ULL, 0x0e12f089ULL );
194
+	s64divmod_ok ( 0x2b90ddccf699f765ULL, 0xed9f5e73ULL,
195
+		       0x2eef6ab4ULL, 0x0e12f089ULL );
196
+	u64divmod_ok ( 0xc09e00dcb9e34b54ULL, 0x35968185cdc744f3ULL,
197
+		       3, 0x1fda7c4b508d7c7bULL );
198
+	s64divmod_ok ( -0x3f61ff23461cb4acLL, 0x35968185cdc744f3ULL,
199
+		       -1LL, -0x9cb7d9d78556fb9LL );
200
+	u64divmod_ok ( 0, 0x5b2f2737f4ffULL, 0, 0 );
201
+	s64divmod_ok ( 0, 0xbb00ded72766207fULL, 0, 0 );
80 202
 }
81 203
 
82 204
 /** Mathematical self-tests */

Laden…
Annuleren
Opslaan