generic_float.odin 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package strconv
  2. import "decimal"
  3. Decimal_Slice :: struct {
  4. digits: []byte,
  5. count: int,
  6. decimal_point: int,
  7. neg: bool,
  8. }
  9. Float_Info :: struct {
  10. mantbits: uint,
  11. expbits: uint,
  12. bias: int,
  13. }
  14. _f16_info := Float_Info{10, 5, -15}
  15. _f32_info := Float_Info{23, 8, -127}
  16. _f64_info := Float_Info{52, 11, -1023}
  17. /*
  18. Converts a floating-point number to a string with the specified format and precision.
  19. **Inputs**
  20. buf: A byte slice to store the resulting string
  21. val: The floating-point value to be converted
  22. fmt: The formatting byte, accepted values are 'e', 'E', 'f', 'F', 'g', 'G'
  23. precision: The number of decimal places to round to
  24. bit_size: The size of the floating-point number in bits, valid values are 16, 32, 64
  25. Example:
  26. buf: [32]byte
  27. val := 3.141592
  28. fmt := 'f'
  29. precision := 2
  30. bit_size := 64
  31. result := strconv.generic_ftoa(buf[:], val, fmt, precision, bit_size) -> "3.14"
  32. **Returns**
  33. - A byte slice containing the formatted string
  34. */
  35. generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, precision, bit_size: int) -> []byte {
  36. bits: u64
  37. flt: ^Float_Info
  38. switch bit_size {
  39. case 16:
  40. bits = u64(transmute(u16)f16(val))
  41. flt = &_f16_info
  42. case 32:
  43. bits = u64(transmute(u32)f32(val))
  44. flt = &_f32_info
  45. case 64:
  46. bits = transmute(u64)val
  47. flt = &_f64_info
  48. case:
  49. panic("strconv: invalid bit_size")
  50. }
  51. neg := bits>>(flt.expbits+flt.mantbits) != 0
  52. exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
  53. mant := bits & (u64(1) << flt.mantbits - 1)
  54. switch exp {
  55. case 1<<flt.expbits - 1:
  56. s: string
  57. if mant != 0 {
  58. s = "NaN"
  59. } else if neg {
  60. s = "-Inf"
  61. } else {
  62. s = "+Inf"
  63. }
  64. n := copy(buf, s)
  65. return buf[:n]
  66. case 0: // denormalized
  67. exp += 1
  68. case:
  69. mant |= u64(1) << flt.mantbits
  70. }
  71. exp += flt.bias
  72. d_: decimal.Decimal
  73. d := &d_
  74. decimal.assign(d, mant)
  75. decimal.shift(d, exp - int(flt.mantbits))
  76. digs: Decimal_Slice
  77. prec := precision
  78. shortest := prec < 0
  79. if shortest {
  80. round_shortest(d, mant, exp, flt)
  81. digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point}
  82. switch fmt {
  83. case 'e', 'E': prec = digs.count-1
  84. case 'f', 'F': prec = max(digs.count-digs.decimal_point, 0)
  85. case 'g', 'G': prec = digs.count
  86. }
  87. } else {
  88. switch fmt {
  89. case 'e', 'E':
  90. prec += 1
  91. decimal.round(d, prec)
  92. case 'f', 'F':
  93. decimal.round(d, d.decimal_point+prec)
  94. case 'g', 'G':
  95. if prec == 0 {
  96. prec = 1
  97. }
  98. decimal.round(d, prec)
  99. }
  100. digs = Decimal_Slice{digits = d.digits[:], count = d.count, decimal_point = d.decimal_point}
  101. }
  102. return format_digits(buf, shortest, neg, digs, prec, fmt)
  103. }
  104. /*
  105. Converts a decimal floating-point number into a byte buffer with the given format
  106. **Inputs**
  107. - buf: The byte buffer to store the formatted number
  108. - shortest: If true, generates the shortest representation of the number
  109. - neg: If true, the number is negative
  110. - digs: The decimal number to be formatted
  111. - precision: The number of digits after the decimal point
  112. - fmt: The format specifier (accepted values: 'f', 'F', 'e', 'E', 'g', 'G')
  113. **Returns**
  114. - A byte slice containing the formatted decimal floating-point number
  115. */
  116. format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slice, precision: int, fmt: byte) -> []byte {
  117. Buffer :: struct {
  118. b: []byte,
  119. n: int,
  120. }
  121. to_bytes :: proc(b: Buffer) -> []byte {
  122. return b.b[:b.n]
  123. }
  124. add_bytes :: proc(buf: ^Buffer, bytes: ..byte) {
  125. buf.n += copy(buf.b[buf.n:], bytes)
  126. }
  127. b := Buffer{b = buf}
  128. prec := precision
  129. switch fmt {
  130. case 'f', 'F':
  131. add_bytes(&b, '-' if neg else '+')
  132. // integer, padded with zeros when needed
  133. if digs.decimal_point > 0 {
  134. m := min(digs.count, digs.decimal_point)
  135. add_bytes(&b, ..digs.digits[0:m])
  136. for ; m < digs.decimal_point; m += 1 {
  137. add_bytes(&b, '0')
  138. }
  139. } else {
  140. add_bytes(&b, '0')
  141. }
  142. // fractional part
  143. if prec > 0 {
  144. add_bytes(&b, '.')
  145. for i in 0..<prec {
  146. c: byte = '0'
  147. if j := digs.decimal_point + i; 0 <= j && j < digs.count {
  148. c = digs.digits[j]
  149. }
  150. add_bytes(&b, c)
  151. }
  152. }
  153. return to_bytes(b)
  154. case 'e', 'E':
  155. add_bytes(&b, '-' if neg else '+')
  156. ch := byte('0')
  157. if digs.count != 0 {
  158. ch = digs.digits[0]
  159. }
  160. add_bytes(&b, ch)
  161. if prec > 0 {
  162. add_bytes(&b, '.')
  163. i := 1
  164. m := min(digs.count, prec+1)
  165. if i < m {
  166. add_bytes(&b, ..digs.digits[i:m])
  167. i = m
  168. }
  169. for ; i <= prec; i += 1 {
  170. add_bytes(&b, '0')
  171. }
  172. }
  173. add_bytes(&b, fmt)
  174. exp := digs.decimal_point-1
  175. if digs.count == 0 {
  176. // Zero has exponent of 0
  177. exp = 0
  178. }
  179. ch = '+'
  180. if exp < 0 {
  181. ch = '-'
  182. exp = -exp
  183. }
  184. add_bytes(&b, ch)
  185. switch {
  186. case exp < 10: add_bytes(&b, '0', byte(exp)+'0') // add prefix 0
  187. case exp < 100: add_bytes(&b, byte(exp/10)+'0', byte(exp%10)+'0')
  188. case: add_bytes(&b, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
  189. }
  190. return to_bytes(b)
  191. case 'g', 'G':
  192. eprec := prec
  193. if eprec > digs.count && digs.count >= digs.decimal_point {
  194. eprec = digs.count
  195. }
  196. if shortest {
  197. eprec = 6
  198. }
  199. exp := digs.decimal_point - 1
  200. if exp < -4 || exp >= eprec {
  201. if prec > digs.count {
  202. prec = digs.count
  203. }
  204. return format_digits(buf, shortest, neg, digs, prec-1, fmt+'e'-'g') // keep the same case
  205. }
  206. if prec > digs.decimal_point {
  207. prec = digs.count
  208. }
  209. return format_digits(buf, shortest, neg, digs, max(prec-digs.decimal_point, 0), 'f')
  210. case:
  211. add_bytes(&b, '%', fmt)
  212. return to_bytes(b)
  213. }
  214. }
  215. /*
  216. Rounds the given decimal number to its shortest representation, considering the provided floating-point format
  217. **Inputs**
  218. - d: The decimal number to round
  219. - mant: The mantissa of the floating-point number
  220. - exp: The exponent of the floating-point number
  221. - flt: Pointer to the Float_Info structure containing information about the floating-point format
  222. */
  223. round_shortest :: proc(d: ^decimal.Decimal, mant: u64, exp: int, flt: ^Float_Info) {
  224. if mant == 0 { // If mantissa is zero, the number is zero
  225. d.count = 0
  226. return
  227. }
  228. /*
  229. 10^(dp-nd) > 2^(exp-mantbits)
  230. log2(10) * (dp-nd) > exp-mantbits
  231. log(2) >~ 0.332
  232. 332*(dp-nd) >= 100*(exp-mantbits)
  233. */
  234. minexp := flt.bias+1
  235. if exp > minexp && 332*(d.decimal_point-d.count) >= 100*(exp - int(flt.mantbits)) {
  236. // Number is already its shortest
  237. return
  238. }
  239. upper_: decimal.Decimal; upper := &upper_
  240. decimal.assign(upper, 2*mant - 1)
  241. decimal.shift(upper, exp - int(flt.mantbits) - 1)
  242. mantlo: u64
  243. explo: int
  244. if mant > 1<<flt.mantbits || exp == minexp {
  245. mantlo = mant-1
  246. explo = exp
  247. } else {
  248. mantlo = 2*mant - 1
  249. explo = exp-1
  250. }
  251. lower_: decimal.Decimal; lower := &lower_
  252. decimal.assign(lower, 2*mantlo + 1)
  253. decimal.shift(lower, explo - int(flt.mantbits) - 1)
  254. inclusive := mant%2 == 0
  255. for i in 0..<d.count {
  256. l: byte = '0' // lower digit
  257. if i < lower.count {
  258. l = lower.digits[i]
  259. }
  260. m := d.digits[i] // middle digit
  261. u: byte = '0' // upper digit
  262. if i < upper.count {
  263. u = upper.digits[i]
  264. }
  265. ok_round_down := l != m || inclusive && i+1 == lower.count
  266. ok_round_up := m != u && (inclusive || m+1 < u || i+1 < upper.count)
  267. if ok_round_down && ok_round_up {
  268. decimal.round(d, i+1)
  269. return
  270. }
  271. if ok_round_down {
  272. decimal.round_down(d, i+1)
  273. return
  274. }
  275. if ok_round_up {
  276. decimal.round_up(d, i+1)
  277. return
  278. }
  279. }
  280. }
  281. /*
  282. Converts a decimal number to its floating-point representation with the given format and returns the resulting bits
  283. **Inputs**
  284. - d: Pointer to the decimal number to convert
  285. - info: Pointer to the Float_Info structure containing information about the floating-point format
  286. **Returns**
  287. - b: The bits representing the floating-point number
  288. - overflow: A boolean indicating whether an overflow occurred during conversion
  289. */
  290. @(private)
  291. decimal_to_float_bits :: proc(d: ^decimal.Decimal, info: ^Float_Info) -> (b: u64, overflow: bool) {
  292. end :: proc "contextless" (d: ^decimal.Decimal, mant: u64, exp: int, info: ^Float_Info) -> (bits: u64) {
  293. bits = mant & (u64(1)<<info.mantbits - 1)
  294. bits |= u64((exp-info.bias) & (1<<info.expbits - 1)) << info.mantbits
  295. if d.neg {
  296. bits |= 1<< info.mantbits << info.expbits
  297. }
  298. return
  299. }
  300. set_overflow :: proc "contextless" (mant: ^u64, exp: ^int, info: ^Float_Info) -> bool {
  301. mant^ = 0
  302. exp^ = 1<<info.expbits - 1 + info.bias
  303. return true
  304. }
  305. mant: u64
  306. exp: int
  307. if d.count == 0 {
  308. mant = 0
  309. exp = info.bias
  310. b = end(d, mant, exp, info)
  311. return
  312. }
  313. if d.decimal_point > 310 {
  314. set_overflow(&mant, &exp, info)
  315. b = end(d, mant, exp, info)
  316. return
  317. } else if d.decimal_point < -330 {
  318. mant = 0
  319. exp = info.bias
  320. b = end(d, mant, exp, info)
  321. return
  322. }
  323. @static power_table := [?]int{1, 3, 6, 9, 13, 16, 19, 23, 26}
  324. exp = 0
  325. for d.decimal_point > 0 {
  326. n := 27 if d.decimal_point >= len(power_table) else power_table[d.decimal_point]
  327. decimal.shift(d, -n)
  328. exp += n
  329. }
  330. for d.decimal_point < 0 || d.decimal_point == 0 && d.digits[0] < '5' {
  331. n := 27 if -d.decimal_point >= len(power_table) else power_table[-d.decimal_point]
  332. decimal.shift(d, n)
  333. exp -= n
  334. }
  335. // go from [0.5, 1) to [1, 2)
  336. exp -= 1
  337. if exp < info.bias + 1 {
  338. n := info.bias + 1 - exp
  339. decimal.shift(d, n)
  340. exp += n
  341. }
  342. if (exp-info.bias) >= (1<<info.expbits - 1) {
  343. set_overflow(&mant, &exp, info)
  344. b = end(d, mant, exp, info)
  345. return
  346. }
  347. decimal.shift(d, int(1 + info.mantbits))
  348. mant = decimal.rounded_integer(d)
  349. if mant == 2<<info.mantbits {
  350. mant >>= 1
  351. exp += 1
  352. if (exp-info.bias) >= (1<<info.expbits - 1) {
  353. set_overflow(&mant, &exp, info)
  354. b = end(d, mant, exp, info)
  355. return
  356. }
  357. }
  358. if mant & (1<<info.mantbits) == 0 {
  359. exp = info.bias
  360. }
  361. b = end(d, mant, exp, info)
  362. return
  363. }