Upgrade to Pro — share decks privately, control downloads, hide ads and more …

四則演算を理解する/four-arithmetic-operations

 四則演算を理解する/four-arithmetic-operations

以下のイベントで発表した資料です!
https://ariaki.connpass.com/event/245907/

kohei.okamoto

May 17, 2022
Tweet

More Decks by kohei.okamoto

Other Decks in Programming

Transcript

  1. ࣗݾ঺հɿԬຊ ᕣฏ • େֶ͸Խֶͷݚڀ • ৽ଔ͸ۚ༥ܥͷSIer (γϯϓϨΫε) • 2021/7ʙ ϦΫϧʔτͰΤϯδχΞͯ͠·͢

    • झຯɿԻήʔɺڝϓϩɺΫιΞϓϦ࡞੒ • Twitter: @okaponta_ • GitHub: @okaponta
  2. Ճ๏ • ͖͞΄Ͳͷ࿦ཧԋࢉͷ૊Έ߹ΘͤͰදݱͰ͖Δɻ • (1ͷҐ) A XOR B • (2ͷҐ)

    A AND B • ൒Ճࢉث͍ͬͯ͏Β͍͠ɻ • https://nandgame.com/
  3. Ճ๏ • ܁Γ্͕Γʹ΋ରԠͨ͠΋ͷΛશՃࢉثͱ͍͏ • (1ͷҐ) (A XOR B) XOR C

    • (2ͷҐ) (A AND B) OR ((A XOR B) AND C) • ͪͳΈʹɺNANDͷΈͰશͯͷ࿦ཧճ࿏Λද ݱͰ͖ΔΒ͍͠Ͱ͢ɻ
  4. Ճ๏ • ܁Γ্͕Γʹ΋ରԠͨ͠΋ͷΛશՃࢉثͱ͍͏ • (1ͷҐ) (A XOR B) XOR C

    • (2ͷҐ) (A AND B) OR ((A XOR B) AND C) • ͪͳΈʹɺNANDͷΈͰશͯͷ࿦ཧճ࿏Λද ݱͰ͖ΔΒ͍͠Ͱ͢ɻ
  5. γϑτԋࢉͯ͠Δ࣮ͬͯײ͍ͨ͠ package mai n func mul(x, y int) int {

    return x * y } func main() { mul(5, 13 ) } > go tool compile -S mul.g o 0x0000 00000 (mul.go:4) MOVD "".x(FP), R 0 0x0004 00004 (mul.go:4) MOVD "".y+8(FP), R 1 0x0008 00008 (mul.go:4) MUL R1, R0, R 0 0x000c 00012 (mul.go:4) MOVD R0, "".~r2+16(FP ) 0x0010 00016 (mul.go:4) RET (R30) ΞηϯϒϥϨϕϧͰ .6-໋ྩ͕͋Δ
  6. γϑτԋࢉͯ͠Δ࣮ͬͯײ͍ͨ͠(ͺʔͱ2 func Benchmark_mul_2exp(b *testing.B) { var base int64 = get2exp()

    // 2^30 or 2^3 1 var hoge int64 = get2exp() - 1 b.ResetTimer( ) for i := 0; i < b.N; i++ { for j := 0; j < 10000; j++ { hoge = mul64(hoge, base ) } } if hoge == 1 { b.Fail( ) } } ͜͜ΛCBTFIPHFΛ͍Ζ͍Ζ ม࣮͑ͯݧͯ͠ΈΔ
  7. γϑτԋࢉͯ͠Δ࣮ͬͯײ͍ͨ͠(ͺʔͱ2 > go test -bench . -benchme m goos: darwi

    n goarch: arm6 4 pkg: mu l Benchmark_mul_2exp-8 123480 9659 ns/op 0 B/op 0 allocs/o p Benchmark_mul_2exp_minus1-8 123620 9651 ns/op 0 B/op 0 allocs/o p Benchmark_mul_2exp_plus1-8 123568 9647 ns/op 0 B/op 0 allocs/o p Benchmark_plus_2exp-8 365474 3226 ns/op 0 B/op 0 allocs/o p PAS S ok mul 5.341s
  8. আ๏ • ஗͍ͱઐΒධ൑ • SRT๏ • ϧοΫΞοϓςʔϒϧΛ༻͍ͯෳ਺ͷܻΛ·ͱΊׂͯΓࢉ • 2ܻͣͭॲཧ͢Δͷ͕Radix-4 Divider

    • 4ܻͣͭॲཧ͢Δͷ͕Radix-16 Divider • ଞʹ΋χϡʔτϯ๏Λ࢖͏ํ๏΍ɺΰʔϧυγϡϛοτ๏ͳΔ΋ͷ͕͋ΔΒ ͍͠
  9. আࢉ func Benchmark_div_2exp(b *testing.B) { var base int = getSmall2exp()

    // 2^5 or 2^ 6 var hoge int = get2exp() - 1 b.ResetTimer( ) for i := 0; i < b.N; i++ { for j := 0; j < 10000; j++ { hoge = div(hoge, base ) } } if hoge == 1 { b.Fail( ) } } ͜͜ΛมԽͤͯ͞ΈΔ
  10. আࢉ > go test -bench . -benchmem goos: darwi n

    goarch: arm6 4 pkg: di v Benchmark_div_2exp-8 51686 22046 ns/op 0 B/op 0 allocs/o p Benchmark_div_2exp_minus1-8 53272 21935 ns/op 0 B/op 0 allocs/o p Benchmark_div_2exp_plus1-8 54561 21931 ns/op 0 B/op 0 allocs/o p Benchmark_plus_2exp-8 372552 3140 ns/op 0 B/op 0 allocs/o p PAS S ok div 5.487s
  11. আࢉʢఆ਺ʣ func div2(x uint) uint { return x / 2

    } func div3(x uint16) uint16 { return x / 3 } > go tool compile -S ./div-const.g o 0x0000 00000 (./div-const.go:4) MOVD "".x(FP), R 0 0x0004 00004 (./div-const.go:4) LSR $1, R0, R 0 0x0008 00008 (./div-const.go:4) MOVD R0, "".~r1+8(FP ) 0x000c 00012 (./div-const.go:4) RET (R30)
  12. আࢉʢఆ਺ʣ func div2(x uint) uint { return x / 2

    } func div3(x uint16) uint16 { return x / 3 } > go tool compile -S ./div-const.g o 0x0000 00000 (./div-const.go:4) MOVD "".x(FP), R 0 0x0004 00004 (./div-const.go:4) LSR $1, R0, R 0 0x0008 00008 (./div-const.go:4) MOVD R0, "".~r1+8(FP ) 0x000c 00012 (./div-const.go:4) RET (R30) ࠨγϑτԋࢉ
  13. আࢉʢఆ਺ʣ func div2(x uint) uint { return x / 2

    } func div3(x uint16) uint16 { return x / 3 } > go tool compile -S ./div-const.g o 0x0000 00000 (./div-const.go:8) MOVHU "".x(FP), R 0 0x0004 00004 (./div-const.go:8) MOVD $87382, R 1 0x000c 00012 (./div-const.go:8) MUL R0, R1, R 0 0x0010 00016 (./div-const.go:8) LSR $18, R0, R 0 0x0014 00020 (./div-const.go:8) MOVH R0, "".~r1+8(FP ) 0x0018 00024 (./div-const.go:8) RET (R30) ͱͷ ֻ͚ࢉʹม׵
  14. আࢉʢఆ਺ʣ func div2(x uint) uint { return x / 2

    } func div3(x uint16) uint16 { return x / 3 } > go tool compile -S ./div-const.g o 0x0000 00000 (./div-const.go:8) MOVHU "".x(FP), R 0 0x0004 00004 (./div-const.go:8) MOVD $87382, R 1 0x000c 00012 (./div-const.go:8) MUL R0, R1, R 0 0x0010 00016 (./div-const.go:8) LSR $18, R0, R 0 0x0014 00020 (./div-const.go:8) MOVH R0, "".~r1+8(FP ) 0x0018 00024 (./div-const.go:8) RET (R30) x 3 = x × 1 3 = x × 218 3 × 1 218 ≃ x × 218 + 2 3 × 1 218